zoukankan      html  css  js  c++  java
  • SQLServer (1)封装SqlHelper【包含事务】

    使用方式

    SqlHelper SqlHelp = new SqlHelper();
     SqlHelp.Open();
    //开启事务
     SqlHelp.TranBegin();
    //执行各种SQL语句
    
    //提交事务
      SqlHelp.TranCommit();
    //回滚事务
    SqlHelp.TranRollBack();
    
     SqlHelp.Close();
       1 public class SqlHelper
       2     {
       3         public SqlHelper()
       4         {
       5 
       6         }
       7         /// <summary>
       8         /// 数据库连接串
       9         /// </summary>
      10         public static string connStr = ConfigurationManager.AppSettings["DBConStr"];
      11         /// <summary>
      12         /// 声明sqlConn的连接
      13         /// </summary>
      14         public SqlConnection sqlConn = null;
      15         /// <summary>
      16         /// 声明sqlTrans事务
      17         /// </summary>
      18         public SqlTransaction sqlTrans = null;
      19         /// <summary>
      20         /// 声明sqlCmd命令
      21         /// </summary>
      22         public SqlCommand sqlCmd = null;
      23 
      24         /// <summary>
      25         /// 打开连接
      26         /// </summary>
      27         public void Open()
      28         {
      29             if (sqlConn == null)
      30             {
      31                 sqlConn = new SqlConnection(connStr);
      32             }
      33             if (sqlConn.State != ConnectionState.Open)
      34             {
      35                 sqlConn.Open();
      36             }
      37         }
      38 
      39         /// <summary>
      40         /// 打开其他的数据库
      41         /// 重写
      42         /// </summary>
      43         /// <param name="conStr"></param>
      44         public void Open(string conStr)
      45         {
      46             if (sqlConn == null)
      47             {
      48                 sqlConn = new SqlConnection(conStr);
      49             }
      50             if (sqlConn.State != ConnectionState.Open)
      51             {
      52                 sqlConn.Open();
      53             }
      54         }
      55         /// <summary>
      56         /// 关闭连接
      57         /// </summary>
      58         public void Close()
      59         {
      60             if (sqlConn != null && sqlConn.State == ConnectionState.Open)
      61             {
      62                 sqlConn.Close();
      63             }
      64         }
      65         /// <summary>
      66         /// 开始事务
      67         /// </summary>
      68         public void TranBegin()
      69         {
      70             sqlCmd = new SqlCommand();
      71             sqlCmd.Connection = sqlConn;
      72             sqlTrans = sqlConn.BeginTransaction();
      73             sqlCmd.CommandTimeout = 120;
      74             sqlCmd.Transaction = sqlTrans;
      75         }
      76         /// <summary>
      77         /// 提交事务
      78         /// </summary>
      79         public void TranCommit()
      80         {
      81             sqlTrans.Commit();
      82         }
      83         /// <summary>
      84         /// 回滚事务
      85         /// </summary>
      86         public void TranRollBack()
      87         {
      88             if (sqlTrans != null && sqlTrans.Connection != null)
      89             {
      90                 sqlTrans.Rollback();
      91             }
      92         }
      93 
      94         #region private utility methods & constructors
      95 
      96         /// <summary>
      97         /// This method is used to attach array of SqlParameters to a SqlCommand.
      98         /// This method will assign a value of DbNull to any parameter with a direction of
      99         /// InputOutput and a value of null.
     100         /// This behavior will prevent default values from being used, but
     101         /// this will be the less common case than an intended pure output parameter (derived as InputOutput)
     102         /// where the user provided no input value.
     103         /// </summary>
     104         /// <param name="command">The command to which the parameters will be added</param>
     105         /// <param name="commandParameters">an array of SqlParameters tho be added to command</param>
     106         private void AttachParameters(SqlCommand command, SqlParameter[] commandParameters)
     107         {
     108             foreach (SqlParameter p in commandParameters)
     109             {
     110                 //check for derived output value with no value assigned
     111                 if ((p.Direction == ParameterDirection.InputOutput) && (p.Value == null))
     112                 {
     113                     p.Value = DBNull.Value;
     114                 }
     115 
     116                 command.Parameters.Add(p);
     117             }
     118         }
     119 
     120         /// <summary>
     121         /// This method assigns an array of values to an array of SqlParameters.
     122         /// </summary>
     123         /// <param name="commandParameters">array of SqlParameters to be assigned values</param>
     124         /// <param name="parameterValues">array of Components holding the values to be assigned</param>
     125         private void AssignParameterValues(SqlParameter[] commandParameters, object[] parameterValues)
     126         {
     127             if ((commandParameters == null) || (parameterValues == null))
     128             {
     129                 //do nothing if we get no data
     130                 return;
     131             }
     132 
     133             // we must have the same number of values as we pave parameters to put them in
     134             if (commandParameters.Length != parameterValues.Length)
     135             {
     136                 throw new ArgumentException("Parameter count does not match Parameter Value count.");
     137             }
     138 
     139             //iterate through the SqlParameters, assigning the values from the corresponding position in the 
     140             //value array
     141             for (int i = 0, j = commandParameters.Length; i < j; i++)
     142             {
     143                 commandParameters[i].Value = parameterValues[i];
     144             }
     145         }
     146 
     147         /// <summary>
     148         /// This method opens (if necessary) and assigns a connection, transaction, command type and parameters 
     149         /// to the provided command.
     150         /// </summary>
     151         /// <param name="command">the SqlCommand to be prepared</param>
     152         /// <param name="connection">a valid SqlConnection, on which to execute this command</param>
     153         /// <param name="transaction">a valid SqlTransaction, or 'null'</param>
     154         /// <param name="commandType">the CommandType (stored procedure, text, etc.)</param>
     155         /// <param name="commandText">the stored procedure name or T-SQL command</param>
     156         /// <param name="commandParameters">an array of SqlParameters to be associated with the command or 'null' if no parameters are required</param>
     157         private void PrepareCommand(SqlCommand command, SqlConnection connection, SqlTransaction transaction, CommandType commandType, string commandText, SqlParameter[] commandParameters)
     158         {
     159             //if the provided connection is not open, we will open it
     160             if (connection.State != ConnectionState.Open)
     161             {
     162                 connection.Open();
     163             }
     164 
     165             //associate the connection with the command
     166             command.Connection = connection;
     167             command.CommandTimeout = 300;
     168 
     169             //set the command text (stored procedure name or SQL statement)
     170             command.CommandText = commandText;
     171 
     172             //if we were provided a transaction, assign it.
     173             if (transaction != null)
     174             {
     175                 command.Transaction = transaction;
     176             }
     177 
     178             //set the command type
     179             command.CommandType = commandType;
     180 
     181             //attach the command parameters if they are provided
     182             if (commandParameters != null)
     183             {
     184                 AttachParameters(command, commandParameters);
     185             }
     186 
     187             return;
     188         }
     189 
     190         /// <summary>
     191         /// ExecuteNonQuery方法专用
     192         /// 可以把方法都放到一个事务中
     193         /// This method opens (if necessary) and assigns a connection, transaction, command type and parameters 
     194         /// to the provided command.
     195         /// </summary>
     196         /// <param name="command">the SqlCommand to be prepared</param>
     197         /// <param name="connection">a valid SqlConnection, on which to execute this command</param>
     198         /// <param name="transaction">a valid SqlTransaction, or 'null'</param>
     199         /// <param name="commandType">the CommandType (stored procedure, text, etc.)</param>
     200         /// <param name="commandText">the stored procedure name or T-SQL command</param>
     201         /// <param name="commandParameters">an array of SqlParameters to be associated with the command or 'null' if no parameters are required</param>
     202         private void PrepareCommand(CommandType commandType, string commandText, SqlParameter[] commandParameters)
     203         {
     204             //if the provided connection is not open, we will open it
     205 
     206             if (sqlConn.State != ConnectionState.Open)
     207             {
     208                 sqlConn.Open();
     209             }
     210 
     211             //set the command text (stored procedure name or SQL statement)
     212             sqlCmd.CommandText = commandText;
     213 
     214             //set the command type
     215             sqlCmd.CommandType = commandType;
     216 
     217             //attach the command parameters if they are provided
     218             if (commandParameters != null)
     219             {
     220                 AttachParameters(sqlCmd, commandParameters);
     221             }
     222 
     223             return;
     224         }
     225 
     226 
     227         #endregion private utility methods & constructors
     228 
     229         #region DataHelpers
     230 
     231         public string CheckNull(object obj)
     232         {
     233             return (string)obj;
     234         }
     235 
     236         public string CheckNull(DBNull obj)
     237         {
     238             return null;
     239         }
     240 
     241         #endregion
     242 
     243         #region AddParameters
     244 
     245         public object CheckForNullString(string text)
     246         {
     247             if (text == null || text.Trim().Length == 0)
     248             {
     249                 return DBNull.Value;
     250             }
     251             else
     252             {
     253                 return text;
     254             }
     255         }
     256 
     257         public SqlParameter MakeInParam(string ParamName, object Value)
     258         {
     259             return new SqlParameter(ParamName, Value);
     260         }
     261 
     262         /// <summary>
     263         /// Make input param.
     264         /// </summary>
     265         /// <param name="ParamName">Name of param.</param>
     266         /// <param name="DbType">Param type.</param>
     267         /// <param name="Size">Param size.</param>
     268         /// <param name="Value">Param value.</param>
     269         /// <returns>New parameter.</returns>
     270         public SqlParameter MakeInParam(string ParamName, SqlDbType DbType, Int32 Size, object Value)
     271         {
     272             return MakeParam(ParamName, DbType, Size, ParameterDirection.Input, Value);
     273         }
     274 
     275         /// <summary>
     276         /// Make input param.
     277         /// </summary>
     278         /// <param name="ParamName">Name of param.</param>
     279         /// <param name="DbType">Param type.</param>
     280         /// <param name="Size">Param size.</param>
     281         /// <returns>New parameter.</returns>
     282         public SqlParameter MakeOutParam(string ParamName, SqlDbType DbType, int Size)
     283         {
     284             return MakeParam(ParamName, DbType, Size, ParameterDirection.Output, null);
     285         }
     286 
     287         /// <summary>
     288         /// Make stored procedure param.
     289         /// </summary>
     290         /// <param name="ParamName">Name of param.</param>
     291         /// <param name="DbType">Param type.</param>
     292         /// <param name="Size">Param size.</param>
     293         /// <param name="Direction">Parm direction.</param>
     294         /// <param name="Value">Param value.</param>
     295         /// <returns>New parameter.</returns>
     296         public SqlParameter MakeParam(string ParamName, SqlDbType DbType, int Size, ParameterDirection Direction, object Value)
     297         {
     298             SqlParameter param;
     299 
     300             if (Size > 0)
     301                 param = new SqlParameter(ParamName, DbType, Size);
     302             else
     303                 param = new SqlParameter(ParamName, DbType);
     304 
     305             param.Direction = Direction;
     306             if (!(Direction == ParameterDirection.Output && Value == null))
     307                 param.Value = Value;
     308 
     309             return param;
     310         }
     311 
     312         /// <summary>
     313         /// 把Hashtable转化为SqlParameter参数组
     314         /// </summary>
     315         /// <param name="ParamList">Hashtable</param>
     316         /// <returns></returns>
     317         public SqlParameter[] MakeAllParam(Hashtable ParamList)
     318         {
     319             SqlParameter[] p1 = new SqlParameter[ParamList.Count];
     320             int i = 0;
     321             System.Collections.IDictionaryEnumerator CacheEnum = ParamList.GetEnumerator();
     322 
     323             while (CacheEnum.MoveNext())
     324             {
     325                 p1[i] = MakeInParam(CacheEnum.Key.ToString(), CacheEnum.Value);
     326                 i++;
     327             }
     328             return p1;
     329         }
     330 
     331 
     332         #endregion
     333 
     334         #region ExecuteNonQuery
     335 
     336         /// <summary>
     337         /// Execute a SqlCommand (that returns no resultset) against the specified SqlTransaction
     338         /// using the provided parameters.
     339         /// </summary>
     340         /// <remarks>
     341         /// e.g.:  
     342         ///  int result = ExecuteNonQuery(trans, CommandType.StoredProcedure, "GetOrders", new SqlParameter("@prodid", 24));
     343         /// </remarks>
     344         /// <param name="transaction">a valid SqlTransaction</param>
     345         /// <param name="commandType">the CommandType (stored procedure, text, etc.)</param>
     346         /// <param name="commandText">the stored procedure name or T-SQL command</param>
     347         /// <param name="commandParameters">an array of SqlParamters used to execute the command</param>
     348         /// <returns>an int representing the number of rows affected by the command</returns>
     349         public int ExecuteNonQuery(CommandType commandType, string commandText, params SqlParameter[] commandParameters)
     350         {
     351             //create a command and prepare it for execution
     352             PrepareCommand(commandType, commandText, commandParameters);
     353 
     354             //finally, execute the command.
     355             int retval = sqlCmd.ExecuteNonQuery();
     356 
     357             // detach the SqlParameters from the command object, so they can be used again.
     358             sqlCmd.Parameters.Clear();
     359 
     360             return retval;
     361         }
     362 
     363         /// <summary>
     364         /// 执行一个简单的Sqlcommand 没有返回结果
     365         /// </summary>
     366         ///  例如:  
     367         ///  int result = ExecuteNonQuery("delete from test where 1>2 ");  返回 result =0
     368         /// <param name="commandText">只能是sql语句</param>
     369         /// <returns>受影响的行数</returns>
     370         public int ExecuteNonQuery(string commandText)
     371         {
     372             return ExecuteNonQuery(CommandType.Text, commandText, (SqlParameter[])null);
     373         }
     374 
     375         /// <summary>
     376         /// 执行一个简单的Sqlcommand 没有返回结果
     377         /// </summary>
     378         /// 例如:    
     379         ///  int result = ExecuteNonQuery("delete from test where  tt =@tt", new SqlParameter("@tt", 24));
     380         /// <param name="commandText">只能是sql语句</param>
     381         /// <param name="commandParameters">参数</param>
     382         /// <returns>受影响的行数</returns>
     383         public int ExecuteNonQuery(string commandText, params SqlParameter[] commandParameters)
     384         {
     385             return ExecuteNonQuery(CommandType.Text, commandText, commandParameters);
     386         }
     387         /// <summary>
     388         /// 执行一个简单的Sqlcommand 没有返回结果
     389         /// 参数是Hashtable
     390         /// </summary>
     391         /// <param name="commandText">只能是sql语句</param>
     392         /// <param name="commandParameters">Hashtable参数</param>
     393         /// <returns>受影响的行数</returns>
     394         public int ExecuteNonQuery(string commandText, Hashtable commandParameters)
     395         {
     396             return ExecuteNonQuery(CommandType.Text, commandText, MakeAllParam(commandParameters));
     397         }
     398 
     399         #endregion ExecuteNonQuery
     400 
     401         #region SqlDataAdapter
     402 
     403         public SqlDataAdapter ExecuteSqlDataAdapter(string commandText, params SqlParameter[] commandParameters)
     404         {
     405             SqlConnection cn = new SqlConnection();
     406             cn.ConnectionString = connStr;
     407 
     408             cn.Open();
     409             SqlDataAdapter myda = new SqlDataAdapter(commandText, cn);
     410             foreach (SqlParameter p in commandParameters)
     411             {
     412                 //check for derived output value with no value assigned
     413                 if ((p.Direction == ParameterDirection.InputOutput) && (p.Value == null))
     414                 {
     415                     p.Value = DBNull.Value;
     416                 }
     417                 myda.SelectCommand.Parameters.Add(p);
     418             }
     419             cn.Close();
     420             return myda;
     421         }
     422 
     423         /// <summary>
     424         /// 构建DataAdapter的方法,
     425         /// </summary>
     426         /// <param name="commandText"></param>
     427         /// <param name="paramsTable"></param>
     428         /// <returns></returns>
     429         public SqlDataAdapter ExecuteSqlDataAdapter(string commandText, Hashtable paramsTable)
     430         {
     431             return ExecuteSqlDataAdapter(commandText, MakeAllParam(paramsTable));
     432         }
     433         /// <summary>
     434         /// 构建DataAdapter的方法,
     435         /// </summary>
     436         /// <param name="commandText">简单sql语句</param>
     437         /// <returns>返回值</returns>
     438         public SqlDataAdapter ExecuteSqlDataAdapter(string commandText)
     439         {
     440             SqlConnection cn = new SqlConnection();
     441             cn.ConnectionString = connStr;
     442             cn.Open();
     443             SqlDataAdapter myda = new SqlDataAdapter(commandText, cn);
     444             cn.Close();
     445 
     446             return myda;
     447         }
     448 
     449         #endregion
     450 
     451         #region ExecuteDataSet
     452 
     453         /// <summary>
     454         /// Execute a SqlCommand (that returns a resultset and takes no parameters) against the database specified in 
     455         /// the connection string. 
     456         /// </summary>
     457         /// <remarks>
     458         /// e.g.:  
     459         ///  DataSet ds = ExecuteDataset(connString, CommandType.StoredProcedure, "GetOrders");
     460         /// </remarks>
     461         /// <param name="connectionString">a valid connection string for a SqlConnection</param>
     462         /// <param name="commandType">the CommandType (stored procedure, text, etc.)</param>
     463         /// <param name="commandText">the stored procedure name or T-SQL command</param>
     464         /// <returns>a dataset containing the resultset generated by the command</returns>
     465         public DataSet ExecuteDataset(string connectionString, CommandType commandType, string commandText)
     466         {
     467             //pass through the call providing null for the set of SqlParameters
     468             return ExecuteDataset(connectionString, commandType, commandText, (SqlParameter[])null);
     469         }
     470 
     471         /// <summary>
     472         /// Execute a SqlCommand (that returns a resultset) against the database specified in the connection string 
     473         /// using the provided parameters.
     474         /// </summary>
     475         /// <remarks>
     476         /// e.g.:  
     477         ///  DataSet ds = ExecuteDataset(connString, CommandType.StoredProcedure, "GetOrders", new SqlParameter("@prodid", 24));
     478         /// </remarks>
     479         /// <param name="connectionString">a valid connection string for a SqlConnection</param>
     480         /// <param name="commandType">the CommandType (stored procedure, text, etc.)</param>
     481         /// <param name="commandText">the stored procedure name or T-SQL command</param>
     482         /// <param name="commandParameters">an array of SqlParamters used to execute the command</param>
     483         /// <returns>a dataset containing the resultset generated by the command</returns>
     484         public DataSet ExecuteDataset(string connectionString, CommandType commandType, string commandText, params SqlParameter[] commandParameters)
     485         {
     486             //create & open a SqlConnection, and dispose of it after we are done.
     487             using (SqlConnection cn = new SqlConnection(connectionString))
     488             {
     489                 cn.Open();
     490 
     491                 //call the overload that takes a connection in place of the connection string
     492                 return ExecuteDataset(cn, commandType, commandText, commandParameters);
     493             }
     494         }
     495 
     496         /// <summary>
     497         /// Execute a SqlCommand (that returns a resultset and takes no parameters) against the provided SqlConnection. 
     498         /// </summary>
     499         /// <remarks>
     500         /// e.g.:  
     501         ///  DataSet ds = ExecuteDataset(conn, CommandType.StoredProcedure, "GetOrders");
     502         /// </remarks>
     503         /// <param name="connection">a valid SqlConnection</param>
     504         /// <param name="commandType">the CommandType (stored procedure, text, etc.)</param>
     505         /// <param name="commandText">the stored procedure name or T-SQL command</param>
     506         /// <returns>a dataset containing the resultset generated by the command</returns>
     507         public DataSet ExecuteDataset(SqlConnection connection, CommandType commandType, string commandText)
     508         {
     509             //pass through the call providing null for the set of SqlParameters
     510             return ExecuteDataset(connection, commandType, commandText, (SqlParameter[])null);
     511         }
     512 
     513         /// <summary>
     514         /// Execute a SqlCommand (that returns a resultset) against the specified SqlConnection 
     515         /// using the provided parameters.
     516         /// </summary>
     517         /// <remarks>
     518         /// e.g.:  
     519         ///  DataSet ds = ExecuteDataset(conn, CommandType.StoredProcedure, "GetOrders", new SqlParameter("@prodid", 24));
     520         /// </remarks>
     521         /// <param name="connection">a valid SqlConnection</param>
     522         /// <param name="commandType">the CommandType (stored procedure, text, etc.)</param>
     523         /// <param name="commandText">the stored procedure name or T-SQL command</param>
     524         /// <param name="commandParameters">an array of SqlParamters used to execute the command</param>
     525         /// <returns>a dataset containing the resultset generated by the command</returns>
     526         public DataSet ExecuteDataset(SqlConnection connection, CommandType commandType, string commandText, params SqlParameter[] commandParameters)
     527         {
     528             //SqlNotice st = new SqlNotice();
     529             //create a command and prepare it for execution
     530             SqlCommand cmd = new SqlCommand();
     531             PrepareCommand(cmd, connection, (SqlTransaction)null, commandType, commandText, commandParameters);
     532 
     533             //create the DataAdapter & DataSet
     534             SqlDataAdapter da = new SqlDataAdapter(cmd);
     535             DataSet ds = new DataSet();
     536 
     537             //fill the DataSet using default values for DataTable names, etc.
     538             da.Fill(ds);
     539 
     540             // detach the SqlParameters from the command object, so they can be used again.            
     541             cmd.Parameters.Clear();
     542 
     543             //st.End("ExecuteDataset_1", commandText);
     544             //return the dataset
     545             return ds;
     546         }
     547 
     548 
     549         /// <summary>
     550         /// Execute a SqlCommand (that returns a resultset and takes no parameters) against the provided SqlTransaction. 
     551         /// </summary>
     552         /// <remarks>
     553         /// e.g.:  
     554         ///  DataSet ds = ExecuteDataset(trans, CommandType.StoredProcedure, "GetOrders");
     555         /// </remarks>
     556         /// <param name="transaction">a valid SqlTransaction</param>
     557         /// <param name="commandType">the CommandType (stored procedure, text, etc.)</param>
     558         /// <param name="commandText">the stored procedure name or T-SQL command</param>
     559         /// <returns>a dataset containing the resultset generated by the command</returns>
     560         public DataSet ExecuteDataset(SqlTransaction transaction, CommandType commandType, string commandText)
     561         {
     562             //pass through the call providing null for the set of SqlParameters
     563             return ExecuteDataset(transaction, commandType, commandText, (SqlParameter[])null);
     564         }
     565 
     566         /// <summary>
     567         /// Execute a SqlCommand (that returns a resultset) against the specified SqlTransaction
     568         /// using the provided parameters.
     569         /// </summary>
     570         /// <remarks>
     571         /// e.g.:  
     572         ///  DataSet ds = ExecuteDataset(trans, CommandType.StoredProcedure, "GetOrders", new SqlParameter("@prodid", 24));
     573         /// </remarks>
     574         /// <param name="transaction">a valid SqlTransaction</param>
     575         /// <param name="commandType">the CommandType (stored procedure, text, etc.)</param>
     576         /// <param name="commandText">the stored procedure name or T-SQL command</param>
     577         /// <param name="commandParameters">an array of SqlParamters used to execute the command</param>
     578         /// <returns>a dataset containing the resultset generated by the command</returns>
     579         public DataSet ExecuteDataset(SqlTransaction transaction, CommandType commandType, string commandText, params SqlParameter[] commandParameters)
     580         {
     581             //create a command and prepare it for execution
     582             SqlCommand cmd = new SqlCommand();
     583             PrepareCommand(cmd, transaction.Connection, transaction, commandType, commandText, commandParameters);
     584 
     585             //create the DataAdapter & DataSet
     586             SqlDataAdapter da = new SqlDataAdapter(cmd);
     587             DataSet ds = new DataSet();
     588 
     589             //fill the DataSet using default values for DataTable names, etc.
     590             da.Fill(ds);
     591 
     592             // detach the SqlParameters from the command object, so they can be used again.
     593             cmd.Parameters.Clear();
     594 
     595             //st.End("ExecuteDataset_2", commandText);
     596             //return the dataset
     597             return ds;
     598         }
     599 
     600         /// <summary>
     601         /// 返回datataset 只需要传入查询语句
     602         /// </summary>
     603         /// <param name="commandText"></param>
     604         /// <returns></returns>
     605         public DataSet ExecuteDataset(string commandText)
     606         {
     607             //pass through the call providing null for the set of SqlParameters
     608             return ExecuteDataset(connStr, CommandType.Text, commandText, (SqlParameter[])null);
     609         }
     610 
     611         /// <summary>
     612         /// 带参数查询
     613         /// </summary>
     614         /// <param name="commandText"></param>
     615         /// <param name="commandParameters"></param>
     616         /// <returns></returns>
     617         public DataSet ExecuteDataset(string commandText, params SqlParameter[] commandParameters)
     618         {
     619             using (SqlConnection cn = new SqlConnection(connStr))
     620             {
     621                 cn.Open();
     622                 return ExecuteDataset(cn, CommandType.Text, commandText, commandParameters);
     623             }
     624         }
     625         /// <summary>
     626         /// 返回DataSet
     627         /// </summary>
     628         /// <param name="commandText">sql语句</param>
     629         /// <param name="commandParameters">Hashtable参数</param>
     630         /// <returns>返回结果</returns>
     631         public DataSet ExecuteDataset(string commandText, Hashtable commandParameters)
     632         {
     633             using (SqlConnection cn = new SqlConnection(connStr))
     634             {
     635                 cn.Open();
     636                 return ExecuteDataset(cn, CommandType.Text, commandText, MakeAllParam(commandParameters));
     637             }
     638         }
     639 
     640         /// <summary>
     641         /// 执行存储过程 返回相应的dataset
     642         /// </summary>
     643         /// <param name="commandText">存储过程名字</param>
     644         /// <param name="commandType"></param>
     645         /// <param name="commandParameters"></param>
     646         /// <returns></returns>
     647         public DataSet ExecuteDataset(string commandText, CommandType commandType, params SqlParameter[] commandParameters)
     648         {
     649             using (SqlConnection cn = new SqlConnection(connStr))
     650             {
     651                 cn.Open();
     652                 return ExecuteDataset(cn, commandType, commandText, commandParameters);
     653             }
     654         }
     655         /// <summary>
     656         /// 执行sql语句时 返回相应的dataset
     657         /// </summary>
     658         /// <param name="commandText">sql语句</param>
     659         /// <param name="commandType">参数类型</param>
     660         /// <param name="commandParameters">Hashtable参数</param>
     661         /// <returns>返回值</returns>
     662         public DataSet ExecuteDataset(string commandText, CommandType commandType, Hashtable commandParameters)
     663         {
     664             using (SqlConnection cn = new SqlConnection(connStr))
     665             {
     666                 cn.Open();
     667                 return ExecuteDataset(cn, commandType, commandText, MakeAllParam(commandParameters));
     668             }
     669         }
     670 
     671         /// <summary>
     672         /// 事务中执行返回dataset
     673         /// </summary>
     674         /// <param name="transaction"></param>
     675         /// <param name="commandText"></param>
     676         /// <returns></returns>
     677         public DataSet ExecuteDataset(SqlTransaction transaction, string commandText)
     678         {
     679             //pass through the call providing null for the set of SqlParameters
     680             return ExecuteDataset(transaction, CommandType.Text, commandText, (SqlParameter[])null);
     681         }
     682         /// <summary>
     683         /// 事务中执行返回dataset 可带Hashtable参数
     684         /// </summary>
     685         /// <param name="transaction">事务</param>
     686         /// <param name="commandText">sql语句</param>
     687         /// <param name="commandParameters">Hashtable参数</param>
     688         /// <returns></returns>
     689         public DataSet ExecuteDataset(SqlTransaction transaction, string commandText, Hashtable commandParameters)
     690         {
     691             return ExecuteDataset(transaction, commandText, MakeAllParam(commandParameters));
     692         }
     693 
     694         /// <summary>
     695         /// 事务中返回dataset 可带参数
     696         /// </summary>
     697         /// <param name="transaction"></param>
     698         /// <param name="commandText"></param>
     699         /// <param name="commandParameters"></param>
     700         /// <returns></returns>
     701         public DataSet ExecuteDataset(SqlTransaction transaction, string commandText, params SqlParameter[] commandParameters)
     702         {
     703             //create a command and prepare it for execution
     704             SqlCommand cmd = new SqlCommand();
     705             PrepareCommand(cmd, transaction.Connection, transaction, CommandType.Text, commandText, commandParameters);
     706 
     707             //create the DataAdapter & DataSet
     708             SqlDataAdapter da = new SqlDataAdapter(cmd);
     709             DataSet ds = new DataSet();
     710             da.Fill(ds);
     711 
     712             cmd.Parameters.Clear();
     713             return ds;
     714         }
     715 
     716 
     717         #endregion ExecuteDataSet
     718 
     719         #region ExecuteDataTable
     720 
     721         /// <summary>
     722         /// Execute a SqlCommand (that returns a resultset and takes no parameters) against the database specified in 
     723         /// the connection string. 
     724         /// </summary>
     725         /// <remarks>
     726         /// e.g.:  
     727         ///  DataTable dt = ExecuteDataTable(connString, CommandType.StoredProcedure, "GetOrders");
     728         /// </remarks>
     729         /// <param name="connectionString">a valid connection string for a SqlConnection</param>
     730         /// <param name="commandType">the CommandType (stored procedure, text, etc.)</param>
     731         /// <param name="commandText">the stored procedure name or T-SQL command</param>
     732         /// <returns>a DataTable containing the resultset generated by the command</returns>
     733         public DataTable ExecuteDataTable(string connectionString, CommandType commandType, string commandText)
     734         {
     735             //pass through the call providing null for the set of SqlParameters
     736             return ExecuteDataTable(connectionString, commandType, commandText, (SqlParameter[])null);
     737         }
     738 
     739         /// <summary>
     740         /// Execute a SqlCommand (that returns a resultset) against the database specified in the connection string 
     741         /// using the provided parameters.
     742         /// </summary>
     743         /// <remarks>
     744         /// e.g.:  
     745         ///  DataTable dt = ExecuteDataTable(connString, CommandType.StoredProcedure, "GetOrders", new SqlParameter("@prodid", 24));
     746         /// </remarks>
     747         /// <param name="connectionString">a valid connection string for a SqlConnection</param>
     748         /// <param name="commandType">the CommandType (stored procedure, text, etc.)</param>
     749         /// <param name="commandText">the stored procedure name or T-SQL command</param>
     750         /// <param name="commandParameters">an array of SqlParamters used to execute the command</param>
     751         /// <returns>a DataTable containing the resultset generated by the command</returns>
     752         public DataTable ExecuteDataTable(string connectionString, CommandType commandType, string commandText, params SqlParameter[] commandParameters)
     753         {
     754             //create & open a SqlConnection, and dispose of it after we are done.
     755             using (SqlConnection cn = new SqlConnection(connectionString))
     756             {
     757                 cn.Open();
     758 
     759                 //call the overload that takes a connection in place of the connection string
     760                 return ExecuteDataTable(cn, commandType, commandText, commandParameters);
     761             }
     762         }
     763 
     764         /// <summary>
     765         /// Execute a SqlCommand (that returns a resultset and takes no parameters) against the provided SqlConnection. 
     766         /// </summary>
     767         /// <remarks>
     768         /// e.g.:  
     769         ///  DataTable dt = ExecuteDataTable(conn, CommandType.StoredProcedure, "GetOrders");
     770         /// </remarks>
     771         /// <param name="connection">a valid SqlConnection</param>
     772         /// <param name="commandType">the CommandType (stored procedure, text, etc.)</param>
     773         /// <param name="commandText">the stored procedure name or T-SQL command</param>
     774         /// <returns>a DataTable containing the resultset generated by the command</returns>
     775         public DataTable ExecuteDataTable(SqlConnection connection, CommandType commandType, string commandText)
     776         {
     777             //pass through the call providing null for the set of SqlParameters
     778             return ExecuteDataTable(connection, commandType, commandText, (SqlParameter[])null);
     779         }
     780 
     781         /// <summary>
     782         /// Execute a SqlCommand (that returns a resultset) against the specified SqlConnection 
     783         /// using the provided parameters.
     784         /// </summary>
     785         /// <remarks>
     786         /// e.g.:  
     787         ///  DataTable dt = ExecuteDataTable(conn, CommandType.StoredProcedure, "GetOrders", new SqlParameter("@prodid", 24));
     788         /// </remarks>
     789         /// <param name="connection">a valid SqlConnection</param>
     790         /// <param name="commandType">the CommandType (stored procedure, text, etc.)</param>
     791         /// <param name="commandText">the stored procedure name or T-SQL command</param>
     792         /// <param name="commandParameters">an array of SqlParamters used to execute the command</param>
     793         /// <returns>a DataTable containing the resultset generated by the command</returns>
     794         public DataTable ExecuteDataTable(SqlConnection connection, CommandType commandType, string commandText, params SqlParameter[] commandParameters)
     795         {
     796             //create a command and prepare it for execution
     797             SqlCommand cmd = new SqlCommand();
     798             PrepareCommand(cmd, connection, (SqlTransaction)null, commandType, commandText, commandParameters);
     799 
     800             //create the DataAdapter & DataTable
     801             SqlDataAdapter da = new SqlDataAdapter(cmd);
     802             DataTable dt = new DataTable();
     803 
     804             //fill the DataTable using default values for DataTable names, etc.
     805             da.Fill(dt);
     806 
     807             // detach the SqlParameters from the command object, so they can be used again.            
     808             cmd.Parameters.Clear();
     809 
     810             return dt;
     811         }
     812 
     813 
     814         /// <summary>
     815         /// Execute a SqlCommand (that returns a resultset and takes no parameters) against the provided SqlTransaction. 
     816         /// </summary>
     817         /// <remarks>
     818         /// e.g.:  
     819         ///  DataTable dt = ExecuteDataTable(trans, CommandType.StoredProcedure, "GetOrders");
     820         /// </remarks>
     821         /// <param name="transaction">a valid SqlTransaction</param>
     822         /// <param name="commandType">the CommandType (stored procedure, text, etc.)</param>
     823         /// <param name="commandText">the stored procedure name or T-SQL command</param>
     824         /// <returns>a DataTable containing the resultset generated by the command</returns>
     825         public DataTable ExecuteDataTable(SqlTransaction transaction, CommandType commandType, string commandText)
     826         {
     827             //pass through the call providing null for the set of SqlParameters
     828             return ExecuteDataTable(transaction, commandType, commandText, (SqlParameter[])null);
     829         }
     830 
     831         /// <summary>
     832         /// Execute a SqlCommand (that returns a resultset) against the specified SqlTransaction
     833         /// using the provided parameters.
     834         /// </summary>
     835         /// <remarks>
     836         /// e.g.:  
     837         ///  DataTable dt = ExecuteDataTable(trans, CommandType.StoredProcedure, "GetOrders", new SqlParameter("@prodid", 24));
     838         /// </remarks>
     839         /// <param name="transaction">a valid SqlTransaction</param>
     840         /// <param name="commandType">the CommandType (stored procedure, text, etc.)</param>
     841         /// <param name="commandText">the stored procedure name or T-SQL command</param>
     842         /// <param name="commandParameters">an array of SqlParamters used to execute the command</param>
     843         /// <returns>a DataTable containing the resultset generated by the command</returns>
     844         public DataTable ExecuteDataTable(SqlTransaction transaction, CommandType commandType, string commandText, params SqlParameter[] commandParameters)
     845         {
     846             //create a command and prepare it for execution
     847             SqlCommand cmd = new SqlCommand();
     848             PrepareCommand(cmd, transaction.Connection, transaction, commandType, commandText, commandParameters);
     849 
     850             //create the DataAdapter & DataTable
     851             SqlDataAdapter da = new SqlDataAdapter(cmd);
     852             DataTable dt = new DataTable();
     853 
     854             //fill the DataTable using default values for DataTable names, etc.
     855             da.Fill(dt);
     856 
     857             // detach the SqlParameters from the command object, so they can be used again.
     858             cmd.Parameters.Clear();
     859 
     860             //return the DataTable
     861             return dt;
     862         }
     863 
     864 
     865         /// <summary>
     866         /// 返回DataTable
     867         /// </summary>
     868         /// <param name="commandText">简单sql语句</param>
     869         /// <returns></returns>
     870         public DataTable ExecuteDataTable(string commandText)
     871         {
     872             //pass through the call providing null for the set of SqlParameters
     873             return ExecuteDataTable(connStr, CommandType.Text, commandText, (SqlParameter[])null);
     874         }
     875         /// <summary>
     876         /// 返回DataTable
     877         /// </summary>
     878         /// <param name="connectionString">连接串</param>
     879         /// <param name="commandText">简单sql语句</param>
     880         /// <returns></returns>
     881         public DataTable ExecuteDataTable(string connectionString, string commandText)
     882         {
     883             //pass through the call providing null for the set of SqlParameters
     884             return ExecuteDataTable(connectionString, CommandType.Text, commandText, (SqlParameter[])null);
     885         }
     886 
     887         /// <summary>
     888         /// 返回DataTable
     889         /// </summary>
     890         /// <param name="commandText">简单sql语句</param>
     891         /// <param name="commandParameters">可以连接到的参数</param>
     892         /// <returns></returns>
     893         public DataTable ExecuteDataTable(string commandText, params SqlParameter[] commandParameters)
     894         {
     895             //pass through the call providing null for the set of SqlParameters
     896             return ExecuteDataTable(connStr, CommandType.Text, commandText, commandParameters);
     897         }
     898 
     899         /// <summary>
     900         /// 返回DataTable 可带Hashtable参数
     901         /// </summary>
     902         /// <param name="commandText">sql语句</param>
     903         /// <param name="commandParameters">Hashtable参数</param>
     904         /// <returns></returns>
     905         public DataTable ExecuteDataTable(string commandText, Hashtable commandParameters)
     906         {
     907             //pass through the call providing null for the set of SqlParameters
     908             return ExecuteDataTable(connStr, CommandType.Text, commandText, MakeAllParam(commandParameters));
     909         }
     910 
     911         #endregion ExecuteDataTable
     912 
     913         #region ExecuteReader
     914 
     915         /// <summary>
     916         /// this enum is used to indicate whether the connection was provided by the caller, or created by SqlHelper, so that
     917         /// we can set the appropriate CommandBehavior when calling ExecuteReader()
     918         /// </summary>
     919         private enum SqlConnectionOwnership
     920         {
     921             /// <summary>Connection is owned and managed by SqlHelper</summary>
     922             Internal,
     923             /// <summary>Connection is owned and managed by the caller</summary>
     924             External
     925         }
     926 
     927         /// <summary>
     928         /// Create and prepare a SqlCommand, and call ExecuteReader with the appropriate CommandBehavior.
     929         /// </summary>
     930         /// <remarks>
     931         /// If we created and opened the connection, we want the connection to be closed when the DataReader is closed.
     932         /// 
     933         /// If the caller provided the connection, we want to leave it to them to manage.
     934         /// </remarks>
     935         /// <param name="connection">a valid SqlConnection, on which to execute this command</param>
     936         /// <param name="transaction">a valid SqlTransaction, or 'null'</param>
     937         /// <param name="commandType">the CommandType (stored procedure, text, etc.)</param>
     938         /// <param name="commandText">the stored procedure name or T-SQL command</param>
     939         /// <param name="commandParameters">an array of SqlParameters to be associated with the command or 'null' if no parameters are required</param>
     940         /// <param name="connectionOwnership">indicates whether the connection parameter was provided by the caller, or created by SqlHelper</param>
     941         /// <returns>SqlDataReader containing the results of the command</returns>
     942         private SqlDataReader ExecuteReader(SqlConnection connection, SqlTransaction transaction, CommandType commandType, string commandText, SqlParameter[] commandParameters, SqlConnectionOwnership connectionOwnership)
     943         {
     944             //SqlNotice st = new SqlNotice();
     945             //create a command and prepare it for execution
     946             SqlCommand cmd = new SqlCommand();
     947             PrepareCommand(cmd, connection, transaction, commandType, commandText, commandParameters);
     948 
     949             //create a reader
     950             SqlDataReader dr;
     951 
     952             // call ExecuteReader with the appropriate CommandBehavior
     953             if (connectionOwnership == SqlConnectionOwnership.External)
     954             {
     955                 dr = cmd.ExecuteReader();
     956             }
     957             else
     958             {
     959                 dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
     960             }
     961 
     962             // detach the SqlParameters from the command object, so they can be used again.
     963             cmd.Parameters.Clear();
     964 
     965             // st.End("ExecuteReader_1", commandText);
     966             return dr;
     967         }
     968 
     969         /// <summary>
     970         /// Execute a SqlCommand (that returns a resultset and takes no parameters) against the database specified in 
     971         /// the connection string. 
     972         /// </summary>
     973         /// <remarks>
     974         /// e.g.:  
     975         ///  SqlDataReader dr = ExecuteReader(connString, CommandType.StoredProcedure, "GetOrders");
     976         /// </remarks>
     977         /// <param name="connectionString">a valid connection string for a SqlConnection</param>
     978         /// <param name="commandType">the CommandType (stored procedure, text, etc.)</param>
     979         /// <param name="commandText">the stored procedure name or T-SQL command</param>
     980         /// <returns>a SqlDataReader containing the resultset generated by the command</returns>
     981         public SqlDataReader ExecuteReader(string connectionString, CommandType commandType, string commandText)
     982         {
     983             //pass through the call providing null for the set of SqlParameters
     984             return ExecuteReader(connectionString, commandType, commandText, (SqlParameter[])null);
     985         }
     986 
     987         /// <summary>
     988         /// Execute a SqlCommand (that returns a resultset) against the database specified in the connection string 
     989         /// using the provided parameters.
     990         /// </summary>
     991         /// <remarks>
     992         /// e.g.:  
     993         ///  SqlDataReader dr = ExecuteReader(connString, CommandType.StoredProcedure, "GetOrders", new SqlParameter("@prodid", 24));
     994         /// </remarks>
     995         /// <param name="connectionString">a valid connection string for a SqlConnection</param>
     996         /// <param name="commandType">the CommandType (stored procedure, text, etc.)</param>
     997         /// <param name="commandText">the stored procedure name or T-SQL command</param>
     998         /// <param name="commandParameters">an array of SqlParamters used to execute the command</param>
     999         /// <returns>a SqlDataReader containing the resultset generated by the command</returns>
    1000         public SqlDataReader ExecuteReader(string connectionString, CommandType commandType, string commandText, params SqlParameter[] commandParameters)
    1001         {
    1002             //create & open a SqlConnection
    1003             SqlConnection cn = new SqlConnection(connectionString);
    1004             cn.Open();
    1005 
    1006             try
    1007             {
    1008                 //call the private overload that takes an internally owned connection in place of the connection string
    1009                 return ExecuteReader(cn, null, commandType, commandText, commandParameters, SqlConnectionOwnership.Internal);
    1010             }
    1011             catch
    1012             {
    1013                 //if we fail to return the SqlDatReader, we need to close the connection ourselves
    1014                 cn.Close();
    1015                 throw;
    1016             }
    1017         }
    1018 
    1019         /// <summary>
    1020         /// Execute a SqlCommand (that returns a resultset and takes no parameters) against the provided SqlConnection. 
    1021         /// </summary>
    1022         /// <remarks>
    1023         /// e.g.:  
    1024         ///  SqlDataReader dr = ExecuteReader(conn, CommandType.StoredProcedure, "GetOrders");
    1025         /// </remarks>
    1026         /// <param name="connection">a valid SqlConnection</param>
    1027         /// <param name="commandType">the CommandType (stored procedure, text, etc.)</param>
    1028         /// <param name="commandText">the stored procedure name or T-SQL command</param>
    1029         /// <returns>a SqlDataReader containing the resultset generated by the command</returns>
    1030         public SqlDataReader ExecuteReader(SqlConnection connection, CommandType commandType, string commandText)
    1031         {
    1032             //pass through the call providing null for the set of SqlParameters
    1033             return ExecuteReader(connection, commandType, commandText, (SqlParameter[])null);
    1034         }
    1035 
    1036         /// <summary>
    1037         /// Execute a SqlCommand (that returns a resultset) against the specified SqlConnection 
    1038         /// using the provided parameters.
    1039         /// </summary>
    1040         /// <remarks>
    1041         /// e.g.:  
    1042         ///  SqlDataReader dr = ExecuteReader(conn, CommandType.StoredProcedure, "GetOrders", new SqlParameter("@prodid", 24));
    1043         /// </remarks>
    1044         /// <param name="connection">a valid SqlConnection</param>
    1045         /// <param name="commandType">the CommandType (stored procedure, text, etc.)</param>
    1046         /// <param name="commandText">the stored procedure name or T-SQL command</param>
    1047         /// <param name="commandParameters">an array of SqlParamters used to execute the command</param>
    1048         /// <returns>a SqlDataReader containing the resultset generated by the command</returns>
    1049         public SqlDataReader ExecuteReader(SqlConnection connection, CommandType commandType, string commandText, params SqlParameter[] commandParameters)
    1050         {
    1051             //pass through the call to the private overload using a null transaction value and an externally owned connection
    1052             return ExecuteReader(connection, (SqlTransaction)null, commandType, commandText, commandParameters, SqlConnectionOwnership.External);
    1053         }
    1054 
    1055 
    1056         /// <summary>
    1057         /// Execute a SqlCommand (that returns a resultset and takes no parameters) against the provided SqlTransaction. 
    1058         /// </summary>
    1059         /// <remarks>
    1060         /// e.g.:  
    1061         ///  SqlDataReader dr = ExecuteReader(trans, CommandType.StoredProcedure, "GetOrders");
    1062         /// </remarks>
    1063         /// <param name="transaction">a valid SqlTransaction</param>
    1064         /// <param name="commandType">the CommandType (stored procedure, text, etc.)</param>
    1065         /// <param name="commandText">the stored procedure name or T-SQL command</param>
    1066         /// <returns>a SqlDataReader containing the resultset generated by the command</returns>
    1067         public SqlDataReader ExecuteReader(SqlTransaction transaction, CommandType commandType, string commandText)
    1068         {
    1069             //pass through the call providing null for the set of SqlParameters
    1070             return ExecuteReader(transaction, commandType, commandText, (SqlParameter[])null);
    1071         }
    1072 
    1073         /// <summary>
    1074         /// Execute a SqlCommand (that returns a resultset) against the specified SqlTransaction
    1075         /// using the provided parameters.
    1076         /// </summary>
    1077         /// <remarks>
    1078         /// e.g.:  
    1079         ///   SqlDataReader dr = ExecuteReader(trans, CommandType.StoredProcedure, "GetOrders", new SqlParameter("@prodid", 24));
    1080         /// </remarks>
    1081         /// <param name="transaction">a valid SqlTransaction</param>
    1082         /// <param name="commandType">the CommandType (stored procedure, text, etc.)</param>
    1083         /// <param name="commandText">the stored procedure name or T-SQL command</param>
    1084         /// <param name="commandParameters">an array of SqlParamters used to execute the command</param>
    1085         /// <returns>a SqlDataReader containing the resultset generated by the command</returns>
    1086         public SqlDataReader ExecuteReader(SqlTransaction transaction, CommandType commandType, string commandText, params SqlParameter[] commandParameters)
    1087         {
    1088             //pass through to private overload, indicating that the connection is owned by the caller
    1089             return ExecuteReader(transaction.Connection, transaction, commandType, commandText, commandParameters, SqlConnectionOwnership.External);
    1090         }
    1091 
    1092         /// <summary>
    1093         /// 返回SqlDataReader 只是传入一条sql语句
    1094         /// </summary>
    1095         /// <param name="commandText">sql语句</param>
    1096         /// <returns></returns>
    1097         public SqlDataReader ExecuteReader(string commandText)
    1098         {
    1099             return ExecuteReader(connStr, CommandType.Text, commandText, (SqlParameter[])null);
    1100         }
    1101 
    1102         /// <summary>
    1103         /// 返回SqlDataReader 只是传入一条sql语句和相应的参数
    1104         /// </summary>
    1105         /// <param name="commandText"></param>
    1106         /// <param name="commandParameters"></param>
    1107         /// <returns></returns>
    1108         public SqlDataReader ExecuteReader(string commandText, params SqlParameter[] commandParameters)
    1109         {
    1110             //create & open a SqlConnection
    1111             SqlConnection cn = new SqlConnection(connStr);
    1112             cn.Open();
    1113 
    1114             try
    1115             {
    1116                 //call the private overload that takes an internally owned connection in place of the connection string
    1117                 return ExecuteReader(cn, null, CommandType.Text, commandText, commandParameters, SqlConnectionOwnership.Internal);
    1118             }
    1119             catch
    1120             {
    1121                 //if we fail to return the SqlDatReader, we need to close the connection ourselves
    1122                 cn.Close();
    1123                 throw;
    1124             }
    1125         }
    1126 
    1127         /// <summary>
    1128         /// 返回SqlDataReader 只是传入一条sql语句和相应的Hashtable参数
    1129         /// </summary>
    1130         /// <param name="commandText">sql语句</param>
    1131         /// <param name="commandParameters">Hashtable参数</param>
    1132         /// <returns>返回值</returns>
    1133         public SqlDataReader ExecuteReader(string commandText, Hashtable commandParameters)
    1134         {
    1135             //create & open a SqlConnection
    1136             SqlConnection cn = new SqlConnection(connStr);
    1137             cn.Open();
    1138 
    1139             try
    1140             {
    1141                 //call the private overload that takes an internally owned connection in place of the connection string
    1142                 return ExecuteReader(cn, null, CommandType.Text, commandText, MakeAllParam(commandParameters), SqlConnectionOwnership.Internal);
    1143             }
    1144             catch
    1145             {
    1146                 //if we fail to return the SqlDatReader, we need to close the connection ourselves
    1147                 cn.Close();
    1148                 throw;
    1149             }
    1150         }
    1151 
    1152         /// <summary>
    1153         /// 返回SqlDataReader 只是传入一条sql语句和相应的参数
    1154         /// </summary>
    1155         /// <param name="commandText"></param>
    1156         /// <param name="commandParameters"></param>
    1157         /// <returns></returns>
    1158         public SqlDataReader ExecuteReader(string commandText, CommandType commandType, params SqlParameter[] commandParameters)
    1159         {
    1160             //create & open a SqlConnection
    1161             SqlConnection cn = new SqlConnection(connStr);
    1162             cn.Open();
    1163 
    1164             try
    1165             {
    1166                 //call the private overload that takes an internally owned connection in place of the connection string
    1167                 return ExecuteReader(cn, null, commandType, commandText, commandParameters, SqlConnectionOwnership.Internal);
    1168             }
    1169             catch
    1170             {
    1171                 //if we fail to return the SqlDatReader, we need to close the connection ourselves
    1172                 cn.Close();
    1173                 throw;
    1174             }
    1175         }
    1176 
    1177         /// <summary>
    1178         /// 返回SqlDataReader 只是传入一条sql语句和相应的Hashtable参数
    1179         /// </summary>
    1180         /// <param name="commandText">sql语句</param>
    1181         /// <param name="commandType">参数类型</param>
    1182         /// <param name="commandParameters">Hashtable参数</param>
    1183         /// <returns>返回值</returns>
    1184         public SqlDataReader ExecuteReader(string commandText, CommandType commandType, Hashtable commandParameters)
    1185         {
    1186             //create & open a SqlConnection
    1187             SqlConnection cn = new SqlConnection(connStr);
    1188             cn.Open();
    1189 
    1190             try
    1191             {
    1192                 //call the private overload that takes an internally owned connection in place of the connection string
    1193                 return ExecuteReader(cn, null, commandType, commandText, MakeAllParam(commandParameters), SqlConnectionOwnership.Internal);
    1194             }
    1195             catch
    1196             {
    1197                 //if we fail to return the SqlDatReader, we need to close the connection ourselves
    1198                 cn.Close();
    1199                 throw;
    1200             }
    1201         }
    1202         #endregion ExecuteReader
    1203 
    1204         #region ExecuteScalar
    1205 
    1206         /// <summary>
    1207         /// Execute a SqlCommand (that returns a 1x1 resultset and takes no parameters) against the database specified in 
    1208         /// the connection string. 
    1209         /// </summary>
    1210         /// <remarks>
    1211         /// e.g.:  
    1212         ///  int orderCount = (int)ExecuteScalar(connString, CommandType.StoredProcedure, "GetOrderCount");
    1213         /// </remarks>
    1214         /// <param name="connectionString">a valid connection string for a SqlConnection</param>
    1215         /// <param name="commandType">the CommandType (stored procedure, text, etc.)</param>
    1216         /// <param name="commandText">the stored procedure name or T-SQL command</param>
    1217         /// <returns>an object containing the value in the 1x1 resultset generated by the command</returns>
    1218         public object ExecuteScalar(string connectionString, CommandType commandType, string commandText)
    1219         {
    1220             //pass through the call providing null for the set of SqlParameters
    1221             return ExecuteScalar(connectionString, commandType, commandText, (SqlParameter[])null);
    1222         }
    1223 
    1224         /// <summary>
    1225         /// Execute a SqlCommand (that returns a 1x1 resultset) against the database specified in the connection string 
    1226         /// using the provided parameters.
    1227         /// </summary>
    1228         /// <remarks>
    1229         /// e.g.:  
    1230         ///  int orderCount = (int)ExecuteScalar(connString, CommandType.StoredProcedure, "GetOrderCount", new SqlParameter("@prodid", 24));
    1231         /// </remarks>
    1232         /// <param name="connectionString">a valid connection string for a SqlConnection</param>
    1233         /// <param name="commandType">the CommandType (stored procedure, text, etc.)</param>
    1234         /// <param name="commandText">the stored procedure name or T-SQL command</param>
    1235         /// <param name="commandParameters">an array of SqlParamters used to execute the command</param>
    1236         /// <returns>an object containing the value in the 1x1 resultset generated by the command</returns>
    1237         public object ExecuteScalar(string connectionString, CommandType commandType, string commandText, params SqlParameter[] commandParameters)
    1238         {
    1239             //create & open a SqlConnection, and dispose of it after we are done.
    1240             using (SqlConnection cn = new SqlConnection(connectionString))
    1241             {
    1242                 cn.Open();
    1243 
    1244                 //call the overload that takes a connection in place of the connection string
    1245                 return ExecuteScalar(cn, commandType, commandText, commandParameters);
    1246             }
    1247         }
    1248 
    1249         /// <summary>
    1250         /// Execute a SqlCommand (that returns a 1x1 resultset and takes no parameters) against the provided SqlConnection. 
    1251         /// </summary>
    1252         /// <remarks>
    1253         /// e.g.:  
    1254         ///  int orderCount = (int)ExecuteScalar(conn, CommandType.StoredProcedure, "GetOrderCount");
    1255         /// </remarks>
    1256         /// <param name="connection">a valid SqlConnection</param>
    1257         /// <param name="commandType">the CommandType (stored procedure, text, etc.)</param>
    1258         /// <param name="commandText">the stored procedure name or T-SQL command</param>
    1259         /// <returns>an object containing the value in the 1x1 resultset generated by the command</returns>
    1260         public object ExecuteScalar(SqlConnection connection, CommandType commandType, string commandText)
    1261         {
    1262             //pass through the call providing null for the set of SqlParameters
    1263             return ExecuteScalar(connection, commandType, commandText, (SqlParameter[])null);
    1264         }
    1265 
    1266         /// <summary>
    1267         /// Execute a SqlCommand (that returns a 1x1 resultset) against the specified SqlConnection 
    1268         /// using the provided parameters.
    1269         /// </summary>
    1270         /// <remarks>
    1271         /// e.g.:  
    1272         ///  int orderCount = (int)ExecuteScalar(conn, CommandType.StoredProcedure, "GetOrderCount", new SqlParameter("@prodid", 24));
    1273         /// </remarks>
    1274         /// <param name="connection">a valid SqlConnection</param>
    1275         /// <param name="commandType">the CommandType (stored procedure, text, etc.)</param>
    1276         /// <param name="commandText">the stored procedure name or T-SQL command</param>
    1277         /// <param name="commandParameters">an array of SqlParamters used to execute the command</param>
    1278         /// <returns>an object containing the value in the 1x1 resultset generated by the command</returns>
    1279         public object ExecuteScalar(SqlConnection connection, CommandType commandType, string commandText, params SqlParameter[] commandParameters)
    1280         {
    1281             //SqlNotice st = new SqlNotice();
    1282             //create a command and prepare it for execution
    1283             SqlCommand cmd = new SqlCommand();
    1284             PrepareCommand(cmd, connection, (SqlTransaction)null, commandType, commandText, commandParameters);
    1285 
    1286             //execute the command & return the results
    1287             object retval = cmd.ExecuteScalar();
    1288 
    1289             // detach the SqlParameters from the command object, so they can be used again.
    1290             cmd.Parameters.Clear();
    1291 
    1292             //st.End("ExecuteScalar_1", commandText);
    1293             return retval;
    1294 
    1295         }
    1296 
    1297         /// <summary>
    1298         /// Execute a SqlCommand (that returns a 1x1 resultset and takes no parameters) against the provided SqlTransaction. 
    1299         /// </summary>
    1300         /// <remarks>
    1301         /// e.g.:  
    1302         ///  int orderCount = (int)ExecuteScalar(trans, CommandType.StoredProcedure, "GetOrderCount");
    1303         /// </remarks>
    1304         /// <param name="transaction">a valid SqlTransaction</param>
    1305         /// <param name="commandType">the CommandType (stored procedure, text, etc.)</param>
    1306         /// <param name="commandText">the stored procedure name or T-SQL command</param>
    1307         /// <returns>an object containing the value in the 1x1 resultset generated by the command</returns>
    1308         public object ExecuteScalar(SqlTransaction transaction, CommandType commandType, string commandText)
    1309         {
    1310             //pass through the call providing null for the set of SqlParameters
    1311             return ExecuteScalar(transaction, commandType, commandText, (SqlParameter[])null);
    1312         }
    1313 
    1314         /// <summary>
    1315         /// Execute a SqlCommand (that returns a 1x1 resultset) against the specified SqlTransaction
    1316         /// using the provided parameters.
    1317         /// </summary>
    1318         /// <remarks>
    1319         /// e.g.:  
    1320         ///  int orderCount = (int)ExecuteScalar(trans, CommandType.StoredProcedure, "GetOrderCount", new SqlParameter("@prodid", 24));
    1321         /// </remarks>
    1322         /// <param name="transaction">a valid SqlTransaction</param>
    1323         /// <param name="commandType">the CommandType (stored procedure, text, etc.)</param>
    1324         /// <param name="commandText">the stored procedure name or T-SQL command</param>
    1325         /// <param name="commandParameters">an array of SqlParamters used to execute the command</param>
    1326         /// <returns>an object containing the value in the 1x1 resultset generated by the command</returns>
    1327         public object ExecuteScalar(SqlTransaction transaction, CommandType commandType, string commandText, params SqlParameter[] commandParameters)
    1328         {
    1329             //SqlNotice st = new SqlNotice();
    1330             //create a command and prepare it for execution
    1331             SqlCommand cmd = new SqlCommand();
    1332             PrepareCommand(cmd, transaction.Connection, transaction, commandType, commandText, commandParameters);
    1333 
    1334             //execute the command & return the results
    1335             object retval = cmd.ExecuteScalar();
    1336 
    1337             // detach the SqlParameters from the command object, so they can be used again.
    1338             cmd.Parameters.Clear();
    1339 
    1340             //st.End("ExecuteScalar_2", commandText);
    1341             return retval;
    1342         }
    1343 
    1344         /// <summary>
    1345         /// 返回ExecuteScalar 只是传入一条sql语句
    1346         /// </summary>
    1347         /// <param name="commandText">sql语句</param>
    1348         /// <returns></returns>
    1349         public object ExecuteScalar(string commandText)
    1350         {
    1351             return ExecuteScalar(connStr, CommandType.Text, commandText, (SqlParameter[])null);
    1352         }
    1353 
    1354         /// <summary>
    1355         /// 返回ExecuteScalar 可传入参数
    1356         /// </summary>
    1357         /// <param name="commandText">sql语句</param>
    1358         /// <param name="commandParameters">参数</param>
    1359         /// <returns></returns>
    1360         public object ExecuteScalar(string commandText, params SqlParameter[] commandParameters)
    1361         {
    1362             return ExecuteScalar(connStr, CommandType.Text, commandText, commandParameters);
    1363         }
    1364 
    1365         /// <summary>
    1366         /// 返回ExecuteScalar 可传入Hashtable参数
    1367         /// </summary>
    1368         /// <param name="commandText">sql语句</param>
    1369         /// <param name="commandParameters">Hashtable参数</param>
    1370         /// <returns></returns>
    1371         public object ExecuteScalar(string commandText, Hashtable commandParameters)
    1372         {
    1373             return ExecuteScalar(connStr, CommandType.Text, commandText, MakeAllParam(commandParameters));
    1374         }
    1375         /// <summary>
    1376         ///  返回ExecuteScalar 可传入事务、Hashtable参数
    1377         /// </summary>
    1378         /// <param name="transaction">事务</param>
    1379         /// <param name="commandText">sql语句</param>
    1380         /// <param name="commandParameters">Hashtable参数</param>
    1381         /// <returns></returns>
    1382         public object ExecuteScalar(SqlTransaction transaction, string commandText, Hashtable commandParameters)
    1383         {
    1384             return ExecuteScalar(transaction, CommandType.Text, commandText, MakeAllParam(commandParameters));
    1385         }
    1386         /// <summary>
    1387         /// 返回ExecuteScalar 可传入事务、参数
    1388         /// </summary>
    1389         /// <param name="transaction">事务</param>
    1390         /// <param name="commandText">sql语句</param>
    1391         /// <param name="commandParameters">参数</param>
    1392         /// <returns></returns>
    1393         public object ExecuteScalar(SqlTransaction transaction, string commandText, params SqlParameter[] commandParameters)
    1394         {
    1395             return ExecuteScalar(transaction, CommandType.Text, commandText, commandParameters);
    1396         }
    1397 
    1398         /// <summary>
    1399         /// 执行一个简单的Sqlcommand 没有返回结果
    1400         /// </summary>
    1401         ///  例如:  
    1402         ///  int result = ExecuteScalar(myTran,"delete from test where 1>2 ");  返回 result =0
    1403         /// <param name="transaction">事务名称</param>
    1404         /// <param name="commandText">只能是sql语句</param>
    1405         /// <returns>受影响的行数</returns>
    1406         public object ExecuteScalar(SqlTransaction transaction, string commandText)
    1407         {
    1408             return ExecuteScalar(transaction, CommandType.Text, commandText, (SqlParameter[])null);
    1409         }
    1410 
    1411         #endregion ExecuteScalar
    1412 
    1413         #region ExecuteXmlReader
    1414 
    1415         /// <summary>
    1416         /// Execute a SqlCommand (that returns a resultset and takes no parameters) against the provided SqlConnection. 
    1417         /// </summary>
    1418         /// <remarks>
    1419         /// e.g.:  
    1420         ///  XmlReader r = ExecuteXmlReader(conn, CommandType.StoredProcedure, "GetOrders");
    1421         /// </remarks>
    1422         /// <param name="connection">a valid SqlConnection</param>
    1423         /// <param name="commandType">the CommandType (stored procedure, text, etc.)</param>
    1424         /// <param name="commandText">the stored procedure name or T-SQL command using "FOR XML AUTO"</param>
    1425         /// <returns>an XmlReader containing the resultset generated by the command</returns>
    1426         public XmlReader ExecuteXmlReader(SqlConnection connection, CommandType commandType, string commandText)
    1427         {
    1428             //pass through the call providing null for the set of SqlParameters
    1429             return ExecuteXmlReader(connection, commandType, commandText, (SqlParameter[])null);
    1430         }
    1431 
    1432 
    1433 
    1434         /// <summary>
    1435         /// Execute a SqlCommand (that returns a resultset) against the specified SqlConnection 
    1436         /// using the provided parameters.
    1437         /// </summary>
    1438         /// <remarks>
    1439         /// e.g.:  
    1440         ///  XmlReader r = ExecuteXmlReader(conn, CommandType.StoredProcedure, "GetOrders", new SqlParameter("@prodid", 24));
    1441         /// </remarks>
    1442         /// <param name="connection">a valid SqlConnection</param>
    1443         /// <param name="commandType">the CommandType (stored procedure, text, etc.)</param>
    1444         /// <param name="commandText">the stored procedure name or T-SQL command using "FOR XML AUTO"</param>
    1445         /// <param name="commandParameters">an array of SqlParamters used to execute the command</param>
    1446         /// <returns>an XmlReader containing the resultset generated by the command</returns>
    1447         public XmlReader ExecuteXmlReader(SqlConnection connection, CommandType commandType, string commandText, params SqlParameter[] commandParameters)
    1448         {
    1449             //create a command and prepare it for execution
    1450             SqlCommand cmd = new SqlCommand();
    1451             PrepareCommand(cmd, connection, (SqlTransaction)null, commandType, commandText, commandParameters);
    1452 
    1453             //create the DataAdapter & DataSet
    1454             XmlReader retval = cmd.ExecuteXmlReader();
    1455 
    1456             // detach the SqlParameters from the command object, so they can be used again.
    1457             cmd.Parameters.Clear();
    1458             return retval;
    1459 
    1460         }
    1461 
    1462 
    1463         /// <summary>
    1464         /// Execute a SqlCommand (that returns a resultset and takes no parameters) against the provided SqlTransaction. 
    1465         /// </summary>
    1466         /// <remarks>
    1467         /// e.g.:  
    1468         ///  XmlReader r = ExecuteXmlReader(trans, CommandType.StoredProcedure, "GetOrders");
    1469         /// </remarks>
    1470         /// <param name="transaction">a valid SqlTransaction</param>
    1471         /// <param name="commandType">the CommandType (stored procedure, text, etc.)</param>
    1472         /// <param name="commandText">the stored procedure name or T-SQL command using "FOR XML AUTO"</param>
    1473         /// <returns>an XmlReader containing the resultset generated by the command</returns>
    1474         public XmlReader ExecuteXmlReader(SqlTransaction transaction, CommandType commandType, string commandText)
    1475         {
    1476             //pass through the call providing null for the set of SqlParameters
    1477             return ExecuteXmlReader(transaction, commandType, commandText, (SqlParameter[])null);
    1478         }
    1479 
    1480         /// <summary>
    1481         /// Execute a SqlCommand (that returns a resultset) against the specified SqlTransaction
    1482         /// using the provided parameters.
    1483         /// </summary>
    1484         /// <remarks>
    1485         /// e.g.:  
    1486         ///  XmlReader r = ExecuteXmlReader(trans, CommandType.StoredProcedure, "GetOrders", new SqlParameter("@prodid", 24));
    1487         /// </remarks>
    1488         /// <param name="transaction">a valid SqlTransaction</param>
    1489         /// <param name="commandType">the CommandType (stored procedure, text, etc.)</param>
    1490         /// <param name="commandText">the stored procedure name or T-SQL command using "FOR XML AUTO"</param>
    1491         /// <param name="commandParameters">an array of SqlParamters used to execute the command</param>
    1492         /// <returns>an XmlReader containing the resultset generated by the command</returns>
    1493         public XmlReader ExecuteXmlReader(SqlTransaction transaction, CommandType commandType, string commandText, params SqlParameter[] commandParameters)
    1494         {
    1495             //create a command and prepare it for execution
    1496             SqlCommand cmd = new SqlCommand();
    1497             PrepareCommand(cmd, transaction.Connection, transaction, commandType, commandText, commandParameters);
    1498 
    1499             //create the DataAdapter & DataSet
    1500             XmlReader retval = cmd.ExecuteXmlReader();
    1501 
    1502             // detach the SqlParameters from the command object, so they can be used again.
    1503             cmd.Parameters.Clear();
    1504             return retval;
    1505 
    1506         }
    1507 
    1508         #endregion ExecuteXmlReader
    1509 
    1510         /// <summary>
    1511         /// 存储过程可以使用事务
    1512         /// </summary>
    1513         /// <param name="commandType"></param>
    1514         /// <param name="commandText"></param>
    1515         /// <param name="IsTrans"></param>
    1516         /// <param name="commandParameters"></param>
    1517         /// <returns></returns>
    1518         public int ExecuteNonQuery(CommandType commandType, string commandText, string IsTrans, params SqlParameter[] commandParameters)
    1519         {
    1520             //create a command and prepare it for execution
    1521             PrepareCommand(commandType, commandText, IsTrans, commandParameters);
    1522 
    1523             //finally, execute the command.
    1524             int retval = sqlCmd.ExecuteNonQuery();
    1525 
    1526             // detach the SqlParameters from the command object, so they can be used again.
    1527             sqlCmd.Parameters.Clear();
    1528 
    1529             return retval;
    1530         }
    1531 
    1532         private void PrepareCommand(CommandType commandType, string commandText, string IsTrans, SqlParameter[] commandParameters)
    1533         {
    1534             //if the provided connection is not open, we will open it
    1535 
    1536             if (sqlConn.State != ConnectionState.Open)
    1537             {
    1538                 sqlConn.Open();
    1539             }
    1540 
    1541             if (IsTrans == "1")
    1542             {
    1543                 if (sqlCmd == null)
    1544                 {
    1545                     sqlCmd = new SqlCommand();
    1546                     sqlCmd.Connection = sqlConn;
    1547                     sqlCmd.CommandTimeout = 10000;
    1548                 }
    1549                 //set the command text (stored procedure name or SQL statement)
    1550                 sqlCmd.CommandText = commandText;
    1551 
    1552                 //set the command type
    1553                 sqlCmd.CommandType = commandType;
    1554 
    1555                 //attach the command parameters if they are provided
    1556                 if (commandParameters != null)
    1557                 {
    1558                     AttachParameters(sqlCmd, commandParameters);
    1559                 }
    1560 
    1561             }
    1562             return;
    1563         }
    1564 
    1565         #region  Ys SQL获取执行结果 
    1566 
    1567         /// <summary>
    1568         /// 增、删、改的方法[ExecuteNonQuery] 返回所影响的行数,执行其他
    1569         /// </summary>
    1570         /// <param name="sql">Sql命令</param>
    1571         /// <param name="cmdtype">SQL语句(CommandType.Text)或者存储过程(CommandType.StoredProcedure)</param>
    1572         /// <param name="pms"></param>
    1573         /// <returns></returns>
    1574         public  int GetExecuteNonQuery(string sql, CommandType cmdtype, params SqlParameter[] pms)
    1575         {
    1576             int i = -1;
    1577             try
    1578             {
    1579                 using (SqlConnection con = new SqlConnection(connStr))
    1580                 {
    1581                     using (SqlCommand cmd = new SqlCommand(sql, con))
    1582                     {
    1583                         //存储过程或者Sql语句
    1584                         cmd.CommandType = cmdtype;
    1585                         if (pms != null)
    1586                         {
    1587                             cmd.Parameters.AddRange(pms);
    1588                         }
    1589                         if (con.State != ConnectionState.Open)
    1590                         {
    1591                             con.Open();
    1592                         }
    1593                         i = cmd.ExecuteNonQuery();
    1594                     }
    1595                 }
    1596             }
    1597             catch (Exception)
    1598             {
    1599                 throw;
    1600             }
    1601 
    1602             return i;
    1603         }
    1604 
    1605         /// <summary>
    1606         /// 执行查询、查询多少条数据;返回第一行,第一列方法[ExecuteScalar] 返回-1执行失败 
    1607         /// </summary>
    1608         /// <param name="sql">Sql命令</param>
    1609         /// <param name="cmdtype">SQL语句(CommandType.Text)或者存储过程(CommandType.StoredProcedure)</param>
    1610         /// <param name="pms"></param>
    1611         /// <returns></returns>
    1612         public  object GetExecuteScalar(string sql, CommandType cmdtype, params SqlParameter[] pms)
    1613         {
    1614             try
    1615             {
    1616                 SqlConnection con = new SqlConnection(connStr);
    1617                 using (SqlCommand cmd = new SqlCommand(sql, con))
    1618                 {
    1619                     //存储过程或者Sql语句
    1620                     cmd.CommandType = cmdtype;
    1621                     if (pms != null)
    1622                     {
    1623                         cmd.Parameters.AddRange(pms);
    1624                     }
    1625                     if (con.State != ConnectionState.Open)
    1626                     {
    1627                         con.Open();
    1628                     }
    1629                     return cmd.ExecuteScalar();
    1630                 }
    1631             }
    1632             catch (Exception)
    1633             {
    1634                 throw;
    1635             }
    1636         }
    1637 
    1638         /// <summary>
    1639         /// 执行查询,返回多行、多列的方法ExecuteReader()
    1640         /// </summary>
    1641         /// <param name="sql">Sql命令</param>
    1642         /// <param name="cmdtype">SQL语句(CommandType.Text)或者存储过程(CommandType.StoredProcedure)</param>
    1643         /// <param name="pms"></param>
    1644         /// <returns></returns>
    1645         public  SqlDataReader GetExecuteReader(string sql, CommandType cmdtype, params SqlParameter[] pms)
    1646         {
    1647             SqlConnection con = new SqlConnection(connStr);
    1648 
    1649             using (SqlCommand cmd = new SqlCommand(sql, con))
    1650             {
    1651                 //cmd.CommandTimeout = 60000000;
    1652                 //存储过程或者Sql语句
    1653                 cmd.CommandType = cmdtype;
    1654                 if (pms != null)
    1655                 {
    1656                     cmd.Parameters.AddRange(pms);
    1657                 }
    1658 
    1659                 try
    1660                 {
    1661                     if (con.State != ConnectionState.Open)
    1662                     {
    1663                         con.Open();
    1664                     }
    1665                     return cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection);
    1666                 }
    1667                 catch
    1668                 {
    1669                     con.Close();
    1670                     con.Dispose();
    1671                     throw;
    1672                 }
    1673             }
    1674         }
    1675 
    1676         /// <summary>
    1677         /// 执行返回多个查询时使用,返回DataTable类型
    1678         /// </summary>
    1679         /// <param name="sql">Sql命令</param>
    1680         /// <param name="cmdtype">SQL语句(CommandType.Text)或者存储过程(CommandType.StoredProcedure)</param>
    1681         /// <param name="pms">参数</param>
    1682         /// <returns></returns>
    1683         public  DataTable GetExecuteDataTable(string sql, CommandType cmdtype, params SqlParameter[] pms)
    1684         {
    1685             DataTable dt = new DataTable();
    1686             try
    1687             {
    1688                 //通过adapter读取数据。
    1689                 using (SqlDataAdapter adapter = new SqlDataAdapter(sql, connStr))
    1690                 {
    1691                     adapter.SelectCommand.CommandType = cmdtype;
    1692                     if (pms != null)
    1693                     {
    1694                         adapter.SelectCommand.Parameters.AddRange(pms);
    1695                     }
    1696 
    1697                     adapter.Fill(dt);
    1698                     return dt;
    1699                 }
    1700             }
    1701             catch (Exception)
    1702             {
    1703                 throw;
    1704             }
    1705         }
    1706 
    1707         /// <summary>  
    1708         /// 该方法是一个数据集方法在很多地方用得到的   
    1709         /// </summary>  
    1710         /// <param name="sql">第一个参数是传sql语句</param>  
    1711         /// <param name="table">第二个参数是传数据库当中的表名</param>  
    1712         /// <returns></returns>  
    1713         public  DataSet GetGetDataSet(string sql, CommandType cmdtype, string table)
    1714         {
    1715             try
    1716             {
    1717                 using (SqlDataAdapter adapter = new SqlDataAdapter(sql, connStr))
    1718                 {
    1719                     adapter.SelectCommand.CommandType = cmdtype;
    1720                     DataSet ds = new DataSet();
    1721                     adapter.Fill(ds, table);
    1722                     return ds;
    1723                 }
    1724             }
    1725             catch (Exception)
    1726             {
    1727                 throw;
    1728             }
    1729         }
    1730 
    1731         /// <summary>
    1732         /// 连接数据
    1733         /// </summary>
    1734         /// <param name="constring"></param>
    1735         /// <returns></returns>
    1736         public  bool GetOpen(string constring = null)
    1737         {
    1738             if (constring == null)
    1739             {
    1740                 constring = connStr;
    1741             }
    1742             SqlConnection con = new SqlConnection(constring);
    1743             try
    1744             {
    1745                 if (con.State != ConnectionState.Open)
    1746                 {
    1747                     con.Open();
    1748                 }
    1749                 if (con.State == ConnectionState.Open)
    1750                 {
    1751                     return true;
    1752                 }
    1753                 else
    1754                 {
    1755                     return false;
    1756                 }
    1757             }
    1758             catch
    1759             {
    1760                 con.Close();
    1761                 con.Dispose();
    1762                 return false;
    1763             }
    1764         }
    1765         #endregion
    1766     }
    View Code
  • 相关阅读:
    MYSQL limit用法
    mybaties mapping中if
    执行数据库同时又调接口
    WITH (NOLOCK)
    SpringMVC转发和重定向区别!
    MyBatis的foreach语句详解
    SSM mapper.xml
    win7与virtualbox中centos文件共享
    PBOC2.0中消费交易流程
    PBOC2.0协议中电子存折/电子钱包中圈存交易流程
  • 原文地址:https://www.cnblogs.com/yuanshuo/p/15192652.html
Copyright © 2011-2022 走看看