[C#] C#版本的DBHelper →→→→→进入此内容的聊天室

来自 , 2020-10-13, 写在 C#, 查看 110 次.
URL http://www.code666.cn/view/4e2ecebb
  1. /********************************
  2.  * Produce: DbHelper
  3.  * Version: beta 2
  4.  * Date: 2012.10.11
  5.  ********************************/
  6.  
  7. using System;
  8. using System.Data;
  9. using System.Data.SqlClient;
  10. using System.Configuration;
  11. using System.Collections.Generic;
  12.  
  13. namespace DbHelper
  14. {
  15.     public class SqlDbHelper :IDisposable
  16.     {
  17.         protected SqlConnection conn;
  18.         protected SqlCommand cmd;
  19.         protected SqlDataReader reader;
  20.         protected SqlDataAdapter adapter;
  21.         protected string connectionString = ConfigurationManager.ConnectionStrings["connStr"].ConnectionString;
  22.         public static SqlDbHelper Instance = null;
  23.  
  24.         public string ConnectionString
  25.         {
  26.             get { return this.connectionString; }
  27.             set { this.connectionString = value; }
  28.         }
  29.  
  30.         static SqlDbHelper()
  31.         { SqlDbHelper.Instance = new SqlDbHelper(); }
  32.  
  33.         /// <summary>
  34.         /// 获取一个未打开连接的SqlConnection对象
  35.         /// </summary>
  36.         /// <returns>SqlConnection对象</returns>
  37.         public SqlConnection GetConnection()
  38.         {
  39.             if (conn != null)
  40.                 return this.conn;
  41.             return this.conn = new SqlConnection(connectionString);
  42.         }
  43.  
  44.         /// <summary>
  45.         /// 使用连接字符串获取未打开连接SqlConnection对象
  46.         /// </summary>
  47.         /// <param name="_connStr">连接字符串</param>
  48.         /// <returns>SqlConnection对象</returns>
  49.         public SqlConnection GetConnection(string _connStr)
  50.         {
  51.             if (this.conn != null)
  52.                 this.conn.ConnectionString = _connStr;
  53.             else
  54.                 this.conn = new SqlConnection(_connStr);
  55.             return this.conn;
  56.         }
  57.  
  58.         /// <summary>
  59.         /// 使用指定的Sql语句创建SqlCommand对象
  60.         /// </summary>
  61.         /// <param name="sqlStr">Sql语句</param>
  62.         /// <returns>SqlCommand对象</returns>
  63.         private SqlCommand GetCommand(string sqlStr)
  64.         {
  65.             if (this.conn == null)
  66.                 this.conn = GetConnection();
  67.             if (this.cmd == null)
  68.                 this.cmd = this.GetCommand(sqlStr, CommandType.Text, null);
  69.             else
  70.             {
  71.                                 this.cmd.CommandText = sqlStr;
  72.                 this.cmd.CommandType = CommandType.Text;
  73.                 this.cmd.Parameters.Clear();
  74.             }
  75.             return this.cmd;
  76.         }
  77.  
  78.         /// <summary>
  79.         /// 使用指定的Sql语句,CommandType,SqlParameter数组创建SqlCommand对象
  80.         /// </summary>
  81.         /// <param name="sqlStr">Sql语句</param>
  82.         /// <param name="type">命令类型</param>
  83.         /// <param name="paras">SqlParameter数组</param>
  84.         /// <returns>SqlCommand对象</returns>
  85.         public SqlCommand GetCommand(string sqlStr, CommandType type, SqlParameter[] paras)
  86.         {
  87.             if (conn == null)
  88.                 this.conn = this.GetConnection();
  89.             if (cmd == null)
  90.                 this.cmd = conn.CreateCommand();
  91.             this.cmd.CommandType = type;
  92.             this.cmd.CommandText = sqlStr;
  93.             this.cmd.Parameters.Clear();
  94.             if (paras != null)
  95.                 this.cmd.Parameters.AddRange(paras);
  96.             return this.cmd;
  97.         }
  98.  
  99.         /// <summary>
  100.         /// 执行Sql语句返回受影响的行数
  101.         /// </summary>
  102.         /// <param name="sqlStr">Sql语句</param>
  103.         /// <returns>受影响的行数,失败则返回-1</returns>
  104.         public int ExecuteNonQuery(string sqlStr)
  105.         {
  106.             int line = -1;
  107.             try { line = this.ExecuteNonQuery(sqlStr,CommandType.Text,null); }
  108.             catch (SqlException e) { throw e; }
  109.             return line;
  110.         }
  111.  
  112.         /// <summary>
  113.         /// 使用指定的Sql语句,CommandType,SqlParameter数组执行Sql语句,返回受影响的行数
  114.         /// </summary>
  115.         /// <param name="sqlStr">Sql语句</param>
  116.         /// <param name="type">命令类型</param>
  117.         /// <param name="paras">SqlParameter数组</param>
  118.         /// <returns>受影响的行数</returns>
  119.         public int ExecuteNonQuery(string sqlStr, CommandType type, SqlParameter[] paras)
  120.         {
  121.             int line = -1;
  122.             CheckArgs(sqlStr);
  123.             if (this.cmd == null)
  124.                 GetCommand(sqlStr, type, paras);
  125.             this.cmd.Parameters.Clear();
  126.             this.cmd.CommandText = sqlStr;
  127.             this.cmd.CommandType = type;
  128.             if(paras != null)
  129.                 this.cmd.Parameters.AddRange(paras);
  130.             try { OpenConn(); line = this.cmd.ExecuteNonQuery(); }
  131.             catch (SqlException e) { throw e; }
  132.             return line;
  133.         }
  134.  
  135.         /// <summary>
  136.         /// 使用指定Sql语句获取dataTable
  137.         /// </summary>
  138.         /// <param name="sqlStr">Sql语句</param>
  139.         /// <returns>DataTable对象</returns>
  140.         public DataTable GetDataTable(string sqlStr)
  141.         {
  142.             CheckArgs(sqlStr);
  143.             if (this.conn == null)
  144.                 this.conn = GetConnection();
  145.             this.adapter = new SqlDataAdapter(sqlStr, this.conn);
  146.             DataTable table = new DataTable();
  147.             try { adapter.Fill(table); }
  148.             catch (SqlException e) { throw e; }
  149.             finally { this.adapter.Dispose(); }
  150.             return table;
  151.         }
  152.  
  153.         /// <summary>
  154.         /// 使用指定的Sql语句获取SqlDataReader
  155.         /// </summary>
  156.         /// <param name="sqlStr">sql语句</param>
  157.         /// <returns>SqlDataReader对象</returns>
  158.         public SqlDataReader GetSqlDataReader(string sqlStr)
  159.         {
  160.             CheckArgs(sqlStr);
  161.             if (cmd == null)
  162.                 GetCommand(sqlStr);
  163.             if(reader != null)
  164.                 reader.Dispose();
  165.             try { OpenConn(); this.reader = this.cmd.ExecuteReader(); }
  166.             catch (SqlException e) { throw e; }
  167.             return this.reader;
  168.         }
  169.  
  170.         /// <summary>
  171.         /// 使用事务执行多条Sql语句
  172.         /// </summary>
  173.         /// <param name="sqlCommands">sql语句数组</param>
  174.         /// <returns>全部成功则返回true否则返回false</returns>
  175.         public bool ExecuteSqls(List<string> sqlCommands)
  176.         {
  177.             if (sqlCommands == null)
  178.                 throw new ArgumentNullException();
  179.             if (sqlCommands.Count == 0)
  180.                 throw new ArgumentOutOfRangeException();
  181.             if(this.cmd == null)
  182.                 GetCommand(null);
  183.             SqlTransaction tran = null;
  184.             try {
  185.                 OpenConn();
  186.                 tran = this.conn.BeginTransaction();
  187.                 this.cmd.Transaction = tran;
  188.                 foreach (string sql in sqlCommands)
  189.                 {
  190.                     if (ExecuteNonQuery(sql) == 0)
  191.                     { tran.Rollback(); return false; }
  192.                 }
  193.             }
  194.             catch { if (tran != null) tran.Rollback(); throw; }
  195.             tran.Commit();
  196.             return true;
  197.         }
  198.  
  199.         public virtual void Dispose()
  200.         {
  201.             if (this.reader != null)
  202.             { reader.Dispose(); this.reader = null; }
  203.             if (this.cmd != null)
  204.             { this.cmd.Dispose(); this.cmd = null; }
  205.             if (this.conn != null)
  206.             { this.conn.Dispose(); conn = null; }
  207.         }
  208.  
  209.         protected void OpenConn()
  210.         {
  211.             try {
  212.                 if (this.conn.State != ConnectionState.Open)
  213.                     conn.Open();
  214.             }
  215.             catch (SqlException e) { throw e; }
  216.         }
  217.  
  218.         /// <summary>
  219.         /// 关闭连接
  220.         /// </summary>
  221.         public void CloseConn()
  222.         {
  223.             if (this.conn != null && this.conn.State == ConnectionState.Open)
  224.                 this.conn.Close();
  225.         }
  226.  
  227.         /// <summary>
  228.         /// 检查Sql语句是否合法
  229.         /// </summary>
  230.         /// <param name="sqlStr">Sql语句</param>
  231.         protected virtual void CheckArgs(string sqlStr)
  232.         {
  233.             if (sqlStr == null)
  234.                 throw new ArgumentNullException();
  235.             if (sqlStr.Length == 0)
  236.                 throw new ArgumentOutOfRangeException();
  237.         }
  238.  
  239.     }
  240. }
  241. //csharp/5423

回复 "C#版本的DBHelper"

这儿你可以回复上面这条便签

captcha