C#数据库类

  1. using Microsoft.VisualBasic;
  2. using System.Data;
  3. using System.Data.SqlClient;
  4. public class SQLBASE
  5. {
  6.    
  7.     public static readonly string sqlConnection = System.Configuration.ConfigurationManager.ConnectionStrings("Ccopconnstr").ConnectionString;
  8.     public static SqlConnection conn = new SqlConnection(SQLBASE.sqlConnection);
  9.    
  10.     public static SqlConnection Db_Conn()
  11.     {
  12.         if ((SQLBASE.conn.State == ConnectionState.Closed)) {
  13.             SQLBASE.conn.Open();
  14.         }
  15.         return SQLBASE.conn;
  16.     }
  17.     public static void close()
  18.     {
  19.         if ((SQLBASE.conn.State != ConnectionState.Closed)) {
  20.             SQLBASE.conn.Close();
  21.         }
  22.     }
  23.     public static void DataBind(GridView MyDataGrid, string CommandTxt)
  24.     {
  25.         try {
  26.             SqlCommand sqlCommd = new SqlCommand(CommandTxt, SQLBASE.conn);
  27.             sqlCommd.CommandType = CommandType.Text;
  28.             SQLBASE.Db_Conn();
  29.             SqlDataReader Dr = sqlCommd.ExecuteReader;
  30.             MyDataGrid.DataSource = Dr;
  31.             MyDataGrid.DataBind();
  32.             Dr.Close();
  33.             sqlCommd.Dispose();
  34.             SQLBASE.close();
  35.         }
  36.         catch (Exception ex) {
  37.             SQLBASE.close();
  38.             throw ex;
  39.         }
  40.     }
  41.     public static void DataBind(DataList MyDataList, string CommandTxt)
  42.     {
  43.         try {
  44.             SqlCommand sqlCommd = new SqlCommand(CommandTxt, SQLBASE.conn);
  45.             sqlCommd.CommandType = CommandType.Text;
  46.             SQLBASE.Db_Conn();
  47.             SqlDataReader Dr = sqlCommd.ExecuteReader;
  48.             MyDataList.DataSource = Dr;
  49.             MyDataList.DataBind();
  50.             Dr.Close();
  51.             sqlCommd.Dispose();
  52.             SQLBASE.close();
  53.         }
  54.         catch (Exception ex) {
  55.             SQLBASE.close();
  56.             throw ex;
  57.         }
  58.     }
  59.    
  60.     public static void DataBind(Repeater MyRepeater, string CommandTxt)
  61.     {
  62.         try {
  63.             SqlCommand sqlCommd = new SqlCommand(CommandTxt, SQLBASE.conn);
  64.             sqlCommd.CommandType = CommandType.Text;
  65.             Db_Conn();
  66.             SqlDataReader Dr = sqlCommd.ExecuteReader;
  67.             MyRepeater.DataSource = Dr;
  68.             MyRepeater.DataBind();
  69.             Dr.Close();
  70.             sqlCommd.Dispose();
  71.             SQLBASE.close();
  72.         }
  73.         catch (Exception ex) {
  74.             SQLBASE.close();
  75.             throw ex;
  76.         }
  77.     }
  78.    
  79.    
  80.     public static void ExecuteNonQuery(string CommandTxt)
  81.     {
  82.         try {
  83.             using (SqlCommand sqlCommd = new SqlCommand(CommandTxt, SQLBASE.conn)) {
  84.                 SQLBASE.Db_Conn();
  85.                 sqlCommd.ExecuteNonQuery();
  86.                 sqlCommd.Dispose();
  87.             }
  88.         }
  89.         catch (Exception ex) {
  90.             throw ex;
  91.         }
  92.         finally {
  93.             SQLBASE.conn.Close();
  94.         }
  95.     }
  96.    
  97.     public static SqlDataReader ExecuteReader(string CommandTxt)
  98.     {
  99.         SqlDataReader Dr = default(SqlDataReader);
  100.         try {
  101.             SqlCommand sqlCommd = new SqlCommand(CommandTxt, SQLBASE.conn);
  102.             sqlCommd.CommandType = CommandType.Text;
  103.             SQLBASE.Db_Conn();
  104.             Dr = sqlCommd.ExecuteReader;
  105.             sqlCommd.Dispose();
  106.         }
  107.         catch (SqlException e) {
  108.             SQLBASE.conn.Close();
  109.             throw new Exception(e.Message);
  110.         }
  111.         return Dr;
  112.     }
  113.    
  114.     public object ExecuteScalar(string CommandTxt)
  115.     {
  116.         object obj = null;
  117.         try {
  118.             SQLBASE.Db_Conn();
  119.             using (SqlCommand command = new SqlCommand(CommandTxt, SQLBASE.conn)) {
  120.                 obj = command.ExecuteScalar;
  121.                 if ((obj == null)) {
  122.                     return "";
  123.                    
  124.                 }
  125.             }
  126.         }
  127.         catch (Exception Ex) {
  128.             SQLBASE.conn.Close();
  129.             throw Ex;
  130.         }
  131.         return obj;
  132.     }
  133.    
  134.     public static void ExecuteSqlTran(ArrayList SQLStringList)
  135.     {
  136.         using (SqlConnection conn = new SqlConnection(SQLBASE.sqlConnection)) {
  137.             conn.Open();
  138.             SqlCommand cmd = new SqlCommand();
  139.             cmd.Connection = conn;
  140.             SqlTransaction tx = conn.BeginTransaction;
  141.             cmd.Transaction = tx;
  142.             try {
  143.                 int n = 0;
  144.                 for (n = 0; n <= SQLStringList.Count - 1; n++) {
  145.                     string strsql = SQLStringList.Item(n).ToString;
  146.                     if ((strsql.Trim.Length > 1)) {
  147.                         cmd.CommandText = strsql;
  148.                         cmd.ExecuteNonQuery();
  149.                     }
  150.                 }
  151.                 tx.Commit();
  152.             }
  153.             catch (SqlException E) {
  154.                 tx.Rollback();
  155.                 throw new Exception(E.Message);
  156.             }
  157.         }
  158.     }
  159.    
  160.     public static DataSet FillDataSet(string sql)
  161.     {
  162.         DataSet ds = new DataSet();
  163.         try {
  164.             Db_Conn();
  165.             SqlCommand sqlCommd = new SqlCommand(sql, conn);
  166.             try {
  167.                 SqlDataAdapter adapter = new SqlDataAdapter(sqlCommd);
  168.                 adapter.Fill(ds);
  169.                 return ds;
  170.             }
  171.             finally {
  172.                 ((IDisposable)sqlCommd).Dispose();
  173.             }
  174.         }
  175.         catch (Exception Ex) {
  176.             throw Ex;
  177.         }
  178.         finally {
  179.             conn.Close();
  180.         }
  181.     }
  182.    
  183.     public static int GetRowsNum(string sqlstr)
  184.     {
  185.         return SQLBASE.FillDataSet(sqlstr).Tables.Item(0).Rows.Count;
  186.     }
  187.    
  188.     public static bool IfRead(string CommandTxt)
  189.     {
  190.         try {
  191.             if ((SQLBASE.GetRowsNum(CommandTxt) == 0)) {
  192.                 return false;
  193.             }
  194.         }
  195.         catch (Exception Ex) {
  196.             throw Ex;
  197.         }
  198.         return true;
  199.     }
  200.    
  201.     public static object StaticExecuteScalar(string CommandTxt)
  202.     {
  203.         try {
  204.             if (conn.State == ConnectionState.Closed) {
  205.                 conn.Open();
  206.             }
  207.             using (SqlCommand command = new SqlCommand(CommandTxt, conn)) {
  208.                 object obj = command.ExecuteScalar();
  209.                 if (obj == null) {
  210.                     return "";
  211.                 }
  212.                 else {
  213.                     return obj;
  214.                 }
  215.             }
  216.         }
  217.         catch (Exception Ex) {
  218.             conn.Close();
  219.             throw Ex;
  220.         }
  221.     }
  222.    
  223.    
  224. }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值