`

C#(winForm)写在公共类的几个常用方法

    博客分类:
  • c#
阅读更多

 C#(winForm)写在公共类的几个常用方法
/*****************************************************************************      说明
** 1.获取数据库的连接,返回值需判断是否为null-----------GetSqlConnection
*  2.根据Select--查询语句,返回DataSet-------------------GetDataSet
*  3.使用数据库内容填充DataGrid----------------FillDataGridFromSQLString
*  4.使用数据库内容填充DataGrid----------------FillDataGridFromSQLString(重载)
*  5、返回SQL语句所查询出来的行数-------------------------------GetRowCount
*  6.填充下拉列表------------------------------------------FillComboBox*   
   7.由一条SQL语句生成一个DataReader;返回值需要判断是否为空------GetDataReader
*  8.返回单个查询数据:第一列,第一行的值-------------------GetFirstData
*  9.对数据库中的一条记录操作:增、删、更新---------------ExecuteCommand
*  10.对数据库进行增删改操作-----------------------------ExecuteCommand2
*  11.判断str是不是全是由数字构成-------------------------IsNumeric
*  12.检测含有中文字符串的实际长度------------------------len
***************************************************************************/
using System;
using System.Data.SqlClient;
using System.Data;
using System.Windows.Forms;
namespace Tayside.Common
{
   /// <summary>
   /// DataBase 的摘要说明。
   /// </summary>
   public class DataBase
   {
      public DataBase()
      {
      }
      /// <summary>
      /// 1.获取数据库的连接,返回值需判断是否为null
      /// </summary>
      /// <returns></returns>
      public static SqlConnection GetSqlConnection()
      {
         string strCnn = "Server=192.168.12.136;database=Tayside;user ;
         try
         {
            SqlConnection sqlCnn = new SqlConnection(strCnn);
            sqlCnn.Open();
            return sqlCnn;
         }
         catch(Exception ee)
         {
            string temp=ee.Message;
            return null;
         }
      }

      /// <summary>
      /// 获取SqlCommand对象
      /// </summary>
      /// <returns></returns>
      public static SqlCommand GetSqlCommand()
      {
         SqlConnection sqlCnn = GetSqlConnection();
         if(sqlCnn == null)
            return null;
         else
         {
            SqlCommand sqlCmm = new SqlCommand();
            sqlCmm.Connection = sqlCnn;
            return sqlCmm;
         }
      }

      /// <summary>
      /// 2.根据Select--查询语句,返回DataSet
      /// </summary>
      /// <param >Select SQL语句</param>
      /// <returns>返回值需判断是否为空</returns>
      public static DataSet GetDataSet(string strSql)
      {
         try
         {
            using( SqlConnection sqlCnn = GetSqlConnection() )
            {
               SqlDataAdapter dataAdapter = new SqlDataAdapter( strSql, sqlCnn );
               DataSet dataSet = new DataSet();
               dataAdapter.Fill( dataSet );
               return dataSet; 
            }
         }
         catch
         {
            return null;
         }
      }

      /// <summary>
      /// 3.使用数据库内容填充DataGrid
      /// </summary>
      /// <param >要填充的DataGrid</param>
      /// <param >要获取数据库内容的SQL字符串</param>
      /// <returns></returns>
      public static bool FillDataGridFromSQLString( DataGrid dataGrid,string strSql)
      {
         try
         {
            DataSet ds = GetDataSet(strSql);
            dataGrid.SetDataBinding(ds, "");

            return true;
         }
         catch(Exception ee)
         {
            string t=ee.Message;
            return false;
         }
      }

      /// <summary>
      /// 4.使用数据库内容填充DataGrid
      /// </summary>
      /// <param >要填充的DataGrid</param>
      /// <param >要获取数据库内容的SQL字符串</param>
      /// <param >要添充DataGrid的表名</param>
      /// <returns></returns>
      public static bool FillDataGridFromSQLString( DataGrid dataGrid,string strSql,string table)
      {
         try
         {
            DataSet ds = GetDataSet(strSql);
            dataGrid.SetDataBinding(ds, table);

            return true;
         }
         catch(Exception ee)
         {
            string t=ee.Message;
            return false;
         }
      }

      /// <summary>
      /// 5、返回SQL语句所查询出来的行数
      /// </summary>
      /// <param ></param>
      /// <returns></returns>
      public static int GetRowCount(string strSql)
      {
         DataSet ds = GetDataSet(strSql);
         int count = ds.Tables[0].Rows.Count;
         return count;
      }

      /// <summary>
      /// 6.填充下拉列表
      /// </summary>
      /// <param >要添充的ComboBox</param>
      /// <param >查询语句</param>
      /// <returns>是否成功</returns>
      public static bool FillComboBox(ComboBox cmBox,string strSql)
      {
         try
         {
            using(SqlConnection sqlCnn = GetSqlConnection())
            {
               SqlDataReader dr = GetDataReader(strSql);
               while(dr.Read())
               {
                  cmBox.Items.Add(dr.GetValue(0));
               }
               return true;
            }
         }
         catch
         {
            return false;
         }
      }

      /// <summary>
      /// 7.由一条SQL语句生成一个DataReader;返回值需要判断是否为空
      /// </summary>
      /// <param >要使用的SQl语句</param>
      /// <returns></returns>
      public static SqlDataReader GetDataReader(string strSql)
      {
         try
         {
            SqlConnection sqlCnn = GetSqlConnection();
            SqlCommand sqlCmm = new SqlCommand(strSql,sqlCnn);
            return sqlCmm.ExecuteReader(CommandBehavior.CloseConnection);
         }
         catch
         {
            return null;
         }
      }

      /// <summary>
      /// 8.返回单个查询数据:第一列,第一行的值
      /// </summary>
      /// <param >Select SQL语句</param>
      /// <returns></returns>
      public static string GetFirstData(string strSql)
      {
         try
         {
            using( SqlConnection sqlCnn = GetSqlConnection() )
            {
               SqlCommand sqlCmm = new SqlCommand(strSql,sqlCnn);
               return sqlCmm.ExecuteScalar().ToString();
            }
         }
         catch
         {
            return "";
         }
      }

      /// <summary>
      /// 9.对数据库中的一条记录操作:增、删、更新
      /// </summary>
      /// <param >要执行的SQL语句</param>
      /// <returns>返回执行是否成功</returns>
      public static bool ExecuteCommand(string strSql)
      {
         try
         {
            using( SqlConnection sqlCnn = GetSqlConnection())
            {
               SqlCommand sqlCmm = new SqlCommand(strSql,sqlCnn);
               int temp = sqlCmm.ExecuteNonQuery();
               return  temp == 1;

            }
         }
         catch
         {
            return false;
         }

      }

      /// <summary>
      /// 10.对数据库进行增删改操作
      /// </summary>
      /// <param ></param>
      /// <returns></returns>
      public static bool ExecuteCommand2(string strSql)
      {
         try
         {
            using( SqlConnection sqlCnn = GetSqlConnection())
            {
               SqlCommand sqlCmm = new SqlCommand(strSql,sqlCnn);
               int temp = sqlCmm.ExecuteNonQuery();
               return  true;
            }
         }
         catch
         {
            return false;
         }
      }

      /// <summary>
      /// 11.判断str是不是全是由数字构成
      /// </summary>
      /// <param ></param>
      /// <returns></returns>
      public static bool IsNumeric(string str) 
      { 
         if (str==null || str.Length==0) 
            return false; 
         foreach(char c in str) 
         { 
            if (!Char.IsNumber(c)) 
            { 
               return false; 
            } 
         } 
         return true; 
      } 

      /// <summary>
      /// 12.检测含有中文字符串的实际长度
      /// </summary>
      /// <param >字符串</param>
      public static int len(string str)
      {
         System.Text.ASCIIEncoding n = new System.Text.ASCIIEncoding();
         byte[] b = n.GetBytes(str);
         int l = 0; // l 为字符串的实际长度
         for (int i=0;i <= b.Length-1;i++)
         {
            if (b[i] ==63) //判断是否为汉字或全脚符号
            {
               l++;
            }
            l++;
         }   
         return l;
      }     }
}

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics