A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

© aisini 金牌黑马   /  2014-8-15 18:19  /  1108 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

  1. using System;
  2. using System.Data;
  3. using System.Data.SqlClient;

  4. namespace DbHelper
  5. {
  6.     public class SqlDbHelper
  7.     {
  8.         private SqlConnection conn;
  9.         private SqlCommand cmd;
  10.         private SqlDataReader reader;
  11.         private SqlDataAdapter adapter;
  12.         private string connectionString = @"server=.;database=student;uid=sa;pwd=scce";

  13.         public string ConnectionString
  14.         {
  15.             get { return this.connectionString; }
  16.             set { this.connectionString = value; }
  17.         }

  18.         /// <summary>
  19.         /// 获取一个未打开连接的SqlConnection对象
  20.         /// </summary>
  21.         /// <returns>SqlConnection对象</returns>
  22.         public SqlConnection GetConnection()
  23.         {
  24.             if (conn != null)
  25.                 return this.conn;
  26.             return this.conn = new SqlConnection(connectionString);
  27.         }

  28.         /// <summary>
  29.         /// 使用连接字符串获取未打开连接SqlConnection对象
  30.         /// </summary>
  31.         /// <param name="_connStr">连接字符串</param>
  32.         /// <returns>SqlConnection对象</returns>
  33.         public SqlConnection GetConnection(string _connStr)
  34.         {
  35.             if (this.conn != null)
  36.                 this.conn.ConnectionString = _connStr;
  37.             else
  38.                 this.conn = new SqlConnection(_connStr);
  39.             return this.conn;
  40.         }

  41.         /// <summary>
  42.         /// 使用指定的Sql语句创建SqlCommand对象
  43.         /// </summary>
  44.         /// <param name="sqlStr">Sql语句</param>
  45.         /// <returns>SqlCommand对象</returns>
  46.         private SqlCommand GetCommand(string sqlStr)
  47.         {
  48.             if (this.conn == null)
  49.                 this.conn = GetConnection();
  50.             if (this.cmd == null)
  51.                 this.cmd = this.GetCommand(sqlStr, CommandType.Text, null);
  52.             else
  53.             {
  54.                 this.cmd.CommandType = CommandType.Text;
  55.                 this.cmd.Parameters.Clear();
  56.             }
  57.             this.cmd.CommandText = sqlStr;
  58.             return this.cmd;
  59.         }

  60.         /// <summary>
  61.         /// 使用指定的Sql语句,CommandType,SqlParameter数组创建SqlCommand对象
  62.         /// </summary>
  63.         /// <param name="sqlStr">Sql语句</param>
  64.         /// <param name="type">命令类型</param>
  65.         /// <param name="paras">SqlParameter数组</param>
  66.         /// <returns>SqlCommand对象</returns>
  67.         public SqlCommand GetCommand(string sqlStr, CommandType type, SqlParameter[] paras)
  68.         {
  69.             if (conn == null)
  70.                 this.conn = this.GetConnection();
  71.             if (cmd == null)
  72.                 this.cmd = conn.CreateCommand();
  73.             this.cmd.CommandType = type;
  74.             this.cmd.CommandText = sqlStr;
  75.             this.cmd.Parameters.Clear();
  76.             if (paras != null)
  77.                 this.cmd.Parameters.AddRange(paras);
  78.             return this.cmd;
  79.         }

  80.         /// <summary>
  81.         /// 执行Sql语句返回受影响的行数
  82.         /// </summary>
  83.         /// <param name="sqlStr">Sql语句</param>
  84.         /// <returns>受影响的行数,失败则返回-1</returns>
  85.         public int ExecuteNoQuery(string sqlStr)
  86.         {
  87.             int line = -1;
  88.             CheckArgs(sqlStr);
  89.             try { OpenConn(); line = this.ExecuteNonQuery(sqlStr,CommandType.Text,null); }
  90.             catch (SqlException e) { throw e; }
  91.             return line;
  92.         }

  93.         /// <summary>
  94.         /// 使用指定的Sql语句,CommandType,SqlParameter数组执行Sql语句,返回受影响的行数
  95.         /// </summary>
  96.         /// <param name="sqlStr">Sql语句</param>
  97.         /// <param name="type">命令类型</param>
  98.         /// <param name="paras">SqlParameter数组</param>
  99.         /// <returns>受影响的行数</returns>
  100.         public int ExecuteNonQuery(string sqlStr, CommandType type, SqlParameter[] paras)
  101.         {
  102.             int line = -1;
  103.             CheckArgs(sqlStr);
  104.             if (this.cmd == null)
  105.                 GetCommand(sqlStr, type, paras);
  106.             this.cmd.Parameters.Clear();
  107.             this.cmd.CommandText = sqlStr;
  108.             this.cmd.CommandType = type;
  109.             if(paras != null)
  110.                 this.cmd.Parameters.AddRange(paras);
  111.             try { OpenConn(); line = this.cmd.ExecuteNonQuery(); }
  112.             catch (SqlException e) { throw e; }
  113.             return line;
  114.         }

  115.         /// <summary>
  116.         /// 使用指定Sql语句获取dataTable
  117.         /// </summary>
  118.         /// <param name="sqlStr">Sql语句</param>
  119.         /// <returns>DataTable对象</returns>
  120.         public DataTable GetDataTable(string sqlStr)
  121.         {
  122.             CheckArgs(sqlStr);
  123.             if (this.conn == null)
  124.                 this.conn = GetConnection();
  125.             this.adapter = new SqlDataAdapter(sqlStr, this.conn);
  126.             DataTable table = new DataTable();
  127.             try { adapter.Fill(table); }
  128.             catch (SqlException e) { throw e; }
  129.             return table;
  130.         }

  131.         /// <summary>
  132.         /// 使用指定的Sql语句获取SqlDataReader
  133.         /// </summary>
  134.         /// <param name="sqlStr">sql语句</param>
  135.         /// <returns>SqlDataReader对象</returns>
  136.         public SqlDataReader GetSqlDataReader(string sqlStr)
  137.         {
  138.             CheckArgs(sqlStr);
  139.             if (cmd == null)
  140.                 GetCommand(sqlStr);
  141.             if(reader != null)
  142.                 reader.Dispose();
  143.             try { OpenConn(); this.reader = this.cmd.ExecuteReader(); }
  144.             catch (SqlException e) { throw e; }
  145.             return this.reader;
  146.         }

  147.         /// <summary>
  148.         /// 使用事务执行多条Sql语句
  149.         /// </summary>
  150.         /// <param name="sqlCommands">sql语句数组</param>
  151.         /// <returns>全部成功则返回true否则返回false</returns>
  152.         public bool ExecuteSqls(string[] sqlCommands)
  153.         {
  154.             if (sqlCommands == null)
  155.                 throw new ArgumentNullException();
  156.             if(this.cmd == null)
  157.                 GetCommand(null);
  158.             SqlTransaction tran = null;
  159.             try
  160.             {
  161.                 OpenConn();
  162.                 tran = this.conn.BeginTransaction();
  163.                 this.cmd.Transaction = tran;
  164.                 foreach (string sql in sqlCommands)
  165.                 {
  166.                     if (ExecuteNoQuery(sql) == 0)
  167.                     { tran.Rollback(); return false; }
  168.                 }
  169.             }
  170.             catch (SqlException e)
  171.             {
  172.                 if(tran != null)
  173.                     tran.Rollback();
  174.                 throw e;
  175.             }
  176.             tran.Commit();
  177.             return true;
  178.         }

  179.         private void OpenConn()
  180.         {
  181.             try
  182.             {
  183.                 if (this.conn.State == ConnectionState.Closed)
  184.                     conn.Open();
  185.             }
  186.             catch (SqlException e) { throw e; }
  187.         }

  188.         private void CheckArgs(string sqlStr)
  189.         {
  190.             if (sqlStr == null)
  191.                 throw new ArgumentNullException();
  192.             if (sqlStr.Length == 0)
  193.                 throw new ArgumentOutOfRangeException();
  194.         }

  195.     }
  196. }
复制代码


0 个回复

您需要登录后才可以回帖 登录 | 加入黑马