微软C#版SQLHelper.cs类

转载自:http://blog.csdn.net/fengqingtao2008/article/details/17399247

   1 using System;
   2 using System.Data;
   3 using System.Xml;
   4 using System.Data.SqlClient;
   5 using System.Collections;
   6 using System.Configuration;
   7
   8 namespace BookDAL
   9 {
  10     /// <summary>
  11     /// SqlServer数据访问帮助类
  12     /// </summary>
  13     public sealed class SqlHelper
  14     {
  15         #region 私有构造函数和方法
  16
  17         private SqlHelper() { }
  18
  19         /// <summary>
  20         /// 将SqlParameter参数数组(参数值)分配给SqlCommand命令.
  21         /// 这个方法将给任何一个参数分配DBNull.Value;
  22         /// 该操作将阻止默认值的使用.
  23         /// </summary>
  24         /// <param name="command">命令名</param>
  25         /// <param name="commandParameters">SqlParameters数组</param>
  26         private static void AttachParameters(SqlCommand command, SqlParameter[] commandParameters)
  27         {
  28             if (command == null) throw new ArgumentNullException("command");
  29             if (commandParameters != null)
  30             {
  31                 foreach (SqlParameter p in commandParameters)
  32                 {
  33                     if (p != null)
  34                     {
  35                         // 检查未分配值的输出参数,将其分配以DBNull.Value.
  36                         if ((p.Direction == ParameterDirection.InputOutput || p.Direction == ParameterDirection.Input) &&
  37                             (p.Value == null))
  38                         {
  39                             p.Value = DBNull.Value;
  40                         }
  41                         command.Parameters.Add(p);
  42                     }
  43                 }
  44             }
  45         }
  46
  47         /// <summary>
  48         /// 将DataRow类型的列值分配到SqlParameter参数数组.
  49         /// </summary>
  50         /// <param name="commandParameters">要分配值的SqlParameter参数数组</param>
  51         /// <param name="dataRow">将要分配给存储过程参数的DataRow</param>
  52         private static void AssignParameterValues(SqlParameter[] commandParameters, DataRow dataRow)
  53         {
  54             if ((commandParameters == null) || (dataRow == null))
  55             {
  56                 return;
  57             }
  58
  59             int i = 0;
  60             // 设置参数值
  61             foreach (SqlParameter commandParameter in commandParameters)
  62             {
  63                 // 创建参数名称,如果不存在,只抛出一个异常.
  64                 if (commandParameter.ParameterName == null ||
  65                     commandParameter.ParameterName.Length <= 1)
  66                     throw new Exception(
  67                         string.Format("请提供参数{0}一个有效的名称{1}.", i, commandParameter.ParameterName));
  68                 // 从dataRow的表中获取为参数数组中数组名称的列的索引.
  69                 // 如果存在和参数名称相同的列,则将列值赋给当前名称的参数.
  70                 if (dataRow.Table.Columns.IndexOf(commandParameter.ParameterName.Substring(1)) != -1)
  71                     commandParameter.Value = dataRow[commandParameter.ParameterName.Substring(1)];
  72                 i++;
  73             }
  74         }
  75
  76         /// <summary>
  77         /// 将一个对象数组分配给SqlParameter参数数组.
  78         /// </summary>
  79         /// <param name="commandParameters">要分配值的SqlParameter参数数组</param>
  80         /// <param name="parameterValues">将要分配给存储过程参数的对象数组</param>
  81         private static void AssignParameterValues(SqlParameter[] commandParameters, object[] parameterValues)
  82         {
  83             if ((commandParameters == null) || (parameterValues == null))
  84             {
  85                 return;
  86             }
  87
  88             // 确保对象数组个数与参数个数匹配,如果不匹配,抛出一个异常.
  89             if (commandParameters.Length != parameterValues.Length)
  90             {
  91                 throw new ArgumentException("参数值个数与参数不匹配.");
  92             }
  93
  94             // 给参数赋值
  95             for (int i = 0, j = commandParameters.Length; i < j; i++)
  96             {
  97                 // If the current array value derives from IDbDataParameter, then assign its Value property
  98                 if (parameterValues[i] is IDbDataParameter)
  99                 {
 100                     IDbDataParameter paramInstance = (IDbDataParameter)parameterValues[i];
 101                     if (paramInstance.Value == null)
 102                     {
 103                         commandParameters[i].Value = DBNull.Value;
 104                     }
 105                     else
 106                     {
 107                         commandParameters[i].Value = paramInstance.Value;
 108                     }
 109                 }
 110                 else if (parameterValues[i] == null)
 111                 {
 112                     commandParameters[i].Value = DBNull.Value;
 113                 }
 114                 else
 115                 {
 116                     commandParameters[i].Value = parameterValues[i];
 117                 }
 118             }
 119         }
 120
 121         /// <summary>
 122         /// 预处理用户提供的命令,数据库连接/事务/命令类型/参数
 123         /// </summary>
 124         /// <param name="command">要处理的SqlCommand</param>
 125         /// <param name="connection">数据库连接</param>
 126         /// <param name="transaction">一个有效的事务或者是null值</param>
 127         /// <param name="commandType">命令类型 (存储过程,命令文本, 其它.)</param>
 128         /// <param name="commandText">存储过程名或都T-SQL命令文本</param>
 129         /// <param name="commandParameters">和命令相关联的SqlParameter参数数组,如果没有参数为‘null‘</param>
 130         /// <param name="mustCloseConnection"><c>true</c> 如果连接是打开的,则为true,其它情况下为false.</param>
 131         private static void PrepareCommand(SqlCommand command, SqlConnection connection, SqlTransaction transaction, CommandType commandType, string commandText, SqlParameter[] commandParameters, out bool mustCloseConnection)
 132         {
 133             if (command == null) throw new ArgumentNullException("command");
 134             if (commandText == null || commandText.Length == 0) throw new ArgumentNullException("commandText");
 135
 136             // If the provided connection is not open, we will open it
 137             if (connection.State != ConnectionState.Open)
 138             {
 139                 mustCloseConnection = true;
 140                 connection.Open();
 141             }
 142             else
 143             {
 144                 mustCloseConnection = false;
 145             }
 146
 147             // 给命令分配一个数据库连接.
 148             command.Connection = connection;
 149
 150             // 设置命令文本(存储过程名或SQL语句)
 151             command.CommandText = commandText;
 152
 153             // 分配事务
 154             if (transaction != null)
 155             {
 156                 if (transaction.Connection == null) throw new ArgumentException("The transaction was rollbacked or commited, please provide an open transaction.", "transaction");
 157                 command.Transaction = transaction;
 158             }
 159
 160             // 设置命令类型.
 161             command.CommandType = commandType;
 162
 163             // 分配命令参数
 164             if (commandParameters != null)
 165             {
 166                 AttachParameters(command, commandParameters);
 167             }
 168             return;
 169         }
 170
 171         #endregion 私有构造函数和方法结束
 172
 173         #region 数据库连接
 174         /// <summary>
 175         /// 一个有效的数据库连接字符串
 176         /// </summary>
 177         /// <returns></returns>
 178         public static string GetConnSting()
 179         {
 180             return ConfigurationManager.ConnectionStrings["ConStr"].ConnectionString;
 181         }
 182         /// <summary>
 183         /// 一个有效的数据库连接对象
 184         /// </summary>
 185         /// <returns></returns>
 186         public static SqlConnection GetConnection()
 187         {
 188             SqlConnection Connection = new SqlConnection(SqlHelper.GetConnSting());
 189             return Connection;
 190         }
 191         #endregion
 192
 193         #region ExecuteNonQuery命令
 194
 195         /// <summary>
 196         /// 执行指定连接字符串,类型的SqlCommand.
 197         /// </summary>
 198         /// <remarks>
 199         /// 示例:
 200         ///  int result = ExecuteNonQuery(connString, CommandType.StoredProcedure, "PublishOrders");
 201         /// </remarks>
 202         /// <param name="connectionString">一个有效的数据库连接字符串</param>
 203         /// <param name="commandType">命令类型 (存储过程,命令文本, 其它.)</param>
 204         /// <param name="commandText">存储过程名称或SQL语句</param>
 205         /// <returns>返回命令影响的行数</returns>
 206         public static int ExecuteNonQuery(string connectionString, CommandType commandType, string commandText)
 207         {
 208             return ExecuteNonQuery(connectionString, commandType, commandText, (SqlParameter[])null);
 209         }
 210
 211         /// <summary>
 212         /// 执行指定连接字符串,类型的SqlCommand.如果没有提供参数,不返回结果.
 213         /// </summary>
 214         /// <remarks>
 215         /// 示例:
 216         ///  int result = ExecuteNonQuery(connString, CommandType.StoredProcedure, "PublishOrders", new SqlParameter("@prodid", 24));
 217         /// </remarks>
 218         /// <param name="connectionString">一个有效的数据库连接字符串</param>
 219         /// <param name="commandType">命令类型 (存储过程,命令文本, 其它.)</param>
 220         /// <param name="commandText">存储过程名称或SQL语句</param>
 221         /// <param name="commandParameters">SqlParameter参数数组</param>
 222         /// <returns>返回命令影响的行数</returns>
 223         public static int ExecuteNonQuery(string connectionString, CommandType commandType, string commandText, params SqlParameter[] commandParameters)
 224         {
 225             if (connectionString == null || connectionString.Length == 0) throw new ArgumentNullException("connectionString");
 226
 227             using (SqlConnection connection = new SqlConnection(connectionString))
 228             {
 229                 connection.Open();
 230
 231                 return ExecuteNonQuery(connection, commandType, commandText, commandParameters);
 232             }
 233         }
 234
 235         /// <summary>
 236         /// 执行指定连接字符串的存储过程,将对象数组的值赋给存储过程参数,
 237         /// 此方法需要在参数缓存方法中探索参数并生成参数.
 238         /// </summary>
 239         /// <remarks>
 240         /// 这个方法没有提供访问输出参数和返回值.
 241         /// 示例:
 242         ///  int result = ExecuteNonQuery(connString, "PublishOrders", 24, 36);
 243         /// </remarks>
 244         /// <param name="connectionString">一个有效的数据库连接字符串/param>
 245         /// <param name="spName">存储过程名称</param>
 246         /// <param name="parameterValues">分配到存储过程输入参数的对象数组</param>
 247         /// <returns>返回受影响的行数</returns>
 248         public static int ExecuteNonQuery(string connectionString, string spName, params object[] parameterValues)
 249         {
 250             if (connectionString == null || connectionString.Length == 0) throw new ArgumentNullException("connectionString");
 251             if (spName == null || spName.Length == 0) throw new ArgumentNullException("spName");
 252
 253             // 如果存在参数值
 254             if ((parameterValues != null) && (parameterValues.Length > 0))
 255             {
 256                 // 从探索存储过程参数(加载到缓存)并分配给存储过程参数数组.
 257                 SqlParameter[] commandParameters = SqlHelperParameterCache.GetSpParameterSet(connectionString, spName);
 258
 259                 // 给存储过程参数赋值
 260                 AssignParameterValues(commandParameters, parameterValues);
 261
 262                 return ExecuteNonQuery(connectionString, CommandType.StoredProcedure, spName, commandParameters);
 263             }
 264             else
 265             {
 266                 // 没有参数情况下
 267                 return ExecuteNonQuery(connectionString, CommandType.StoredProcedure, spName);
 268             }
 269         }
 270
 271         /// <summary>
 272         /// 执行指定数据库连接对象的命令
 273         /// </summary>
 274         /// <remarks>
 275         /// 示例:
 276         ///  int result = ExecuteNonQuery(conn, CommandType.StoredProcedure, "PublishOrders");
 277         /// </remarks>
 278         /// <param name="connection">一个有效的数据库连接对象</param>
 279         /// <param name="commandType">命令类型(存储过程,命令文本或其它.)</param>
 280         /// <param name="commandText">存储过程名称或T-SQL语句</param>
 281         /// <returns>返回影响的行数</returns>
 282         public static int ExecuteNonQuery(SqlConnection connection, CommandType commandType, string commandText)
 283         {
 284             return ExecuteNonQuery(connection, commandType, commandText, (SqlParameter[])null);
 285         }
 286
 287         /// <summary>
 288         /// 执行指定数据库连接对象的命令
 289         /// </summary>
 290         /// <remarks>
 291         /// 示例:
 292         ///  int result = ExecuteNonQuery(conn, CommandType.StoredProcedure, "PublishOrders", new SqlParameter("@prodid", 24));
 293         /// </remarks>
 294         /// <param name="connection">一个有效的数据库连接对象</param>
 295         /// <param name="commandType">命令类型(存储过程,命令文本或其它.)</param>
 296         /// <param name="commandText">T存储过程名称或T-SQL语句</param>
 297         /// <param name="commandParameters">SqlParamter参数数组</param>
 298         /// <returns>返回影响的行数</returns>
 299         public static int ExecuteNonQuery(SqlConnection connection, CommandType commandType, string commandText, params SqlParameter[] commandParameters)
 300         {
 301             if (connection == null) throw new ArgumentNullException("connection");
 302
 303             // 创建SqlCommand命令,并进行预处理
 304             SqlCommand cmd = new SqlCommand();
 305             bool mustCloseConnection = false;
 306             PrepareCommand(cmd, connection, (SqlTransaction)null, commandType, commandText, commandParameters, out mustCloseConnection);
 307
 308             // Finally, execute the command
 309             int retval = cmd.ExecuteNonQuery();
 310
 311             // 清除参数,以便再次使用.
 312             cmd.Parameters.Clear();
 313             if (mustCloseConnection)
 314                 connection.Close();
 315             return retval;
 316         }
 317
 318         /// <summary>
 319         /// 执行指定数据库连接对象的命令,将对象数组的值赋给存储过程参数.
 320         /// </summary>
 321         /// <remarks>
 322         /// 此方法不提供访问存储过程输出参数和返回值
 323         /// 示例:
 324         ///  int result = ExecuteNonQuery(conn, "PublishOrders", 24, 36);
 325         /// </remarks>
 326         /// <param name="connection">一个有效的数据库连接对象</param>
 327         /// <param name="spName">存储过程名</param>
 328         /// <param name="parameterValues">分配给存储过程输入参数的对象数组</param>
 329         /// <returns>返回影响的行数</returns>
 330         public static int ExecuteNonQuery(SqlConnection connection, string spName, params object[] parameterValues)
 331         {
 332             if (connection == null) throw new ArgumentNullException("connection");
 333             if (spName == null || spName.Length == 0) throw new ArgumentNullException("spName");
 334
 335             // 如果有参数值
 336             if ((parameterValues != null) && (parameterValues.Length > 0))
 337             {
 338                 // 从缓存中加载存储过程参数
 339                 SqlParameter[] commandParameters = SqlHelperParameterCache.GetSpParameterSet(connection, spName);
 340
 341                 // 给存储过程分配参数值
 342                 AssignParameterValues(commandParameters, parameterValues);
 343
 344                 return ExecuteNonQuery(connection, CommandType.StoredProcedure, spName, commandParameters);
 345             }
 346             else
 347             {
 348                 return ExecuteNonQuery(connection, CommandType.StoredProcedure, spName);
 349             }
 350         }
 351
 352         /// <summary>
 353         /// 执行带事务的SqlCommand.
 354         /// </summary>
 355         /// <remarks>
 356         /// 示例.:
 357         ///  int result = ExecuteNonQuery(trans, CommandType.StoredProcedure, "PublishOrders");
 358         /// </remarks>
 359         /// <param name="transaction">一个有效的数据库连接对象</param>
 360         /// <param name="commandType">命令类型(存储过程,命令文本或其它.)</param>
 361         /// <param name="commandText">存储过程名称或T-SQL语句</param>
 362         /// <returns>返回影响的行数/returns>
 363         public static int ExecuteNonQuery(SqlTransaction transaction, CommandType commandType, string commandText)
 364         {
 365             return ExecuteNonQuery(transaction, commandType, commandText, (SqlParameter[])null);
 366         }
 367
 368         /// <summary>
 369         /// 执行带事务的SqlCommand(指定参数).
 370         /// </summary>
 371         /// <remarks>
 372         /// 示例:
 373         ///  int result = ExecuteNonQuery(trans, CommandType.StoredProcedure, "GetOrders", new SqlParameter("@prodid", 24));
 374         /// </remarks>
 375         /// <param name="transaction">一个有效的数据库连接对象</param>
 376         /// <param name="commandType">命令类型(存储过程,命令文本或其它.)</param>
 377         /// <param name="commandText">存储过程名称或T-SQL语句</param>
 378         /// <param name="commandParameters">SqlParamter参数数组</param>
 379         /// <returns>返回影响的行数</returns>
 380         public static int ExecuteNonQuery(SqlTransaction transaction, CommandType commandType, string commandText, params SqlParameter[] commandParameters)
 381         {
 382             if (transaction == null) throw new ArgumentNullException("transaction");
 383             if (transaction != null && transaction.Connection == null) throw new ArgumentException("The transaction was rollbacked or commited, please provide an open transaction.", "transaction");
 384
 385             // 预处理
 386             SqlCommand cmd = new SqlCommand();
 387             bool mustCloseConnection = false;
 388             PrepareCommand(cmd, transaction.Connection, transaction, commandType, commandText, commandParameters, out mustCloseConnection);
 389
 390             // 执行
 391             int retval = cmd.ExecuteNonQuery();
 392
 393             // 清除参数集,以便再次使用.
 394             cmd.Parameters.Clear();
 395             return retval;
 396         }
 397
 398         /// <summary>
 399         /// 执行带事务的SqlCommand(指定参数值).
 400         /// </summary>
 401         /// <remarks>
 402         /// 此方法不提供访问存储过程输出参数和返回值
 403         /// 示例:
 404         ///  int result = ExecuteNonQuery(conn, trans, "PublishOrders", 24, 36);
 405         /// </remarks>
 406         /// <param name="transaction">一个有效的数据库连接对象</param>
 407         /// <param name="spName">存储过程名</param>
 408         /// <param name="parameterValues">分配给存储过程输入参数的对象数组</param>
 409         /// <returns>返回受影响的行数</returns>
 410         public static int ExecuteNonQuery(SqlTransaction transaction, string spName, params object[] parameterValues)
 411         {
 412             if (transaction == null) throw new ArgumentNullException("transaction");
 413             if (transaction != null && transaction.Connection == null) throw new ArgumentException("The transaction was rollbacked or commited, please provide an open transaction.", "transaction");
 414             if (spName == null || spName.Length == 0) throw new ArgumentNullException("spName");
 415
 416             // 如果有参数值
 417             if ((parameterValues != null) && (parameterValues.Length > 0))
 418             {
 419                 // 从缓存中加载存储过程参数,如果缓存中不存在则从数据库中检索参数信息并加载到缓存中. ()
 420                 SqlParameter[] commandParameters = SqlHelperParameterCache.GetSpParameterSet(transaction.Connection, spName);
 421
 422                 // 给存储过程参数赋值
 423                 AssignParameterValues(commandParameters, parameterValues);
 424
 425                 // 调用重载方法
 426                 return ExecuteNonQuery(transaction, CommandType.StoredProcedure, spName, commandParameters);
 427             }
 428             else
 429             {
 430                 // 没有参数值
 431                 return ExecuteNonQuery(transaction, CommandType.StoredProcedure, spName);
 432             }
 433         }
 434
 435         #endregion ExecuteNonQuery方法结束
 436
 437         #region ExecuteDataset方法
 438
 439         /// <summary>
 440         /// 执行指定数据库连接字符串的命令,返回DataSet.
 441         /// </summary>
 442         /// <remarks>
 443         /// 示例:
 444         ///  DataSet ds = ExecuteDataset(connString, CommandType.StoredProcedure, "GetOrders");
 445         /// </remarks>
 446         /// <param name="connectionString">一个有效的数据库连接字符串</param>
 447         /// <param name="commandType">命令类型 (存储过程,命令文本或其它)</param>
 448         /// <param name="commandText">存储过程名称或T-SQL语句</param>
 449         /// <returns>返回一个包含结果集的DataSet</returns>
 450         public static DataSet ExecuteDataset(string connectionString, CommandType commandType, string commandText)
 451         {
 452             return ExecuteDataset(connectionString, commandType, commandText, (SqlParameter[])null);
 453         }
 454
 455         /// <summary>
 456         /// 执行指定数据库连接字符串的命令,返回DataSet.
 457         /// </summary>
 458         /// <remarks>
 459         /// 示例:
 460         ///  DataSet ds = ExecuteDataset(connString, CommandType.StoredProcedure, "GetOrders", new SqlParameter("@prodid", 24));
 461         /// </remarks>
 462         /// <param name="connectionString">一个有效的数据库连接字符串</param>
 463         /// <param name="commandType">命令类型 (存储过程,命令文本或其它)</param>
 464         /// <param name="commandText">存储过程名称或T-SQL语句</param>
 465         /// <param name="commandParameters">SqlParamters参数数组</param>
 466         /// <returns>返回一个包含结果集的DataSet</returns>
 467         public static DataSet ExecuteDataset(string connectionString, CommandType commandType, string commandText, params SqlParameter[] commandParameters)
 468         {
 469             if (connectionString == null || connectionString.Length == 0) throw new ArgumentNullException("connectionString");
 470
 471             // 创建并打开数据库连接对象,操作完成释放对象.
 472             using (SqlConnection connection = new SqlConnection(connectionString))
 473             {
 474                 connection.Open();
 475
 476                 // 调用指定数据库连接字符串重载方法.
 477                 return ExecuteDataset(connection, commandType, commandText, commandParameters);
 478             }
 479         }
 480
 481         /// <summary>
 482         /// 执行指定数据库连接字符串的命令,直接提供参数值,返回DataSet.
 483         /// </summary>
 484         /// <remarks>
 485         /// 此方法不提供访问存储过程输出参数和返回值.
 486         /// 示例:
 487         ///  DataSet ds = ExecuteDataset(connString, "GetOrders", 24, 36);
 488         /// </remarks>
 489         /// <param name="connectionString">一个有效的数据库连接字符串</param>
 490         /// <param name="spName">存储过程名</param>
 491         /// <param name="parameterValues">分配给存储过程输入参数的对象数组</param>
 492         /// <returns>返回一个包含结果集的DataSet</returns>
 493         public static DataSet ExecuteDataset(string connectionString, string spName, params object[] parameterValues)
 494         {
 495             if (connectionString == null || connectionString.Length == 0) throw new ArgumentNullException("connectionString");
 496             if (spName == null || spName.Length == 0) throw new ArgumentNullException("spName");
 497
 498             if ((parameterValues != null) && (parameterValues.Length > 0))
 499             {
 500                 // 从缓存中检索存储过程参数
 501                 SqlParameter[] commandParameters = SqlHelperParameterCache.GetSpParameterSet(connectionString, spName);
 502
 503                 // 给存储过程参数分配值
 504                 AssignParameterValues(commandParameters, parameterValues);
 505
 506                 return ExecuteDataset(connectionString, CommandType.StoredProcedure, spName, commandParameters);
 507             }
 508             else
 509             {
 510                 return ExecuteDataset(connectionString, CommandType.StoredProcedure, spName);
 511             }
 512         }
 513
 514         /// <summary>
 515         /// 执行指定数据库连接对象的命令,返回DataSet.
 516         /// </summary>
 517         /// <remarks>
 518         /// 示例:
 519         ///  DataSet ds = ExecuteDataset(conn, CommandType.StoredProcedure, "GetOrders");
 520         /// </remarks>
 521         /// <param name="connection">一个有效的数据库连接对象</param>
 522         /// <param name="commandType">命令类型 (存储过程,命令文本或其它)</param>
 523         /// <param name="commandText">存储过程名或T-SQL语句</param>
 524         /// <returns>返回一个包含结果集的DataSet</returns>
 525         public static DataSet ExecuteDataset(SqlConnection connection, CommandType commandType, string commandText)
 526         {
 527             return ExecuteDataset(connection, commandType, commandText, (SqlParameter[])null);
 528         }
 529
 530         /// <summary>
 531         /// 执行指定数据库连接对象的命令,指定存储过程参数,返回DataSet.
 532         /// </summary>
 533         /// <remarks>
 534         /// 示例:
 535         ///  DataSet ds = ExecuteDataset(conn, CommandType.StoredProcedure, "GetOrders", new SqlParameter("@prodid", 24));
 536         /// </remarks>
 537         /// <param name="connection">一个有效的数据库连接对象</param>
 538         /// <param name="commandType">命令类型 (存储过程,命令文本或其它)</param>
 539         /// <param name="commandText">存储过程名或T-SQL语句</param>
 540         /// <param name="commandParameters">SqlParamter参数数组</param>
 541         /// <returns>返回一个包含结果集的DataSet</returns>
 542         public static DataSet ExecuteDataset(SqlConnection connection, CommandType commandType, string commandText, params SqlParameter[] commandParameters)
 543         {
 544             if (connection == null) throw new ArgumentNullException("connection");
 545
 546             // 预处理
 547             SqlCommand cmd = new SqlCommand();
 548             bool mustCloseConnection = false;
 549             PrepareCommand(cmd, connection, (SqlTransaction)null, commandType, commandText, commandParameters, out mustCloseConnection);
 550
 551             // 创建SqlDataAdapter和DataSet.
 552             using (SqlDataAdapter da = new SqlDataAdapter(cmd))
 553             {
 554                 DataSet ds = new DataSet();
 555
 556                 // 填充DataSet.
 557                 da.Fill(ds);
 558
 559                 cmd.Parameters.Clear();
 560
 561                 if (mustCloseConnection)
 562                     connection.Close();
 563
 564                 return ds;
 565             }
 566         }
 567
 568         /// <summary>
 569         /// 执行指定数据库连接对象的命令,指定参数值,返回DataSet.
 570         /// </summary>
 571         /// <remarks>
 572         /// 此方法不提供访问存储过程输入参数和返回值.
 573         /// 示例.:
 574         ///  DataSet ds = ExecuteDataset(conn, "GetOrders", 24, 36);
 575         /// </remarks>
 576         /// <param name="connection">一个有效的数据库连接对象</param>
 577         /// <param name="spName">存储过程名</param>
 578         /// <param name="parameterValues">分配给存储过程输入参数的对象数组</param>
 579         /// <returns>返回一个包含结果集的DataSet</returns>
 580         public static DataSet ExecuteDataset(SqlConnection connection, string spName, params object[] parameterValues)
 581         {
 582             if (connection == null) throw new ArgumentNullException("connection");
 583             if (spName == null || spName.Length == 0) throw new ArgumentNullException("spName");
 584
 585             if ((parameterValues != null) && (parameterValues.Length > 0))
 586             {
 587                 // 比缓存中加载存储过程参数
 588                 SqlParameter[] commandParameters = SqlHelperParameterCache.GetSpParameterSet(connection, spName);
 589
 590                 // 给存储过程参数分配值
 591                 AssignParameterValues(commandParameters, parameterValues);
 592
 593                 return ExecuteDataset(connection, CommandType.StoredProcedure, spName, commandParameters);
 594             }
 595             else
 596             {
 597                 return ExecuteDataset(connection, CommandType.StoredProcedure, spName);
 598             }
 599         }
 600
 601         /// <summary>
 602         /// 执行指定事务的命令,返回DataSet.
 603         /// </summary>
 604         /// <remarks>
 605         /// 示例:
 606         ///  DataSet ds = ExecuteDataset(trans, CommandType.StoredProcedure, "GetOrders");
 607         /// </remarks>
 608         /// <param name="transaction">事务</param>
 609         /// <param name="commandType">命令类型 (存储过程,命令文本或其它)</param>
 610         /// <param name="commandText">存储过程名或T-SQL语句</param>
 611         /// <returns>返回一个包含结果集的DataSet</returns>
 612         public static DataSet ExecuteDataset(SqlTransaction transaction, CommandType commandType, string commandText)
 613         {
 614             return ExecuteDataset(transaction, commandType, commandText, (SqlParameter[])null);
 615         }
 616
 617         /// <summary>
 618         /// 执行指定事务的命令,指定参数,返回DataSet.
 619         /// </summary>
 620         /// <remarks>
 621         /// 示例:
 622         ///  DataSet ds = ExecuteDataset(trans, CommandType.StoredProcedure, "GetOrders", new SqlParameter("@prodid", 24));
 623         /// </remarks>
 624         /// <param name="transaction">事务</param>
 625         /// <param name="commandType">命令类型 (存储过程,命令文本或其它)</param>
 626         /// <param name="commandText">存储过程名或T-SQL语句</param>
 627         /// <param name="commandParameters">SqlParamter参数数组</param>
 628         /// <returns>返回一个包含结果集的DataSet</returns>
 629         public static DataSet ExecuteDataset(SqlTransaction transaction, CommandType commandType, string commandText, params SqlParameter[] commandParameters)
 630         {
 631             if (transaction == null) throw new ArgumentNullException("transaction");
 632             if (transaction != null && transaction.Connection == null) throw new ArgumentException("The transaction was rollbacked or commited, please provide an open transaction.", "transaction");
 633
 634             // 预处理
 635             SqlCommand cmd = new SqlCommand();
 636             bool mustCloseConnection = false;
 637             PrepareCommand(cmd, transaction.Connection, transaction, commandType, commandText, commandParameters, out mustCloseConnection);
 638
 639             // 创建 DataAdapter & DataSet
 640             using (SqlDataAdapter da = new SqlDataAdapter(cmd))
 641             {
 642                 DataSet ds = new DataSet();
 643                 da.Fill(ds);
 644                 cmd.Parameters.Clear();
 645                 return ds;
 646             }
 647         }
 648
 649         /// <summary>
 650         /// 执行指定事务的命令,指定参数值,返回DataSet.
 651         /// </summary>
 652         /// <remarks>
 653         /// 此方法不提供访问存储过程输入参数和返回值.
 654         /// 示例.:
 655         ///  DataSet ds = ExecuteDataset(trans, "GetOrders", 24, 36);
 656         /// </remarks>
 657         /// <param name="transaction">事务</param>
 658         /// <param name="spName">存储过程名</param>
 659         /// <param name="parameterValues">分配给存储过程输入参数的对象数组</param>
 660         /// <returns>返回一个包含结果集的DataSet</returns>
 661         public static DataSet ExecuteDataset(SqlTransaction transaction, string spName, params object[] parameterValues)
 662         {
 663             if (transaction == null) throw new ArgumentNullException("transaction");
 664             if (transaction != null && transaction.Connection == null) throw new ArgumentException("The transaction was rollbacked or commited, please provide an open transaction.", "transaction");
 665             if (spName == null || spName.Length == 0) throw new ArgumentNullException("spName");
 666
 667             if ((parameterValues != null) && (parameterValues.Length > 0))
 668             {
 669                 // 从缓存中加载存储过程参数
 670                 SqlParameter[] commandParameters = SqlHelperParameterCache.GetSpParameterSet(transaction.Connection, spName);
 671
 672                 // 给存储过程参数分配值
 673                 AssignParameterValues(commandParameters, parameterValues);
 674
 675                 return ExecuteDataset(transaction, CommandType.StoredProcedure, spName, commandParameters);
 676             }
 677             else
 678             {
 679                 return ExecuteDataset(transaction, CommandType.StoredProcedure, spName);
 680             }
 681         }
 682
 683         #endregion ExecuteDataset数据集命令结束
 684
 685         #region ExecuteReader 数据阅读器
 686
 687         /// <summary>
 688         /// 枚举,标识数据库连接是由SqlHelper提供还是由调用者提供
 689         /// </summary>
 690         private enum SqlConnectionOwnership
 691         {
 692             /// <summary>由SqlHelper提供连接</summary>
 693             Internal,
 694             /// <summary>由调用者提供连接</summary>
 695             External
 696         }
 697
 698         /// <summary>
 699         /// 执行指定数据库连接对象的数据阅读器.
 700         /// </summary>
 701         /// <remarks>
 702         /// 如果是SqlHelper打开连接,当连接关闭DataReader也将关闭.
 703         /// 如果是调用都打开连接,DataReader由调用都管理.
 704         /// </remarks>
 705         /// <param name="connection">一个有效的数据库连接对象</param>
 706         /// <param name="transaction">一个有效的事务,或者为 ‘null‘</param>
 707         /// <param name="commandType">命令类型 (存储过程,命令文本或其它)</param>
 708         /// <param name="commandText">存储过程名或T-SQL语句</param>
 709         /// <param name="commandParameters">SqlParameters参数数组,如果没有参数则为‘null‘</param>
 710         /// <param name="connectionOwnership">标识数据库连接对象是由调用者提供还是由SqlHelper提供</param>
 711         /// <returns>返回包含结果集的SqlDataReader</returns>
 712         private static SqlDataReader ExecuteReader(SqlConnection connection, SqlTransaction transaction, CommandType commandType, string commandText, SqlParameter[] commandParameters, SqlConnectionOwnership connectionOwnership)
 713         {
 714             if (connection == null) throw new ArgumentNullException("connection");
 715
 716             bool mustCloseConnection = false;
 717             // 创建命令
 718             SqlCommand cmd = new SqlCommand();
 719             try
 720             {
 721                 PrepareCommand(cmd, connection, transaction, commandType, commandText, commandParameters, out mustCloseConnection);
 722
 723                 // 创建数据阅读器
 724                 SqlDataReader dataReader;
 725
 726                 if (connectionOwnership == SqlConnectionOwnership.External)
 727                 {
 728                     dataReader = cmd.ExecuteReader();
 729                 }
 730                 else
 731                 {
 732                     dataReader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
 733                 }
 734
 735                 // 清除参数,以便再次使用..
 736                 // HACK: There is a problem here, the output parameter values are fletched
 737                 // when the reader is closed, so if the parameters are detached from the command
 738                 // then the SqlReader can磘 set its values.
 739                 // When this happen, the parameters can磘 be used again in other command.
 740                 bool canClear = true;
 741                 foreach (SqlParameter commandParameter in cmd.Parameters)
 742                 {
 743                     if (commandParameter.Direction != ParameterDirection.Input)
 744                         canClear = false;
 745                 }
 746
 747                 if (canClear)
 748                 {
 749                     cmd.Parameters.Clear();
 750                 }
 751
 752                 return dataReader;
 753             }
 754             catch
 755             {
 756                 if (mustCloseConnection)
 757                     connection.Close();
 758                 throw;
 759             }
 760         }
 761
 762         /// <summary>
 763         /// 执行指定数据库连接字符串的数据阅读器.
 764         /// </summary>
 765         /// <remarks>
 766         /// 示例:
 767         ///  SqlDataReader dr = ExecuteReader(connString, CommandType.StoredProcedure, "GetOrders");
 768         /// </remarks>
 769         /// <param name="connectionString">一个有效的数据库连接字符串</param>
 770         /// <param name="commandType">命令类型 (存储过程,命令文本或其它)</param>
 771         /// <param name="commandText">存储过程名或T-SQL语句</param>
 772         /// <returns>返回包含结果集的SqlDataReader</returns>
 773         public static SqlDataReader ExecuteReader(string connectionString, CommandType commandType, string commandText)
 774         {
 775             return ExecuteReader(connectionString, commandType, commandText, (SqlParameter[])null);
 776         }
 777
 778         /// <summary>
 779         /// 执行指定数据库连接字符串的数据阅读器,指定参数.
 780         /// </summary>
 781         /// <remarks>
 782         /// 示例:
 783         ///  SqlDataReader dr = ExecuteReader(connString, CommandType.StoredProcedure, "GetOrders", new SqlParameter("@prodid", 24));
 784         /// </remarks>
 785         /// <param name="connectionString">一个有效的数据库连接字符串</param>
 786         /// <param name="commandType">命令类型 (存储过程,命令文本或其它)</param>
 787         /// <param name="commandText">存储过程名或T-SQL语句</param>
 788         /// <param name="commandParameters">SqlParamter参数数组(new SqlParameter("@prodid", 24))</param>
 789         /// <returns>返回包含结果集的SqlDataReader</returns>
 790         public static SqlDataReader ExecuteReader(string connectionString, CommandType commandType, string commandText, params SqlParameter[] commandParameters)
 791         {
 792             if (connectionString == null || connectionString.Length == 0) throw new ArgumentNullException("connectionString");
 793             SqlConnection connection = null;
 794             try
 795             {
 796                 connection = new SqlConnection(connectionString);
 797                 connection.Open();
 798
 799                 return ExecuteReader(connection, null, commandType, commandText, commandParameters, SqlConnectionOwnership.Internal);
 800             }
 801             catch
 802             {
 803                 // If we fail to return the SqlDatReader, we need to close the connection ourselves
 804                 if (connection != null) connection.Close();
 805                 throw;
 806             }
 807
 808         }
 809
 810         /// <summary>
 811         /// 执行指定数据库连接字符串的数据阅读器,指定参数值.
 812         /// </summary>
 813         /// <remarks>
 814         /// 此方法不提供访问存储过程输出参数和返回值参数.
 815         /// 示例:
 816         ///  SqlDataReader dr = ExecuteReader(connString, "GetOrders", 24, 36);
 817         /// </remarks>
 818         /// <param name="connectionString">一个有效的数据库连接字符串</param>
 819         /// <param name="spName">存储过程名</param>
 820         /// <param name="parameterValues">分配给存储过程输入参数的对象数组</param>
 821         /// <returns>返回包含结果集的SqlDataReader</returns>
 822         public static SqlDataReader ExecuteReader(string connectionString, string spName, params object[] parameterValues)
 823         {
 824             if (connectionString == null || connectionString.Length == 0) throw new ArgumentNullException("connectionString");
 825             if (spName == null || spName.Length == 0) throw new ArgumentNullException("spName");
 826
 827             if ((parameterValues != null) && (parameterValues.Length > 0))
 828             {
 829                 SqlParameter[] commandParameters = SqlHelperParameterCache.GetSpParameterSet(connectionString, spName);
 830
 831                 AssignParameterValues(commandParameters, parameterValues);
 832
 833                 return ExecuteReader(connectionString, CommandType.StoredProcedure, spName, commandParameters);
 834             }
 835             else
 836             {
 837                 return ExecuteReader(connectionString, CommandType.StoredProcedure, spName);
 838             }
 839         }
 840
 841         /// <summary>
 842         /// 执行指定数据库连接对象的数据阅读器.
 843         /// </summary>
 844         /// <remarks>
 845         /// 示例:
 846         ///  SqlDataReader dr = ExecuteReader(conn, CommandType.StoredProcedure, "GetOrders");
 847         /// </remarks>
 848         /// <param name="connection">一个有效的数据库连接对象</param>
 849         /// <param name="commandType">命令类型 (存储过程,命令文本或其它)</param>
 850         /// <param name="commandText">存储过程名或T-SQL语句</param>
 851         /// <returns>返回包含结果集的SqlDataReader</returns>
 852         public static SqlDataReader ExecuteReader(SqlConnection connection, CommandType commandType, string commandText)
 853         {
 854             return ExecuteReader(connection, commandType, commandText, (SqlParameter[])null);
 855         }
 856
 857         /// <summary>
 858         /// [调用者方式]执行指定数据库连接对象的数据阅读器,指定参数.
 859         /// </summary>
 860         /// <remarks>
 861         /// 示例:
 862         ///  SqlDataReader dr = ExecuteReader(conn, CommandType.StoredProcedure, "GetOrders", new SqlParameter("@prodid", 24));
 863         /// </remarks>
 864         /// <param name="connection">一个有效的数据库连接对象</param>
 865         /// <param name="commandType">命令类型 (存储过程,命令文本或其它)</param>
 866         /// <param name="commandText">命令类型 (存储过程,命令文本或其它)</param>
 867         /// <param name="commandParameters">SqlParamter参数数组</param>
 868         /// <returns>返回包含结果集的SqlDataReader</returns>
 869         public static SqlDataReader ExecuteReader(SqlConnection connection, CommandType commandType, string commandText, params SqlParameter[] commandParameters)
 870         {
 871             return ExecuteReader(connection, (SqlTransaction)null, commandType, commandText, commandParameters, SqlConnectionOwnership.External);
 872         }
 873
 874         /// <summary>
 875         /// [调用者方式]执行指定数据库连接对象的数据阅读器,指定参数值.
 876         /// </summary>
 877         /// <remarks>
 878         /// 此方法不提供访问存储过程输出参数和返回值参数.
 879         /// 示例:
 880         ///  SqlDataReader dr = ExecuteReader(conn, "GetOrders", 24, 36);
 881         /// </remarks>
 882         /// <param name="connection">一个有效的数据库连接对象</param>
 883         /// <param name="spName">T存储过程名</param>
 884         /// <param name="parameterValues">分配给存储过程输入参数的对象数组</param>
 885         /// <returns>返回包含结果集的SqlDataReader</returns>
 886         public static SqlDataReader ExecuteReader(SqlConnection connection, string spName, params object[] parameterValues)
 887         {
 888             if (connection == null) throw new ArgumentNullException("connection");
 889             if (spName == null || spName.Length == 0) throw new ArgumentNullException("spName");
 890
 891             if ((parameterValues != null) && (parameterValues.Length > 0))
 892             {
 893                 SqlParameter[] commandParameters = SqlHelperParameterCache.GetSpParameterSet(connection, spName);
 894
 895                 AssignParameterValues(commandParameters, parameterValues);
 896
 897                 return ExecuteReader(connection, CommandType.StoredProcedure, spName, commandParameters);
 898             }
 899             else
 900             {
 901                 return ExecuteReader(connection, CommandType.StoredProcedure, spName);
 902             }
 903         }
 904
 905         /// <summary>
 906         /// [调用者方式]执行指定数据库事务的数据阅读器,指定参数值.
 907         /// </summary>
 908         /// <remarks>
 909         /// 示例:
 910         ///  SqlDataReader dr = ExecuteReader(trans, CommandType.StoredProcedure, "GetOrders");
 911         /// </remarks>
 912         /// <param name="transaction">一个有效的连接事务</param>
 913         /// <param name="commandType">命令类型 (存储过程,命令文本或其它)</param>
 914         /// <param name="commandText">存储过程名称或T-SQL语句</param>
 915         /// <returns>返回包含结果集的SqlDataReader</returns>
 916         public static SqlDataReader ExecuteReader(SqlTransaction transaction, CommandType commandType, string commandText)
 917         {
 918             return ExecuteReader(transaction, commandType, commandText, (SqlParameter[])null);
 919         }
 920
 921         /// <summary>
 922         /// [调用者方式]执行指定数据库事务的数据阅读器,指定参数.
 923         /// </summary>
 924         /// <remarks>
 925         /// 示例:
 926         ///   SqlDataReader dr = ExecuteReader(trans, CommandType.StoredProcedure, "GetOrders", new SqlParameter("@prodid", 24));
 927         /// </remarks>
 928         /// <param name="transaction">一个有效的连接事务</param>
 929         /// <param name="commandType">命令类型 (存储过程,命令文本或其它)</param>
 930         /// <param name="commandText">存储过程名称或T-SQL语句</param>
 931         /// <param name="commandParameters">分配给命令的SqlParamter参数数组</param>
 932         /// <returns>返回包含结果集的SqlDataReader</returns>
 933         public static SqlDataReader ExecuteReader(SqlTransaction transaction, CommandType commandType, string commandText, params SqlParameter[] commandParameters)
 934         {
 935             if (transaction == null) throw new ArgumentNullException("transaction");
 936             if (transaction != null && transaction.Connection == null) throw new ArgumentException("The transaction was rollbacked or commited, please provide an open transaction.", "transaction");
 937
 938             return ExecuteReader(transaction.Connection, transaction, commandType, commandText, commandParameters, SqlConnectionOwnership.External);
 939         }
 940
 941         /// <summary>
 942         /// [调用者方式]执行指定数据库事务的数据阅读器,指定参数值.
 943         /// </summary>
 944         /// <remarks>
 945         /// 此方法不提供访问存储过程输出参数和返回值参数.
 946         ///
 947         /// 示例:
 948         ///  SqlDataReader dr = ExecuteReader(trans, "GetOrders", 24, 36);
 949         /// </remarks>
 950         /// <param name="transaction">一个有效的连接事务</param>
 951         /// <param name="spName">存储过程名称</param>
 952         /// <param name="parameterValues">分配给存储过程输入参数的对象数组</param>
 953         /// <returns>返回包含结果集的SqlDataReader</returns>
 954         public static SqlDataReader ExecuteReader(SqlTransaction transaction, string spName, params object[] parameterValues)
 955         {
 956             if (transaction == null) throw new ArgumentNullException("transaction");
 957             if (transaction != null && transaction.Connection == null) throw new ArgumentException("The transaction was rollbacked or commited, please provide an open transaction.", "transaction");
 958             if (spName == null || spName.Length == 0) throw new ArgumentNullException("spName");
 959
 960             // 如果有参数值
 961             if ((parameterValues != null) && (parameterValues.Length > 0))
 962             {
 963                 SqlParameter[] commandParameters = SqlHelperParameterCache.GetSpParameterSet(transaction.Connection, spName);
 964
 965                 AssignParameterValues(commandParameters, parameterValues);
 966
 967                 return ExecuteReader(transaction, CommandType.StoredProcedure, spName, commandParameters);
 968             }
 969             else
 970             {
 971                 // 没有参数值
 972                 return ExecuteReader(transaction, CommandType.StoredProcedure, spName);
 973             }
 974         }
 975
 976         #endregion ExecuteReader数据阅读器
 977
 978         #region ExecuteScalar 返回结果集中的第一行第一列
 979
 980         /// <summary>
 981         /// 执行指定数据库连接字符串的命令,返回结果集中的第一行第一列.
 982         /// </summary>
 983         /// <remarks>
 984         /// 示例:
 985         ///  int orderCount = (int)ExecuteScalar(connString, CommandType.StoredProcedure, "GetOrderCount");
 986         /// </remarks>
 987         /// <param name="connectionString">一个有效的数据库连接字符串</param>
 988         /// <param name="commandType">命令类型 (存储过程,命令文本或其它)</param>
 989         /// <param name="commandText">存储过程名称或T-SQL语句</param>
 990         /// <returns>返回结果集中的第一行第一列</returns>
 991         public static object ExecuteScalar(string connectionString, CommandType commandType, string commandText)
 992         {
 993             // 执行参数为空的方法
 994             return ExecuteScalar(connectionString, commandType, commandText, (SqlParameter[])null);
 995         }
 996
 997         /// <summary>
 998         /// 执行指定数据库连接字符串的命令,指定参数,返回结果集中的第一行第一列.
 999         /// </summary>
1000         /// <remarks>
1001         /// 示例:
1002         ///  int orderCount = (int)ExecuteScalar(connString, CommandType.StoredProcedure, "GetOrderCount", new SqlParameter("@prodid", 24));
1003         /// </remarks>
1004         /// <param name="connectionString">一个有效的数据库连接字符串</param>
1005         /// <param name="commandType">命令类型 (存储过程,命令文本或其它)</param>
1006         /// <param name="commandText">存储过程名称或T-SQL语句</param>
1007         /// <param name="commandParameters">分配给命令的SqlParamter参数数组</param>
1008         /// <returns>返回结果集中的第一行第一列</returns>
1009         public static object ExecuteScalar(string connectionString, CommandType commandType, string commandText, params SqlParameter[] commandParameters)
1010         {
1011             if (connectionString == null || connectionString.Length == 0) throw new ArgumentNullException("connectionString");
1012             // 创建并打开数据库连接对象,操作完成释放对象.
1013             using (SqlConnection connection = new SqlConnection(connectionString))
1014             {
1015                 connection.Open();
1016
1017                 // 调用指定数据库连接字符串重载方法.
1018                 return ExecuteScalar(connection, commandType, commandText, commandParameters);
1019             }
1020         }
1021
1022         /// <summary>
1023         /// 执行指定数据库连接字符串的命令,指定参数值,返回结果集中的第一行第一列.
1024         /// </summary>
1025         /// <remarks>
1026         /// 此方法不提供访问存储过程输出参数和返回值参数.
1027         ///
1028         /// 示例:
1029         ///  int orderCount = (int)ExecuteScalar(connString, "GetOrderCount", 24, 36);
1030         /// </remarks>
1031         /// <param name="connectionString">一个有效的数据库连接字符串</param>
1032         /// <param name="spName">存储过程名称</param>
1033         /// <param name="parameterValues">分配给存储过程输入参数的对象数组</param>
1034         /// <returns>返回结果集中的第一行第一列</returns>
1035         public static object ExecuteScalar(string connectionString, string spName, params object[] parameterValues)
1036         {
1037             if (connectionString == null || connectionString.Length == 0) throw new ArgumentNullException("connectionString");
1038             if (spName == null || spName.Length == 0) throw new ArgumentNullException("spName");
1039
1040             // 如果有参数值
1041             if ((parameterValues != null) && (parameterValues.Length > 0))
1042             {
1043                 // 从缓存中加载存储过程参数,如果缓存中不存在则从数据库中检索参数信息并加载到缓存中. ()
1044                 SqlParameter[] commandParameters = SqlHelperParameterCache.GetSpParameterSet(connectionString, spName);
1045
1046                 // 给存储过程参数赋值
1047                 AssignParameterValues(commandParameters, parameterValues);
1048
1049                 // 调用重载方法
1050                 return ExecuteScalar(connectionString, CommandType.StoredProcedure, spName, commandParameters);
1051             }
1052             else
1053             {
1054                 // 没有参数值
1055                 return ExecuteScalar(connectionString, CommandType.StoredProcedure, spName);
1056             }
1057         }
1058
1059         /// <summary>
1060         /// 执行指定数据库连接对象的命令,返回结果集中的第一行第一列.
1061         /// </summary>
1062         /// <remarks>
1063         /// 示例:
1064         ///  int orderCount = (int)ExecuteScalar(conn, CommandType.StoredProcedure, "GetOrderCount");
1065         /// </remarks>
1066         /// <param name="connection">一个有效的数据库连接对象</param>
1067         /// <param name="commandType">命令类型 (存储过程,命令文本或其它)</param>
1068         /// <param name="commandText">存储过程名称或T-SQL语句</param>
1069         /// <returns>返回结果集中的第一行第一列</returns>
1070         public static object ExecuteScalar(SqlConnection connection, CommandType commandType, string commandText)
1071         {
1072             // 执行参数为空的方法
1073             return ExecuteScalar(connection, commandType, commandText, (SqlParameter[])null);
1074         }
1075
1076         /// <summary>
1077         /// 执行指定数据库连接对象的命令,指定参数,返回结果集中的第一行第一列.
1078         /// </summary>
1079         /// <remarks>
1080         /// 示例:
1081         ///  int orderCount = (int)ExecuteScalar(conn, CommandType.StoredProcedure, "GetOrderCount", new SqlParameter("@prodid", 24));
1082         /// </remarks>
1083         /// <param name="connection">一个有效的数据库连接对象</param>
1084         /// <param name="commandType">命令类型 (存储过程,命令文本或其它)</param>
1085         /// <param name="commandText">存储过程名称或T-SQL语句</param>
1086         /// <param name="commandParameters">分配给命令的SqlParamter参数数组</param>
1087         /// <returns>返回结果集中的第一行第一列</returns>
1088         public static object ExecuteScalar(SqlConnection connection, CommandType commandType, string commandText, params SqlParameter[] commandParameters)
1089         {
1090             if (connection == null) throw new ArgumentNullException("connection");
1091
1092             // 创建SqlCommand命令,并进行预处理
1093             SqlCommand cmd = new SqlCommand();
1094
1095             bool mustCloseConnection = false;
1096             PrepareCommand(cmd, connection, (SqlTransaction)null, commandType, commandText, commandParameters, out mustCloseConnection);
1097
1098             // 执行SqlCommand命令,并返回结果.
1099             object retval = cmd.ExecuteScalar();
1100
1101             // 清除参数,以便再次使用.
1102             cmd.Parameters.Clear();
1103
1104             if (mustCloseConnection)
1105                 connection.Close();
1106
1107             return retval;
1108         }
1109
1110         /// <summary>
1111         /// 执行指定数据库连接对象的命令,指定参数值,返回结果集中的第一行第一列.
1112         /// </summary>
1113         /// <remarks>
1114         /// 此方法不提供访问存储过程输出参数和返回值参数.
1115         ///
1116         /// 示例:
1117         ///  int orderCount = (int)ExecuteScalar(conn, "GetOrderCount", 24, 36);
1118         /// </remarks>
1119         /// <param name="connection">一个有效的数据库连接对象</param>
1120         /// <param name="spName">存储过程名称</param>
1121         /// <param name="parameterValues">分配给存储过程输入参数的对象数组</param>
1122         /// <returns>返回结果集中的第一行第一列</returns>
1123         public static object ExecuteScalar(SqlConnection connection, string spName, params object[] parameterValues)
1124         {
1125             if (connection == null) throw new ArgumentNullException("connection");
1126             if (spName == null || spName.Length == 0) throw new ArgumentNullException("spName");
1127
1128             // 如果有参数值
1129             if ((parameterValues != null) && (parameterValues.Length > 0))
1130             {
1131                 // 从缓存中加载存储过程参数,如果缓存中不存在则从数据库中检索参数信息并加载到缓存中. ()
1132                 SqlParameter[] commandParameters = SqlHelperParameterCache.GetSpParameterSet(connection, spName);
1133
1134                 // 给存储过程参数赋值
1135                 AssignParameterValues(commandParameters, parameterValues);
1136
1137                 // 调用重载方法
1138                 return ExecuteScalar(connection, CommandType.StoredProcedure, spName, commandParameters);
1139             }
1140             else
1141             {
1142                 // 没有参数值
1143                 return ExecuteScalar(connection, CommandType.StoredProcedure, spName);
1144             }
1145         }
1146
1147         /// <summary>
1148         /// 执行指定数据库事务的命令,返回结果集中的第一行第一列.
1149         /// </summary>
1150         /// <remarks>
1151         /// 示例:
1152         ///  int orderCount = (int)ExecuteScalar(trans, CommandType.StoredProcedure, "GetOrderCount");
1153         /// </remarks>
1154         /// <param name="transaction">一个有效的连接事务</param>
1155         /// <param name="commandType">命令类型 (存储过程,命令文本或其它)</param>
1156         /// <param name="commandText">存储过程名称或T-SQL语句</param>
1157         /// <returns>返回结果集中的第一行第一列</returns>
1158         public static object ExecuteScalar(SqlTransaction transaction, CommandType commandType, string commandText)
1159         {
1160             // 执行参数为空的方法
1161             return ExecuteScalar(transaction, commandType, commandText, (SqlParameter[])null);
1162         }
1163
1164         /// <summary>
1165         /// 执行指定数据库事务的命令,指定参数,返回结果集中的第一行第一列.
1166         /// </summary>
1167         /// <remarks>
1168         /// 示例:
1169         ///  int orderCount = (int)ExecuteScalar(trans, CommandType.StoredProcedure, "GetOrderCount", new SqlParameter("@prodid", 24));
1170         /// </remarks>
1171         /// <param name="transaction">一个有效的连接事务</param>
1172         /// <param name="commandType">命令类型 (存储过程,命令文本或其它)</param>
1173         /// <param name="commandText">存储过程名称或T-SQL语句</param>
1174         /// <param name="commandParameters">分配给命令的SqlParamter参数数组</param>
1175         /// <returns>返回结果集中的第一行第一列</returns>
1176         public static object ExecuteScalar(SqlTransaction transaction, CommandType commandType, string commandText, params SqlParameter[] commandParameters)
1177         {
1178             if (transaction == null) throw new ArgumentNullException("transaction");
1179             if (transaction != null && transaction.Connection == null) throw new ArgumentException("The transaction was rollbacked or commited, please provide an open transaction.", "transaction");
1180
1181             // 创建SqlCommand命令,并进行预处理
1182             SqlCommand cmd = new SqlCommand();
1183             bool mustCloseConnection = false;
1184             PrepareCommand(cmd, transaction.Connection, transaction, commandType, commandText, commandParameters, out mustCloseConnection);
1185
1186             // 执行SqlCommand命令,并返回结果.
1187             object retval = cmd.ExecuteScalar();
1188
1189             // 清除参数,以便再次使用.
1190             cmd.Parameters.Clear();
1191             return retval;
1192         }
1193
1194         /// <summary>
1195         /// 执行指定数据库事务的命令,指定参数值,返回结果集中的第一行第一列.
1196         /// </summary>
1197         /// <remarks>
1198         /// 此方法不提供访问存储过程输出参数和返回值参数.
1199         ///
1200         /// 示例:
1201         ///  int orderCount = (int)ExecuteScalar(trans, "GetOrderCount", 24, 36);
1202         /// </remarks>
1203         /// <param name="transaction">一个有效的连接事务</param>
1204         /// <param name="spName">存储过程名称</param>
1205         /// <param name="parameterValues">分配给存储过程输入参数的对象数组</param>
1206         /// <returns>返回结果集中的第一行第一列</returns>
1207         public static object ExecuteScalar(SqlTransaction transaction, string spName, params object[] parameterValues)
1208         {
1209             if (transaction == null) throw new ArgumentNullException("transaction");
1210             if (transaction != null && transaction.Connection == null) throw new ArgumentException("The transaction was rollbacked or commited, please provide an open transaction.", "transaction");
1211             if (spName == null || spName.Length == 0) throw new ArgumentNullException("spName");
1212
1213             // 如果有参数值
1214             if ((parameterValues != null) && (parameterValues.Length > 0))
1215             {
1216                 // PPull the parameters for this stored procedure from the parameter cache ()
1217                 SqlParameter[] commandParameters = SqlHelperParameterCache.GetSpParameterSet(transaction.Connection, spName);
1218
1219                 // 给存储过程参数赋值
1220                 AssignParameterValues(commandParameters, parameterValues);
1221
1222                 // 调用重载方法
1223                 return ExecuteScalar(transaction, CommandType.StoredProcedure, spName, commandParameters);
1224             }
1225             else
1226             {
1227                 // 没有参数值
1228                 return ExecuteScalar(transaction, CommandType.StoredProcedure, spName);
1229             }
1230         }
1231
1232         #endregion ExecuteScalar
1233
1234         #region ExecuteXmlReader XML阅读器
1235         /// <summary>
1236         /// 执行指定数据库连接对象的SqlCommand命令,并产生一个XmlReader对象做为结果集返回.
1237         /// </summary>
1238         /// <remarks>
1239         /// 示例:
1240         ///  XmlReader r = ExecuteXmlReader(conn, CommandType.StoredProcedure, "GetOrders");
1241         /// </remarks>
1242         /// <param name="connection">一个有效的数据库连接对象</param>
1243         /// <param name="commandType">命令类型 (存储过程,命令文本或其它)</param>
1244         /// <param name="commandText">存储过程名称或T-SQL语句 using "FOR XML AUTO"</param>
1245         /// <returns>返回XmlReader结果集对象.</returns>
1246         public static XmlReader ExecuteXmlReader(SqlConnection connection, CommandType commandType, string commandText)
1247         {
1248             // 执行参数为空的方法
1249             return ExecuteXmlReader(connection, commandType, commandText, (SqlParameter[])null);
1250         }
1251
1252         /// <summary>
1253         /// 执行指定数据库连接对象的SqlCommand命令,并产生一个XmlReader对象做为结果集返回,指定参数.
1254         /// </summary>
1255         /// <remarks>
1256         /// 示例:
1257         ///  XmlReader r = ExecuteXmlReader(conn, CommandType.StoredProcedure, "GetOrders", new SqlParameter("@prodid", 24));
1258         /// </remarks>
1259         /// <param name="connection">一个有效的数据库连接对象</param>
1260         /// <param name="commandType">命令类型 (存储过程,命令文本或其它)</param>
1261         /// <param name="commandText">存储过程名称或T-SQL语句 using "FOR XML AUTO"</param>
1262         /// <param name="commandParameters">分配给命令的SqlParamter参数数组</param>
1263         /// <returns>返回XmlReader结果集对象.</returns>
1264         public static XmlReader ExecuteXmlReader(SqlConnection connection, CommandType commandType, string commandText, params SqlParameter[] commandParameters)
1265         {
1266             if (connection == null) throw new ArgumentNullException("connection");
1267
1268             bool mustCloseConnection = false;
1269             // 创建SqlCommand命令,并进行预处理
1270             SqlCommand cmd = new SqlCommand();
1271             try
1272             {
1273                 PrepareCommand(cmd, connection, (SqlTransaction)null, commandType, commandText, commandParameters, out mustCloseConnection);
1274
1275                 // 执行命令
1276                 XmlReader retval = cmd.ExecuteXmlReader();
1277
1278                 // 清除参数,以便再次使用.
1279                 cmd.Parameters.Clear();
1280
1281                 return retval;
1282             }
1283             catch
1284             {
1285                 if (mustCloseConnection)
1286                     connection.Close();
1287                 throw;
1288             }
1289         }
1290
1291         /// <summary>
1292         /// 执行指定数据库连接对象的SqlCommand命令,并产生一个XmlReader对象做为结果集返回,指定参数值.
1293         /// </summary>
1294         /// <remarks>
1295         /// 此方法不提供访问存储过程输出参数和返回值参数.
1296         ///
1297         /// 示例:
1298         ///  XmlReader r = ExecuteXmlReader(conn, "GetOrders", 24, 36);
1299         /// </remarks>
1300         /// <param name="connection">一个有效的数据库连接对象</param>
1301         /// <param name="spName">存储过程名称 using "FOR XML AUTO"</param>
1302         /// <param name="parameterValues">分配给存储过程输入参数的对象数组</param>
1303         /// <returns>返回XmlReader结果集对象.</returns>
1304         public static XmlReader ExecuteXmlReader(SqlConnection connection, string spName, params object[] parameterValues)
1305         {
1306             if (connection == null) throw new ArgumentNullException("connection");
1307             if (spName == null || spName.Length == 0) throw new ArgumentNullException("spName");
1308
1309             // 如果有参数值
1310             if ((parameterValues != null) && (parameterValues.Length > 0))
1311             {
1312                 // 从缓存中加载存储过程参数,如果缓存中不存在则从数据库中检索参数信息并加载到缓存中. ()
1313                 SqlParameter[] commandParameters = SqlHelperParameterCache.GetSpParameterSet(connection, spName);
1314
1315                 // 给存储过程参数赋值
1316                 AssignParameterValues(commandParameters, parameterValues);
1317
1318                 // 调用重载方法
1319                 return ExecuteXmlReader(connection, CommandType.StoredProcedure, spName, commandParameters);
1320             }
1321             else
1322             {
1323                 // 没有参数值
1324                 return ExecuteXmlReader(connection, CommandType.StoredProcedure, spName);
1325             }
1326         }
1327
1328         /// <summary>
1329         /// 执行指定数据库事务的SqlCommand命令,并产生一个XmlReader对象做为结果集返回.
1330         /// </summary>
1331         /// <remarks>
1332         /// 示例:
1333         ///  XmlReader r = ExecuteXmlReader(trans, CommandType.StoredProcedure, "GetOrders");
1334         /// </remarks>
1335         /// <param name="transaction">一个有效的连接事务</param>
1336         /// <param name="commandType">命令类型 (存储过程,命令文本或其它)</param>
1337         /// <param name="commandText">存储过程名称或T-SQL语句 using "FOR XML AUTO"</param>
1338         /// <returns>返回XmlReader结果集对象.</returns>
1339         public static XmlReader ExecuteXmlReader(SqlTransaction transaction, CommandType commandType, string commandText)
1340         {
1341             // 执行参数为空的方法
1342             return ExecuteXmlReader(transaction, commandType, commandText, (SqlParameter[])null);
1343         }
1344
1345         /// <summary>
1346         /// 执行指定数据库事务的SqlCommand命令,并产生一个XmlReader对象做为结果集返回,指定参数.
1347         /// </summary>
1348         /// <remarks>
1349         /// 示例:
1350         ///  XmlReader r = ExecuteXmlReader(trans, CommandType.StoredProcedure, "GetOrders", new SqlParameter("@prodid", 24));
1351         /// </remarks>
1352         /// <param name="transaction">一个有效的连接事务</param>
1353         /// <param name="commandType">命令类型 (存储过程,命令文本或其它)</param>
1354         /// <param name="commandText">存储过程名称或T-SQL语句 using "FOR XML AUTO"</param>
1355         /// <param name="commandParameters">分配给命令的SqlParamter参数数组</param>
1356         /// <returns>返回XmlReader结果集对象.</returns>
1357         public static XmlReader ExecuteXmlReader(SqlTransaction transaction, CommandType commandType, string commandText, params SqlParameter[] commandParameters)
1358         {
1359             if (transaction == null) throw new ArgumentNullException("transaction");
1360             if (transaction != null && transaction.Connection == null) throw new ArgumentException("The transaction was rollbacked or commited, please provide an open transaction.", "transaction");
1361
1362             // 创建SqlCommand命令,并进行预处理
1363             SqlCommand cmd = new SqlCommand();
1364             bool mustCloseConnection = false;
1365             PrepareCommand(cmd, transaction.Connection, transaction, commandType, commandText, commandParameters, out mustCloseConnection);
1366
1367             // 执行命令
1368             XmlReader retval = cmd.ExecuteXmlReader();
1369
1370             // 清除参数,以便再次使用.
1371             cmd.Parameters.Clear();
1372             return retval;
1373         }
1374
1375         /// <summary>
1376         /// 执行指定数据库事务的SqlCommand命令,并产生一个XmlReader对象做为结果集返回,指定参数值.
1377         /// </summary>
1378         /// <remarks>
1379         /// 此方法不提供访问存储过程输出参数和返回值参数.
1380         ///
1381         /// 示例:
1382         ///  XmlReader r = ExecuteXmlReader(trans, "GetOrders", 24, 36);
1383         /// </remarks>
1384         /// <param name="transaction">一个有效的连接事务</param>
1385         /// <param name="spName">存储过程名称</param>
1386         /// <param name="parameterValues">分配给存储过程输入参数的对象数组</param>
1387         /// <returns>返回一个包含结果集的DataSet.</returns>
1388         public static XmlReader ExecuteXmlReader(SqlTransaction transaction, string spName, params object[] parameterValues)
1389         {
1390             if (transaction == null) throw new ArgumentNullException("transaction");
1391             if (transaction != null && transaction.Connection == null) throw new ArgumentException("The transaction was rollbacked or commited, please provide an open transaction.", "transaction");
1392             if (spName == null || spName.Length == 0) throw new ArgumentNullException("spName");
1393
1394             // 如果有参数值
1395             if ((parameterValues != null) && (parameterValues.Length > 0))
1396             {
1397                 // 从缓存中加载存储过程参数,如果缓存中不存在则从数据库中检索参数信息并加载到缓存中. ()
1398                 SqlParameter[] commandParameters = SqlHelperParameterCache.GetSpParameterSet(transaction.Connection, spName);
1399
1400                 // 给存储过程参数赋值
1401                 AssignParameterValues(commandParameters, parameterValues);
1402
1403                 // 调用重载方法
1404                 return ExecuteXmlReader(transaction, CommandType.StoredProcedure, spName, commandParameters);
1405             }
1406             else
1407             {
1408                 // 没有参数值
1409                 return ExecuteXmlReader(transaction, CommandType.StoredProcedure, spName);
1410             }
1411         }
1412
1413         #endregion ExecuteXmlReader 阅读器结束
1414
1415         #region FillDataset 填充数据集
1416         /// <summary>
1417         /// 执行指定数据库连接字符串的命令,映射数据表并填充数据集.
1418         /// </summary>
1419         /// <remarks>
1420         /// 示例:
1421         ///  FillDataset(connString, CommandType.StoredProcedure, "GetOrders", ds, new string[] {"orders"});
1422         /// </remarks>
1423         /// <param name="connectionString">一个有效的数据库连接字符串</param>
1424         /// <param name="commandType">命令类型 (存储过程,命令文本或其它)</param>
1425         /// <param name="commandText">存储过程名称或T-SQL语句</param>
1426         /// <param name="dataSet">要填充结果集的DataSet实例</param>
1427         /// <param name="tableNames">表映射的数据表数组
1428         /// 用户定义的表名 (可有是实际的表名.)</param>
1429         public static void FillDataset(string connectionString, CommandType commandType, string commandText, DataSet dataSet, string[] tableNames)
1430         {
1431             if (connectionString == null || connectionString.Length == 0) throw new ArgumentNullException("connectionString");
1432             if (dataSet == null) throw new ArgumentNullException("dataSet");
1433
1434             // 创建并打开数据库连接对象,操作完成释放对象.
1435             using (SqlConnection connection = new SqlConnection(connectionString))
1436             {
1437                 connection.Open();
1438
1439                 // 调用指定数据库连接字符串重载方法.
1440                 FillDataset(connection, commandType, commandText, dataSet, tableNames);
1441             }
1442         }
1443
1444         /// <summary>
1445         /// 执行指定数据库连接字符串的命令,映射数据表并填充数据集.指定命令参数.
1446         /// </summary>
1447         /// <remarks>
1448         /// 示例:
1449         ///  FillDataset(connString, CommandType.StoredProcedure, "GetOrders", ds, new string[] {"orders"}, new SqlParameter("@prodid", 24));
1450         /// </remarks>
1451         /// <param name="connectionString">一个有效的数据库连接字符串</param>
1452         /// <param name="commandType">命令类型 (存储过程,命令文本或其它)</param>
1453         /// <param name="commandText">存储过程名称或T-SQL语句</param>
1454         /// <param name="commandParameters">分配给命令的SqlParamter参数数组</param>
1455         /// <param name="dataSet">要填充结果集的DataSet实例</param>
1456         /// <param name="tableNames">表映射的数据表数组
1457         /// 用户定义的表名 (可有是实际的表名.)
1458         /// </param>
1459         public static void FillDataset(string connectionString, CommandType commandType,
1460             string commandText, DataSet dataSet, string[] tableNames,
1461             params SqlParameter[] commandParameters)
1462         {
1463             if (connectionString == null || connectionString.Length == 0) throw new ArgumentNullException("connectionString");
1464             if (dataSet == null) throw new ArgumentNullException("dataSet");
1465             // 创建并打开数据库连接对象,操作完成释放对象.
1466             using (SqlConnection connection = new SqlConnection(connectionString))
1467             {
1468                 connection.Open();
1469
1470                 // 调用指定数据库连接字符串重载方法.
1471                 FillDataset(connection, commandType, commandText, dataSet, tableNames, commandParameters);
1472             }
1473         }
1474
1475         /// <summary>
1476         /// 执行指定数据库连接字符串的命令,映射数据表并填充数据集,指定存储过程参数值.
1477         /// </summary>
1478         /// <remarks>
1479         /// 此方法不提供访问存储过程输出参数和返回值参数.
1480         ///
1481         /// 示例:
1482         ///  FillDataset(connString, CommandType.StoredProcedure, "GetOrders", ds, new string[] {"orders"}, 24);
1483         /// </remarks>
1484         /// <param name="connectionString">一个有效的数据库连接字符串</param>
1485         /// <param name="spName">存储过程名称</param>
1486         /// <param name="dataSet">要填充结果集的DataSet实例</param>
1487         /// <param name="tableNames">表映射的数据表数组
1488         /// 用户定义的表名 (可有是实际的表名.)
1489         /// </param>
1490         /// <param name="parameterValues">分配给存储过程输入参数的对象数组</param>
1491         public static void FillDataset(string connectionString, string spName,
1492             DataSet dataSet, string[] tableNames,
1493             params object[] parameterValues)
1494         {
1495             if (connectionString == null || connectionString.Length == 0) throw new ArgumentNullException("connectionString");
1496             if (dataSet == null) throw new ArgumentNullException("dataSet");
1497             // 创建并打开数据库连接对象,操作完成释放对象.
1498             using (SqlConnection connection = new SqlConnection(connectionString))
1499             {
1500                 connection.Open();
1501
1502                 // 调用指定数据库连接字符串重载方法.
1503                 FillDataset(connection, spName, dataSet, tableNames, parameterValues);
1504             }
1505         }
1506
1507         /// <summary>
1508         /// 执行指定数据库连接对象的命令,映射数据表并填充数据集.
1509         /// </summary>
1510         /// <remarks>
1511         /// 示例:
1512         ///  FillDataset(conn, CommandType.StoredProcedure, "GetOrders", ds, new string[] {"orders"});
1513         /// </remarks>
1514         /// <param name="connection">一个有效的数据库连接对象</param>
1515         /// <param name="commandType">命令类型 (存储过程,命令文本或其它)</param>
1516         /// <param name="commandText">存储过程名称或T-SQL语句</param>
1517         /// <param name="dataSet">要填充结果集的DataSet实例</param>
1518         /// <param name="tableNames">表映射的数据表数组
1519         /// 用户定义的表名 (可有是实际的表名.)
1520         /// </param>
1521         public static void FillDataset(SqlConnection connection, CommandType commandType,
1522             string commandText, DataSet dataSet, string[] tableNames)
1523         {
1524             FillDataset(connection, commandType, commandText, dataSet, tableNames, null);
1525         }
1526
1527         /// <summary>
1528         /// 执行指定数据库连接对象的命令,映射数据表并填充数据集,指定参数.
1529         /// </summary>
1530         /// <remarks>
1531         /// 示例:
1532         ///  FillDataset(conn, CommandType.StoredProcedure, "GetOrders", ds, new string[] {"orders"}, new SqlParameter("@prodid", 24));
1533         /// </remarks>
1534         /// <param name="connection">一个有效的数据库连接对象</param>
1535         /// <param name="commandType">命令类型 (存储过程,命令文本或其它)</param>
1536         /// <param name="commandText">存储过程名称或T-SQL语句</param>
1537         /// <param name="dataSet">要填充结果集的DataSet实例</param>
1538         /// <param name="tableNames">表映射的数据表数组
1539         /// 用户定义的表名 (可有是实际的表名.)
1540         /// </param>
1541         /// <param name="commandParameters">分配给命令的SqlParamter参数数组</param>
1542         public static void FillDataset(SqlConnection connection, CommandType commandType,
1543             string commandText, DataSet dataSet, string[] tableNames,
1544             params SqlParameter[] commandParameters)
1545         {
1546             FillDataset(connection, null, commandType, commandText, dataSet, tableNames, commandParameters);
1547         }
1548
1549         /// <summary>
1550         /// 执行指定数据库连接对象的命令,映射数据表并填充数据集,指定存储过程参数值.
1551         /// </summary>
1552         /// <remarks>
1553         /// 此方法不提供访问存储过程输出参数和返回值参数.
1554         ///
1555         /// 示例:
1556         ///  FillDataset(conn, "GetOrders", ds, new string[] {"orders"}, 24, 36);
1557         /// </remarks>
1558         /// <param name="connection">一个有效的数据库连接对象</param>
1559         /// <param name="spName">存储过程名称</param>
1560         /// <param name="dataSet">要填充结果集的DataSet实例</param>
1561         /// <param name="tableNames">表映射的数据表数组
1562         /// 用户定义的表名 (可有是实际的表名.)
1563         /// </param>
1564         /// <param name="parameterValues">分配给存储过程输入参数的对象数组</param>
1565         public static void FillDataset(SqlConnection connection, string spName,
1566             DataSet dataSet, string[] tableNames,
1567             params object[] parameterValues)
1568         {
1569             if (connection == null) throw new ArgumentNullException("connection");
1570             if (dataSet == null) throw new ArgumentNullException("dataSet");
1571             if (spName == null || spName.Length == 0) throw new ArgumentNullException("spName");
1572
1573             // 如果有参数值
1574             if ((parameterValues != null) && (parameterValues.Length > 0))
1575             {
1576                 // 从缓存中加载存储过程参数,如果缓存中不存在则从数据库中检索参数信息并加载到缓存中. ()
1577                 SqlParameter[] commandParameters = SqlHelperParameterCache.GetSpParameterSet(connection, spName);
1578
1579                 // 给存储过程参数赋值
1580                 AssignParameterValues(commandParameters, parameterValues);
1581
1582                 // 调用重载方法
1583                 FillDataset(connection, CommandType.StoredProcedure, spName, dataSet, tableNames, commandParameters);
1584             }
1585             else
1586             {
1587                 // 没有参数值
1588                 FillDataset(connection, CommandType.StoredProcedure, spName, dataSet, tableNames);
1589             }
1590         }
1591
1592         /// <summary>
1593         /// 执行指定数据库事务的命令,映射数据表并填充数据集.
1594         /// </summary>
1595         /// <remarks>
1596         /// 示例:
1597         ///  FillDataset(trans, CommandType.StoredProcedure, "GetOrders", ds, new string[] {"orders"});
1598         /// </remarks>
1599         /// <param name="transaction">一个有效的连接事务</param>
1600         /// <param name="commandType">命令类型 (存储过程,命令文本或其它)</param>
1601         /// <param name="commandText">存储过程名称或T-SQL语句</param>
1602         /// <param name="dataSet">要填充结果集的DataSet实例</param>
1603         /// <param name="tableNames">表映射的数据表数组
1604         /// 用户定义的表名 (可有是实际的表名.)
1605         /// </param>
1606         public static void FillDataset(SqlTransaction transaction, CommandType commandType,
1607             string commandText,
1608             DataSet dataSet, string[] tableNames)
1609         {
1610             FillDataset(transaction, commandType, commandText, dataSet, tableNames, null);
1611         }
1612
1613         /// <summary>
1614         /// 执行指定数据库事务的命令,映射数据表并填充数据集,指定参数.
1615         /// </summary>
1616         /// <remarks>
1617         /// 示例:
1618         ///  FillDataset(trans, CommandType.StoredProcedure, "GetOrders", ds, new string[] {"orders"}, new SqlParameter("@prodid", 24));
1619         /// </remarks>
1620         /// <param name="transaction">一个有效的连接事务</param>
1621         /// <param name="commandType">命令类型 (存储过程,命令文本或其它)</param>
1622         /// <param name="commandText">存储过程名称或T-SQL语句</param>
1623         /// <param name="dataSet">要填充结果集的DataSet实例</param>
1624         /// <param name="tableNames">表映射的数据表数组
1625         /// 用户定义的表名 (可有是实际的表名.)
1626         /// </param>
1627         /// <param name="commandParameters">分配给命令的SqlParamter参数数组</param>
1628         public static void FillDataset(SqlTransaction transaction, CommandType commandType,
1629             string commandText, DataSet dataSet, string[] tableNames,
1630             params SqlParameter[] commandParameters)
1631         {
1632             FillDataset(transaction.Connection, transaction, commandType, commandText, dataSet, tableNames, commandParameters);
1633         }
1634
1635         /// <summary>
1636         /// 执行指定数据库事务的命令,映射数据表并填充数据集,指定存储过程参数值.
1637         /// </summary>
1638         /// <remarks>
1639         /// 此方法不提供访问存储过程输出参数和返回值参数.
1640         ///
1641         /// 示例:
1642         ///  FillDataset(trans, "GetOrders", ds, new string[]{"orders"}, 24, 36);
1643         /// </remarks>
1644         /// <param name="transaction">一个有效的连接事务</param>
1645         /// <param name="spName">存储过程名称</param>
1646         /// <param name="dataSet">要填充结果集的DataSet实例</param>
1647         /// <param name="tableNames">表映射的数据表数组
1648         /// 用户定义的表名 (可有是实际的表名.)
1649         /// </param>
1650         /// <param name="parameterValues">分配给存储过程输入参数的对象数组</param>
1651         public static void FillDataset(SqlTransaction transaction, string spName,
1652             DataSet dataSet, string[] tableNames,
1653             params object[] parameterValues)
1654         {
1655             if (transaction == null) throw new ArgumentNullException("transaction");
1656             if (transaction != null && transaction.Connection == null) throw new ArgumentException("The transaction was rollbacked or commited, please provide an open transaction.", "transaction");
1657             if (dataSet == null) throw new ArgumentNullException("dataSet");
1658             if (spName == null || spName.Length == 0) throw new ArgumentNullException("spName");
1659
1660             // 如果有参数值
1661             if ((parameterValues != null) && (parameterValues.Length > 0))
1662             {
1663                 // 从缓存中加载存储过程参数,如果缓存中不存在则从数据库中检索参数信息并加载到缓存中. ()
1664                 SqlParameter[] commandParameters = SqlHelperParameterCache.GetSpParameterSet(transaction.Connection, spName);
1665
1666                 // 给存储过程参数赋值
1667                 AssignParameterValues(commandParameters, parameterValues);
1668
1669                 // 调用重载方法
1670                 FillDataset(transaction, CommandType.StoredProcedure, spName, dataSet, tableNames, commandParameters);
1671             }
1672             else
1673             {
1674                 // 没有参数值
1675                 FillDataset(transaction, CommandType.StoredProcedure, spName, dataSet, tableNames);
1676             }
1677         }
1678
1679         /// <summary>
1680         /// [私有方法][内部调用]执行指定数据库连接对象/事务的命令,映射数据表并填充数据集,DataSet/TableNames/SqlParameters.
1681         /// </summary>
1682         /// <remarks>
1683         /// 示例:
1684         ///  FillDataset(conn, trans, CommandType.StoredProcedure, "GetOrders", ds, new string[] {"orders"}, new SqlParameter("@prodid", 24));
1685         /// </remarks>
1686         /// <param name="connection">一个有效的数据库连接对象</param>
1687         /// <param name="transaction">一个有效的连接事务</param>
1688         /// <param name="commandType">命令类型 (存储过程,命令文本或其它)</param>
1689         /// <param name="commandText">存储过程名称或T-SQL语句</param>
1690         /// <param name="dataSet">要填充结果集的DataSet实例</param>
1691         /// <param name="tableNames">表映射的数据表数组
1692         /// 用户定义的表名 (可有是实际的表名.)
1693         /// </param>
1694         /// <param name="commandParameters">分配给命令的SqlParamter参数数组</param>
1695         private static void FillDataset(SqlConnection connection, SqlTransaction transaction, CommandType commandType,
1696             string commandText, DataSet dataSet, string[] tableNames,
1697             params SqlParameter[] commandParameters)
1698         {
1699             if (connection == null) throw new ArgumentNullException("connection");
1700             if (dataSet == null) throw new ArgumentNullException("dataSet");
1701
1702             // 创建SqlCommand命令,并进行预处理
1703             SqlCommand command = new SqlCommand();
1704             bool mustCloseConnection = false;
1705             PrepareCommand(command, connection, transaction, commandType, commandText, commandParameters, out mustCloseConnection);
1706
1707             // 执行命令
1708             using (SqlDataAdapter dataAdapter = new SqlDataAdapter(command))
1709             {
1710
1711                 // 追加表映射
1712                 if (tableNames != null && tableNames.Length > 0)
1713                 {
1714                     string tableName = "Table";
1715                     for (int index = 0; index < tableNames.Length; index++)
1716                     {
1717                         if (tableNames[index] == null || tableNames[index].Length == 0) throw new ArgumentException("The tableNames parameter must contain a list of tables, a value was provided as null or empty string.", "tableNames");
1718                         dataAdapter.TableMappings.Add(tableName, tableNames[index]);
1719                         tableName += (index + 1).ToString();
1720                     }
1721                 }
1722
1723                 // 填充数据集使用默认表名称
1724                 dataAdapter.Fill(dataSet);
1725
1726                 // 清除参数,以便再次使用.
1727                 command.Parameters.Clear();
1728             }
1729
1730             if (mustCloseConnection)
1731                 connection.Close();
1732         }
1733         #endregion
1734
1735         #region UpdateDataset 更新数据集
1736         /// <summary>
1737         /// 执行数据集更新到数据库,指定inserted, updated, or deleted命令.
1738         /// </summary>
1739         /// <remarks>
1740         /// 示例:
1741         ///  UpdateDataset(conn, insertCommand, deleteCommand, updateCommand, dataSet, "Order");
1742         /// </remarks>
1743         /// <param name="insertCommand">[追加记录]一个有效的T-SQL语句或存储过程</param>
1744         /// <param name="deleteCommand">[删除记录]一个有效的T-SQL语句或存储过程</param>
1745         /// <param name="updateCommand">[更新记录]一个有效的T-SQL语句或存储过程</param>
1746         /// <param name="dataSet">要更新到数据库的DataSet</param>
1747         /// <param name="tableName">要更新到数据库的DataTable</param>
1748         public static void UpdateDataset(SqlCommand insertCommand, SqlCommand deleteCommand, SqlCommand updateCommand, DataSet dataSet, string tableName)
1749         {
1750             if (insertCommand == null) throw new ArgumentNullException("insertCommand");
1751             if (deleteCommand == null) throw new ArgumentNullException("deleteCommand");
1752             if (updateCommand == null) throw new ArgumentNullException("updateCommand");
1753             if (tableName == null || tableName.Length == 0) throw new ArgumentNullException("tableName");
1754
1755             // 创建SqlDataAdapter,当操作完成后释放.
1756             using (SqlDataAdapter dataAdapter = new SqlDataAdapter())
1757             {
1758                 // 设置数据适配器命令
1759                 dataAdapter.UpdateCommand = updateCommand;
1760                 dataAdapter.InsertCommand = insertCommand;
1761                 dataAdapter.DeleteCommand = deleteCommand;
1762
1763                 // 更新数据集改变到数据库
1764                 dataAdapter.Update(dataSet, tableName);
1765
1766                 // 提交所有改变到数据集.
1767                 dataSet.AcceptChanges();
1768             }
1769         }
1770         #endregion
1771
1772         #region CreateCommand 创建一条SqlCommand命令
1773         /// <summary>
1774         /// 创建SqlCommand命令,指定数据库连接对象,存储过程名和参数.
1775         /// </summary>
1776         /// <remarks>
1777         /// 示例:
1778         ///  SqlCommand command = CreateCommand(conn, "AddCustomer", "CustomerID", "CustomerName");
1779         /// </remarks>
1780         /// <param name="connection">一个有效的数据库连接对象</param>
1781         /// <param name="spName">存储过程名称</param>
1782         /// <param name="sourceColumns">源表的列名称数组</param>
1783         /// <returns>返回SqlCommand命令</returns>
1784         public static SqlCommand CreateCommand(SqlConnection connection, string spName, params string[] sourceColumns)
1785         {
1786             if (connection == null) throw new ArgumentNullException("connection");
1787             if (spName == null || spName.Length == 0) throw new ArgumentNullException("spName");
1788
1789             // 创建命令
1790             SqlCommand cmd = new SqlCommand(spName, connection);
1791             cmd.CommandType = CommandType.StoredProcedure;
1792
1793             // 如果有参数值
1794             if ((sourceColumns != null) && (sourceColumns.Length > 0))
1795             {
1796                 // 从缓存中加载存储过程参数,如果缓存中不存在则从数据库中检索参数信息并加载到缓存中. ()
1797                 SqlParameter[] commandParameters = SqlHelperParameterCache.GetSpParameterSet(connection, spName);
1798
1799                 // 将源表的列到映射到DataSet命令中.
1800                 for (int index = 0; index < sourceColumns.Length; index++)
1801                     commandParameters[index].SourceColumn = sourceColumns[index];
1802
1803                 // Attach the discovered parameters to the SqlCommand object
1804                 AttachParameters(cmd, commandParameters);
1805             }
1806
1807             return cmd;
1808         }
1809         #endregion
1810
1811         #region ExecuteNonQueryTypedParams 类型化参数(DataRow)
1812         /// <summary>
1813         /// 执行指定连接数据库连接字符串的存储过程,使用DataRow做为参数值,返回受影响的行数.
1814         /// </summary>
1815         /// <param name="connectionString">一个有效的数据库连接字符串</param>
1816         /// <param name="spName">存储过程名称</param>
1817         /// <param name="dataRow">使用DataRow作为参数值</param>
1818         /// <returns>返回影响的行数</returns>
1819         public static int ExecuteNonQueryTypedParams(String connectionString, String spName, DataRow dataRow)
1820         {
1821             if (connectionString == null || connectionString.Length == 0) throw new ArgumentNullException("connectionString");
1822             if (spName == null || spName.Length == 0) throw new ArgumentNullException("spName");
1823
1824             // 如果row有值,存储过程必须初始化.
1825             if (dataRow != null && dataRow.ItemArray.Length > 0)
1826             {
1827                 // 从缓存中加载存储过程参数,如果缓存中不存在则从数据库中检索参数信息并加载到缓存中. ()
1828                 SqlParameter[] commandParameters = SqlHelperParameterCache.GetSpParameterSet(connectionString, spName);
1829
1830                 // 分配参数值
1831                 AssignParameterValues(commandParameters, dataRow);
1832
1833                 return SqlHelper.ExecuteNonQuery(connectionString, CommandType.StoredProcedure, spName, commandParameters);
1834             }
1835             else
1836             {
1837                 return SqlHelper.ExecuteNonQuery(connectionString, CommandType.StoredProcedure, spName);
1838             }
1839         }
1840
1841         /// <summary>
1842         /// 执行指定连接数据库连接对象的存储过程,使用DataRow做为参数值,返回受影响的行数.
1843         /// </summary>
1844         /// <param name="connection">一个有效的数据库连接对象</param>
1845         /// <param name="spName">存储过程名称</param>
1846         /// <param name="dataRow">使用DataRow作为参数值</param>
1847         /// <returns>返回影响的行数</returns>
1848         public static int ExecuteNonQueryTypedParams(SqlConnection connection, String spName, DataRow dataRow)
1849         {
1850             if (connection == null) throw new ArgumentNullException("connection");
1851             if (spName == null || spName.Length == 0) throw new ArgumentNullException("spName");
1852
1853             // 如果row有值,存储过程必须初始化.
1854             if (dataRow != null && dataRow.ItemArray.Length > 0)
1855             {
1856                 // 从缓存中加载存储过程参数,如果缓存中不存在则从数据库中检索参数信息并加载到缓存中. ()
1857                 SqlParameter[] commandParameters = SqlHelperParameterCache.GetSpParameterSet(connection, spName);
1858
1859                 // 分配参数值
1860                 AssignParameterValues(commandParameters, dataRow);
1861
1862                 return SqlHelper.ExecuteNonQuery(connection, CommandType.StoredProcedure, spName, commandParameters);
1863             }
1864             else
1865             {
1866                 return SqlHelper.ExecuteNonQuery(connection, CommandType.StoredProcedure, spName);
1867             }
1868         }
1869
1870         /// <summary>
1871         /// 执行指定连接数据库事物的存储过程,使用DataRow做为参数值,返回受影响的行数.
1872         /// </summary>
1873         /// <param name="transaction">一个有效的连接事务 object</param>
1874         /// <param name="spName">存储过程名称</param>
1875         /// <param name="dataRow">使用DataRow作为参数值</param>
1876         /// <returns>返回影响的行数</returns>
1877         public static int ExecuteNonQueryTypedParams(SqlTransaction transaction, String spName, DataRow dataRow)
1878         {
1879             if (transaction == null) throw new ArgumentNullException("transaction");
1880             if (transaction != null && transaction.Connection == null) throw new ArgumentException("The transaction was rollbacked or commited, please provide an open transaction.", "transaction");
1881             if (spName == null || spName.Length == 0) throw new ArgumentNullException("spName");
1882
1883             // Sf the row has values, the store procedure parameters must be initialized
1884             if (dataRow != null && dataRow.ItemArray.Length > 0)
1885             {
1886                 // 从缓存中加载存储过程参数,如果缓存中不存在则从数据库中检索参数信息并加载到缓存中. ()
1887                 SqlParameter[] commandParameters = SqlHelperParameterCache.GetSpParameterSet(transaction.Connection, spName);
1888
1889                 // 分配参数值
1890                 AssignParameterValues(commandParameters, dataRow);
1891
1892                 return SqlHelper.ExecuteNonQuery(transaction, CommandType.StoredProcedure, spName, commandParameters);
1893             }
1894             else
1895             {
1896                 return SqlHelper.ExecuteNonQuery(transaction, CommandType.StoredProcedure, spName);
1897             }
1898         }
1899         #endregion
1900
1901         #region ExecuteDatasetTypedParams 类型化参数(DataRow)
1902         /// <summary>
1903         /// 执行指定连接数据库连接字符串的存储过程,使用DataRow做为参数值,返回DataSet.
1904         /// </summary>
1905         /// <param name="connectionString">一个有效的数据库连接字符串</param>
1906         /// <param name="spName">存储过程名称</param>
1907         /// <param name="dataRow">使用DataRow作为参数值</param>
1908         /// <returns>返回一个包含结果集的DataSet.</returns>
1909         public static DataSet ExecuteDatasetTypedParams(string connectionString, String spName, DataRow dataRow)
1910         {
1911             if (connectionString == null || connectionString.Length == 0) throw new ArgumentNullException("connectionString");
1912             if (spName == null || spName.Length == 0) throw new ArgumentNullException("spName");
1913
1914             //如果row有值,存储过程必须初始化.
1915             if (dataRow != null && dataRow.ItemArray.Length > 0)
1916             {
1917                 // 从缓存中加载存储过程参数,如果缓存中不存在则从数据库中检索参数信息并加载到缓存中. ()
1918                 SqlParameter[] commandParameters = SqlHelperParameterCache.GetSpParameterSet(connectionString, spName);
1919
1920                 // 分配参数值
1921                 AssignParameterValues(commandParameters, dataRow);
1922
1923                 return SqlHelper.ExecuteDataset(connectionString, CommandType.StoredProcedure, spName, commandParameters);
1924             }
1925             else
1926             {
1927                 return SqlHelper.ExecuteDataset(connectionString, CommandType.StoredProcedure, spName);
1928             }
1929         }
1930
1931         /// <summary>
1932         /// 执行指定连接数据库连接对象的存储过程,使用DataRow做为参数值,返回DataSet.
1933         /// </summary>
1934         /// <param name="connection">一个有效的数据库连接对象</param>
1935         /// <param name="spName">存储过程名称</param>
1936         /// <param name="dataRow">使用DataRow作为参数值</param>
1937         /// <returns>返回一个包含结果集的DataSet.</returns>
1938         ///
1939         public static DataSet ExecuteDatasetTypedParams(SqlConnection connection, String spName, DataRow dataRow)
1940         {
1941             if (connection == null) throw new ArgumentNullException("connection");
1942             if (spName == null || spName.Length == 0) throw new ArgumentNullException("spName");
1943
1944             // 如果row有值,存储过程必须初始化.
1945             if (dataRow != null && dataRow.ItemArray.Length > 0)
1946             {
1947                 // 从缓存中加载存储过程参数,如果缓存中不存在则从数据库中检索参数信息并加载到缓存中. ()
1948                 SqlParameter[] commandParameters = SqlHelperParameterCache.GetSpParameterSet(connection, spName);
1949
1950                 // 分配参数值
1951                 AssignParameterValues(commandParameters, dataRow);
1952
1953                 return SqlHelper.ExecuteDataset(connection, CommandType.StoredProcedure, spName, commandParameters);
1954             }
1955             else
1956             {
1957                 return SqlHelper.ExecuteDataset(connection, CommandType.StoredProcedure, spName);
1958             }
1959         }
1960
1961         /// <summary>
1962         /// 执行指定连接数据库事务的存储过程,使用DataRow做为参数值,返回DataSet.
1963         /// </summary>
1964         /// <param name="transaction">一个有效的连接事务 object</param>
1965         /// <param name="spName">存储过程名称</param>
1966         /// <param name="dataRow">使用DataRow作为参数值</param>
1967         /// <returns>返回一个包含结果集的DataSet.</returns>
1968         public static DataSet ExecuteDatasetTypedParams(SqlTransaction transaction, String spName, DataRow dataRow)
1969         {
1970             if (transaction == null) throw new ArgumentNullException("transaction");
1971             if (transaction != null && transaction.Connection == null) throw new ArgumentException("The transaction was rollbacked or commited, please provide an open transaction.", "transaction");
1972             if (spName == null || spName.Length == 0) throw new ArgumentNullException("spName");
1973
1974             // 如果row有值,存储过程必须初始化.
1975             if (dataRow != null && dataRow.ItemArray.Length > 0)
1976             {
1977                 // 从缓存中加载存储过程参数,如果缓存中不存在则从数据库中检索参数信息并加载到缓存中. ()
1978                 SqlParameter[] commandParameters = SqlHelperParameterCache.GetSpParameterSet(transaction.Connection, spName);
1979
1980                 // 分配参数值
1981                 AssignParameterValues(commandParameters, dataRow);
1982
1983                 return SqlHelper.ExecuteDataset(transaction, CommandType.StoredProcedure, spName, commandParameters);
1984             }
1985             else
1986             {
1987                 return SqlHelper.ExecuteDataset(transaction, CommandType.StoredProcedure, spName);
1988             }
1989         }
1990
1991         #endregion
1992
1993         #region ExecuteReaderTypedParams 类型化参数(DataRow)
1994         /// <summary>
1995         /// 执行指定连接数据库连接字符串的存储过程,使用DataRow做为参数值,返回DataReader.
1996         /// </summary>
1997         /// <param name="connectionString">一个有效的数据库连接字符串</param>
1998         /// <param name="spName">存储过程名称</param>
1999         /// <param name="dataRow">使用DataRow作为参数值</param>
2000         /// <returns>返回包含结果集的SqlDataReader</returns>
2001         public static SqlDataReader ExecuteReaderTypedParams(String connectionString, String spName, DataRow dataRow)
2002         {
2003             if (connectionString == null || connectionString.Length == 0) throw new ArgumentNullException("connectionString");
2004             if (spName == null || spName.Length == 0) throw new ArgumentNullException("spName");
2005
2006             // 如果row有值,存储过程必须初始化.
2007             if (dataRow != null && dataRow.ItemArray.Length > 0)
2008             {
2009                 // 从缓存中加载存储过程参数,如果缓存中不存在则从数据库中检索参数信息并加载到缓存中. ()
2010                 SqlParameter[] commandParameters = SqlHelperParameterCache.GetSpParameterSet(connectionString, spName);
2011
2012                 // 分配参数值
2013                 AssignParameterValues(commandParameters, dataRow);
2014
2015                 return SqlHelper.ExecuteReader(connectionString, CommandType.StoredProcedure, spName, commandParameters);
2016             }
2017             else
2018             {
2019                 return SqlHelper.ExecuteReader(connectionString, CommandType.StoredProcedure, spName);
2020             }
2021         }
2022
2023
2024         /// <summary>
2025         /// 执行指定连接数据库连接对象的存储过程,使用DataRow做为参数值,返回DataReader.
2026         /// </summary>
2027         /// <param name="connection">一个有效的数据库连接对象</param>
2028         /// <param name="spName">存储过程名称</param>
2029         /// <param name="dataRow">使用DataRow作为参数值</param>
2030         /// <returns>返回包含结果集的SqlDataReader</returns>
2031         public static SqlDataReader ExecuteReaderTypedParams(SqlConnection connection, String spName, DataRow dataRow)
2032         {
2033             if (connection == null) throw new ArgumentNullException("connection");
2034             if (spName == null || spName.Length == 0) throw new ArgumentNullException("spName");
2035
2036             // 如果row有值,存储过程必须初始化.
2037             if (dataRow != null && dataRow.ItemArray.Length > 0)
2038             {
2039                 // 从缓存中加载存储过程参数,如果缓存中不存在则从数据库中检索参数信息并加载到缓存中. ()
2040                 SqlParameter[] commandParameters = SqlHelperParameterCache.GetSpParameterSet(connection, spName);
2041
2042                 // 分配参数值
2043                 AssignParameterValues(commandParameters, dataRow);
2044
2045                 return SqlHelper.ExecuteReader(connection, CommandType.StoredProcedure, spName, commandParameters);
2046             }
2047             else
2048             {
2049                 return SqlHelper.ExecuteReader(connection, CommandType.StoredProcedure, spName);
2050             }
2051         }
2052
2053         /// <summary>
2054         /// 执行指定连接数据库事物的存储过程,使用DataRow做为参数值,返回DataReader.
2055         /// </summary>
2056         /// <param name="transaction">一个有效的连接事务 object</param>
2057         /// <param name="spName">存储过程名称</param>
2058         /// <param name="dataRow">使用DataRow作为参数值</param>
2059         /// <returns>返回包含结果集的SqlDataReader</returns>
2060         public static SqlDataReader ExecuteReaderTypedParams(SqlTransaction transaction, String spName, DataRow dataRow)
2061         {
2062             if (transaction == null) throw new ArgumentNullException("transaction");
2063             if (transaction != null && transaction.Connection == null) throw new ArgumentException("The transaction was rollbacked or commited, please provide an open transaction.", "transaction");
2064             if (spName == null || spName.Length == 0) throw new ArgumentNullException("spName");
2065
2066             // 如果row有值,存储过程必须初始化.
2067             if (dataRow != null && dataRow.ItemArray.Length > 0)
2068             {
2069                 // 从缓存中加载存储过程参数,如果缓存中不存在则从数据库中检索参数信息并加载到缓存中. ()
2070                 SqlParameter[] commandParameters = SqlHelperParameterCache.GetSpParameterSet(transaction.Connection, spName);
2071
2072                 // 分配参数值
2073                 AssignParameterValues(commandParameters, dataRow);
2074
2075                 return SqlHelper.ExecuteReader(transaction, CommandType.StoredProcedure, spName, commandParameters);
2076             }
2077             else
2078             {
2079                 return SqlHelper.ExecuteReader(transaction, CommandType.StoredProcedure, spName);
2080             }
2081         }
2082         #endregion
2083
2084         #region ExecuteScalarTypedParams 类型化参数(DataRow)
2085         /// <summary>
2086         /// 执行指定连接数据库连接字符串的存储过程,使用DataRow做为参数值,返回结果集中的第一行第一列.
2087         /// </summary>
2088         /// <param name="connectionString">一个有效的数据库连接字符串</param>
2089         /// <param name="spName">存储过程名称</param>
2090         /// <param name="dataRow">使用DataRow作为参数值</param>
2091         /// <returns>返回结果集中的第一行第一列</returns>
2092         public static object ExecuteScalarTypedParams(String connectionString, String spName, DataRow dataRow)
2093         {
2094             if (connectionString == null || connectionString.Length == 0) throw new ArgumentNullException("connectionString");
2095             if (spName == null || spName.Length == 0) throw new ArgumentNullException("spName");
2096
2097             // 如果row有值,存储过程必须初始化.
2098             if (dataRow != null && dataRow.ItemArray.Length > 0)
2099             {
2100                 // 从缓存中加载存储过程参数,如果缓存中不存在则从数据库中检索参数信息并加载到缓存中. ()
2101                 SqlParameter[] commandParameters = SqlHelperParameterCache.GetSpParameterSet(connectionString, spName);
2102
2103                 // 分配参数值
2104                 AssignParameterValues(commandParameters, dataRow);
2105
2106                 return SqlHelper.ExecuteScalar(connectionString, CommandType.StoredProcedure, spName, commandParameters);
2107             }
2108             else
2109             {
2110                 return SqlHelper.ExecuteScalar(connectionString, CommandType.StoredProcedure, spName);
2111             }
2112         }
2113
2114         /// <summary>
2115         /// 执行指定连接数据库连接对象的存储过程,使用DataRow做为参数值,返回结果集中的第一行第一列.
2116         /// </summary>
2117         /// <param name="connection">一个有效的数据库连接对象</param>
2118         /// <param name="spName">存储过程名称</param>
2119         /// <param name="dataRow">使用DataRow作为参数值</param>
2120         /// <returns>返回结果集中的第一行第一列</returns>
2121         public static object ExecuteScalarTypedParams(SqlConnection connection, String spName, DataRow dataRow)
2122         {
2123             if (connection == null) throw new ArgumentNullException("connection");
2124             if (spName == null || spName.Length == 0) throw new ArgumentNullException("spName");
2125
2126             // 如果row有值,存储过程必须初始化.
2127             if (dataRow != null && dataRow.ItemArray.Length > 0)
2128             {
2129                 // 从缓存中加载存储过程参数,如果缓存中不存在则从数据库中检索参数信息并加载到缓存中. ()
2130                 SqlParameter[] commandParameters = SqlHelperParameterCache.GetSpParameterSet(connection, spName);
2131
2132                 // 分配参数值
2133                 AssignParameterValues(commandParameters, dataRow);
2134
2135                 return SqlHelper.ExecuteScalar(connection, CommandType.StoredProcedure, spName, commandParameters);
2136             }
2137             else
2138             {
2139                 return SqlHelper.ExecuteScalar(connection, CommandType.StoredProcedure, spName);
2140             }
2141         }
2142
2143         /// <summary>
2144         /// 执行指定连接数据库事务的存储过程,使用DataRow做为参数值,返回结果集中的第一行第一列.
2145         /// </summary>
2146         /// <param name="transaction">一个有效的连接事务 object</param>
2147         /// <param name="spName">存储过程名称</param>
2148         /// <param name="dataRow">使用DataRow作为参数值</param>
2149         /// <returns>返回结果集中的第一行第一列</returns>
2150         public static object ExecuteScalarTypedParams(SqlTransaction transaction, String spName, DataRow dataRow)
2151         {
2152             if (transaction == null) throw new ArgumentNullException("transaction");
2153             if (transaction != null && transaction.Connection == null) throw new ArgumentException("The transaction was rollbacked or commited, please provide an open transaction.", "transaction");
2154             if (spName == null || spName.Length == 0) throw new ArgumentNullException("spName");
2155
2156             // 如果row有值,存储过程必须初始化.
2157             if (dataRow != null && dataRow.ItemArray.Length > 0)
2158             {
2159                 // 从缓存中加载存储过程参数,如果缓存中不存在则从数据库中检索参数信息并加载到缓存中. ()
2160                 SqlParameter[] commandParameters = SqlHelperParameterCache.GetSpParameterSet(transaction.Connection, spName);
2161
2162                 // 分配参数值
2163                 AssignParameterValues(commandParameters, dataRow);
2164
2165                 return SqlHelper.ExecuteScalar(transaction, CommandType.StoredProcedure, spName, commandParameters);
2166             }
2167             else
2168             {
2169                 return SqlHelper.ExecuteScalar(transaction, CommandType.StoredProcedure, spName);
2170             }
2171         }
2172         #endregion
2173
2174         #region ExecuteXmlReaderTypedParams 类型化参数(DataRow)
2175         /// <summary>
2176         /// 执行指定连接数据库连接对象的存储过程,使用DataRow做为参数值,返回XmlReader类型的结果集.
2177         /// </summary>
2178         /// <param name="connection">一个有效的数据库连接对象</param>
2179         /// <param name="spName">存储过程名称</param>
2180         /// <param name="dataRow">使用DataRow作为参数值</param>
2181         /// <returns>返回XmlReader结果集对象.</returns>
2182         public static XmlReader ExecuteXmlReaderTypedParams(SqlConnection connection, String spName, DataRow dataRow)
2183         {
2184             if (connection == null) throw new ArgumentNullException("connection");
2185             if (spName == null || spName.Length == 0) throw new ArgumentNullException("spName");
2186
2187             // 如果row有值,存储过程必须初始化.
2188             if (dataRow != null && dataRow.ItemArray.Length > 0)
2189             {
2190                 // 从缓存中加载存储过程参数,如果缓存中不存在则从数据库中检索参数信息并加载到缓存中. ()
2191                 SqlParameter[] commandParameters = SqlHelperParameterCache.GetSpParameterSet(connection, spName);
2192
2193                 // 分配参数值
2194                 AssignParameterValues(commandParameters, dataRow);
2195
2196                 return SqlHelper.ExecuteXmlReader(connection, CommandType.StoredProcedure, spName, commandParameters);
2197             }
2198             else
2199             {
2200                 return SqlHelper.ExecuteXmlReader(connection, CommandType.StoredProcedure, spName);
2201             }
2202         }
2203
2204         /// <summary>
2205         /// 执行指定连接数据库事务的存储过程,使用DataRow做为参数值,返回XmlReader类型的结果集.
2206         /// </summary>
2207         /// <param name="transaction">一个有效的连接事务 object</param>
2208         /// <param name="spName">存储过程名称</param>
2209         /// <param name="dataRow">使用DataRow作为参数值</param>
2210         /// <returns>返回XmlReader结果集对象.</returns>
2211         public static XmlReader ExecuteXmlReaderTypedParams(SqlTransaction transaction, String spName, DataRow dataRow)
2212         {
2213             if (transaction == null) throw new ArgumentNullException("transaction");
2214             if (transaction != null && transaction.Connection == null) throw new ArgumentException("The transaction was rollbacked or commited, please provide an open transaction.", "transaction");
2215             if (spName == null || spName.Length == 0) throw new ArgumentNullException("spName");
2216
2217             // 如果row有值,存储过程必须初始化.
2218             if (dataRow != null && dataRow.ItemArray.Length > 0)
2219             {
2220                 // 从缓存中加载存储过程参数,如果缓存中不存在则从数据库中检索参数信息并加载到缓存中. ()
2221                 SqlParameter[] commandParameters = SqlHelperParameterCache.GetSpParameterSet(transaction.Connection, spName);
2222
2223                 // 分配参数值
2224                 AssignParameterValues(commandParameters, dataRow);
2225
2226                 return SqlHelper.ExecuteXmlReader(transaction, CommandType.StoredProcedure, spName, commandParameters);
2227             }
2228             else
2229             {
2230                 return SqlHelper.ExecuteXmlReader(transaction, CommandType.StoredProcedure, spName);
2231             }
2232         }
2233         #endregion
2234
2235     }
2236
2237     /// <summary>
2238     /// SqlHelperParameterCache提供缓存存储过程参数,并能够在运行时从存储过程中探索参数.
2239     /// </summary>
2240     public sealed class SqlHelperParameterCache
2241     {
2242         #region 私有方法,字段,构造函数
2243         // 私有构造函数,妨止类被实例化.
2244         private SqlHelperParameterCache() { }
2245
2246         // 这个方法要注意
2247         private static Hashtable paramCache = Hashtable.Synchronized(new Hashtable());
2248
2249         /// <summary>
2250         /// 探索运行时的存储过程,返回SqlParameter参数数组.
2251         /// 初始化参数值为 DBNull.Value.
2252         /// </summary>
2253         /// <param name="connection">一个有效的数据库连接</param>
2254         /// <param name="spName">存储过程名称</param>
2255         /// <param name="includeReturnValueParameter">是否包含返回值参数</param>
2256         /// <returns>返回SqlParameter参数数组</returns>
2257         private static SqlParameter[] DiscoverSpParameterSet(SqlConnection connection, string spName, bool includeReturnValueParameter)
2258         {
2259             if (connection == null) throw new ArgumentNullException("connection");
2260             if (spName == null || spName.Length == 0) throw new ArgumentNullException("spName");
2261
2262             SqlCommand cmd = new SqlCommand(spName, connection);
2263             cmd.CommandType = CommandType.StoredProcedure;
2264
2265             connection.Open();
2266             // 检索cmd指定的存储过程的参数信息,并填充到cmd的Parameters参数集中.
2267             SqlCommandBuilder.DeriveParameters(cmd);
2268             connection.Close();
2269             // 如果不包含返回值参数,将参数集中的每一个参数删除.
2270             if (!includeReturnValueParameter)
2271             {
2272                 cmd.Parameters.RemoveAt(0);
2273             }
2274
2275             // 创建参数数组
2276             SqlParameter[] discoveredParameters = new SqlParameter[cmd.Parameters.Count];
2277             // 将cmd的Parameters参数集复制到discoveredParameters数组.
2278             cmd.Parameters.CopyTo(discoveredParameters, 0);
2279
2280             // 初始化参数值为 DBNull.Value.
2281             foreach (SqlParameter discoveredParameter in discoveredParameters)
2282             {
2283                 discoveredParameter.Value = DBNull.Value;
2284             }
2285             return discoveredParameters;
2286         }
2287
2288         /// <summary>
2289         /// SqlParameter参数数组的深层拷贝.
2290         /// </summary>
2291         /// <param name="originalParameters">原始参数数组</param>
2292         /// <returns>返回一个同样的参数数组</returns>
2293         private static SqlParameter[] CloneParameters(SqlParameter[] originalParameters)
2294         {
2295             SqlParameter[] clonedParameters = new SqlParameter[originalParameters.Length];
2296
2297             for (int i = 0, j = originalParameters.Length; i < j; i++)
2298             {
2299                 clonedParameters[i] = (SqlParameter)((ICloneable)originalParameters[i]).Clone();
2300             }
2301
2302             return clonedParameters;
2303         }
2304
2305         #endregion 私有方法,字段,构造函数结束
2306
2307         #region 缓存方法
2308
2309         /// <summary>
2310         /// 追加参数数组到缓存.
2311         /// </summary>
2312         /// <param name="connectionString">一个有效的数据库连接字符串</param>
2313         /// <param name="commandText">存储过程名或SQL语句</param>
2314         /// <param name="commandParameters">要缓存的参数数组</param>
2315         public static void CacheParameterSet(string connectionString, string commandText, params SqlParameter[] commandParameters)
2316         {
2317             if (connectionString == null || connectionString.Length == 0) throw new ArgumentNullException("connectionString");
2318             if (commandText == null || commandText.Length == 0) throw new ArgumentNullException("commandText");
2319
2320             string hashKey = connectionString + ":" + commandText;
2321
2322             paramCache[hashKey] = commandParameters;
2323         }
2324
2325         /// <summary>
2326         /// 从缓存中获取参数数组.
2327         /// </summary>
2328         /// <param name="connectionString">一个有效的数据库连接字符</param>
2329         /// <param name="commandText">存储过程名或SQL语句</param>
2330         /// <returns>参数数组</returns>
2331         public static SqlParameter[] GetCachedParameterSet(string connectionString, string commandText)
2332         {
2333             if (connectionString == null || connectionString.Length == 0) throw new ArgumentNullException("connectionString");
2334             if (commandText == null || commandText.Length == 0) throw new ArgumentNullException("commandText");
2335
2336             string hashKey = connectionString + ":" + commandText;
2337
2338             SqlParameter[] cachedParameters = paramCache[hashKey] as SqlParameter[];
2339             if (cachedParameters == null)
2340             {
2341                 return null;
2342             }
2343             else
2344             {
2345                 return CloneParameters(cachedParameters);
2346             }
2347         }
2348
2349         #endregion 缓存方法结束
2350
2351         #region 检索指定的存储过程的参数集
2352
2353         /// <summary>
2354         /// 返回指定的存储过程的参数集
2355         /// </summary>
2356         /// <remarks>
2357         /// 这个方法将查询数据库,并将信息存储到缓存.
2358         /// </remarks>
2359         /// <param name="connectionString">一个有效的数据库连接字符</param>
2360         /// <param name="spName">存储过程名</param>
2361         /// <returns>返回SqlParameter参数数组</returns>
2362         public static SqlParameter[] GetSpParameterSet(string connectionString, string spName)
2363         {
2364             return GetSpParameterSet(connectionString, spName, false);
2365         }
2366
2367         /// <summary>
2368         /// 返回指定的存储过程的参数集
2369         /// </summary>
2370         /// <remarks>
2371         /// 这个方法将查询数据库,并将信息存储到缓存.
2372         /// </remarks>
2373         /// <param name="connectionString">一个有效的数据库连接字符.</param>
2374         /// <param name="spName">存储过程名</param>
2375         /// <param name="includeReturnValueParameter">是否包含返回值参数</param>
2376         /// <returns>返回SqlParameter参数数组</returns>
2377         public static SqlParameter[] GetSpParameterSet(string connectionString, string spName, bool includeReturnValueParameter)
2378         {
2379             if (connectionString == null || connectionString.Length == 0) throw new ArgumentNullException("connectionString");
2380             if (spName == null || spName.Length == 0) throw new ArgumentNullException("spName");
2381
2382             using (SqlConnection connection = new SqlConnection(connectionString))
2383             {
2384                 return GetSpParameterSetInternal(connection, spName, includeReturnValueParameter);
2385             }
2386         }
2387
2388         /// <summary>
2389         /// [内部]返回指定的存储过程的参数集(使用连接对象).
2390         /// </summary>
2391         /// <remarks>
2392         /// 这个方法将查询数据库,并将信息存储到缓存.
2393         /// </remarks>
2394         /// <param name="connection">一个有效的数据库连接字符</param>
2395         /// <param name="spName">存储过程名</param>
2396         /// <returns>返回SqlParameter参数数组</returns>
2397         internal static SqlParameter[] GetSpParameterSet(SqlConnection connection, string spName)
2398         {
2399             return GetSpParameterSet(connection, spName, false);
2400         }
2401
2402         /// <summary>
2403         /// [内部]返回指定的存储过程的参数集(使用连接对象)
2404         /// </summary>
2405         /// <remarks>
2406         /// 这个方法将查询数据库,并将信息存储到缓存.
2407         /// </remarks>
2408         /// <param name="connection">一个有效的数据库连接对象</param>
2409         /// <param name="spName">存储过程名</param>
2410         /// <param name="includeReturnValueParameter">
2411         /// 是否包含返回值参数
2412         /// </param>
2413         /// <returns>返回SqlParameter参数数组</returns>
2414         internal static SqlParameter[] GetSpParameterSet(SqlConnection connection, string spName, bool includeReturnValueParameter)
2415         {
2416             if (connection == null) throw new ArgumentNullException("connection");
2417             using (SqlConnection clonedConnection = (SqlConnection)((ICloneable)connection).Clone())
2418             {
2419                 return GetSpParameterSetInternal(clonedConnection, spName, includeReturnValueParameter);
2420             }
2421         }
2422
2423         /// <summary>
2424         /// [私有]返回指定的存储过程的参数集(使用连接对象)
2425         /// </summary>
2426         /// <param name="connection">一个有效的数据库连接对象</param>
2427         /// <param name="spName">存储过程名</param>
2428         /// <param name="includeReturnValueParameter">是否包含返回值参数</param>
2429         /// <returns>返回SqlParameter参数数组</returns>
2430         private static SqlParameter[] GetSpParameterSetInternal(SqlConnection connection, string spName, bool includeReturnValueParameter)
2431         {
2432             if (connection == null) throw new ArgumentNullException("connection");
2433             if (spName == null || spName.Length == 0) throw new ArgumentNullException("spName");
2434
2435             string hashKey = connection.ConnectionString + ":" + spName + (includeReturnValueParameter ? ":include ReturnValue Parameter" : "");
2436
2437             SqlParameter[] cachedParameters;
2438
2439             cachedParameters = paramCache[hashKey] as SqlParameter[];
2440             if (cachedParameters == null)
2441             {
2442                 SqlParameter[] spParameters = DiscoverSpParameterSet(connection, spName, includeReturnValueParameter);
2443                 paramCache[hashKey] = spParameters;
2444                 cachedParameters = spParameters;
2445             }
2446
2447             return CloneParameters(cachedParameters);
2448         }
2449
2450         #endregion 参数集检索结束
2451
2452     }
2453 }

时间: 2024-10-05 23:55:28

微软C#版SQLHelper.cs类的相关文章

微软C#版SQLHelper.cs通用类

using System; using System.Data; using System.Xml; using System.Data.SqlClient; using System.Collections; using System.Configuration; namespace BookDAL { /// <summary> /// SqlServer数据访问帮助类 /// </summary> public sealed class SqlHelper { #region

C#版SQLHelper.cs类

using System; using System.Data; using System.Xml; using System.Data.SqlClient; using System.Collections; using System.Configuration; namespace BookDAL { /// <summary> /// SqlServer数据访问帮助类 /// </summary> public sealed class SqlHelper { #region

微软SQLHelper.cs类 中文版

using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Configuration; using System.Data; using System.Data.SqlClient; using System.Xml; using System.Collections; namespace LiuYanBanT { public class SqlHelper

自用C#后端SqlHelper.cs类

自用SqlHelper.cs类,此类来自软谋教育徐老师课程SqlHelper.cs! 1 using System; 2 using System.Collections; 3 using System.Collections.Generic; 4 using System.Configuration; 5 using System.Data; 6 using System.Data.SqlClient; 7 using System.Linq; 8 using System.Web; 9 10

WebForm.aspx 页面通过 AJAX 访问WebForm.aspx.cs类中的方法,获取数据(转)

WebForm.aspx 页面通过 AJAX 访问WebForm.aspx.cs类中的方法,获取数据 WebForm1.aspx 页面 (原生AJAX请求,写法一) <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="IsPostBack.WebForm1" %> <!DOCTYPE htm

.NET Framework 2.0 SP2微软官方版+.net

.net framework 2.0 Service Pack 2 是专为解决客户在 .NET Framework 2.0 发布后报告的问题而提供的累积更新.此外,此发行版为 .NET Framework 3.5 Service Pack 1 提供了性能改进和必备功能支持.请在系统需求一节中查看此部署程序包支持的操作系统.Microsoft Vista 的相应更新和 Microsoft Windows 2008 的相应更新可通过完整的 .NET Framework 3.5 service pac

C# winform窗体编程.cs类,窗体打不开问题

此问题主要在VS2008.  Visual Studio 2008版本中出现,解决方案如下: 问题示例:如下图 红色框中的都是winform窗体,而现在却都是.cs类 解决该问题的步骤: 1.在项目中右键,点击下图中红框的选项 2.在目录中找到以".csproj"结尾的文件,如下图 3.打开该文件,找到打不开的窗体名字,在该文件中如下图的位置添加红色框中的配置: 注意添加配置的位置 4.完成,如下图

1——自我实现一个简洁版的String类

在C++中有C没有的string字符串类型,string类型的数据其实是一个指向字符串首地址的指针变量,因此在string类的默认成员函数拷贝构造和赋值运算符的重载就会涉及到深浅拷贝的问题,一不小心要么就是内存泄露要么就是多次释放同一块空间导致程序崩溃,下面就来模拟实现一个简洁版的String类: 既然是指向一个字符串的指针,因此类的成员变量就需要有一个char*类型的指针: #include <iostream> #include <string.h> using namespa

【protobuf进阶】通过.proto文件导出C#支持的.cs类文件

protobuf是一个跨平台的消息交互协议,类似xml.json等. protocolbuffer(以下简称PB)是google 的一种数据交换的格式,它独立于语言,独立于平台.google 提供了多种语言的实现:java.c#.c++.go 和 python,每一种实现都包含了相应语言的编译器以及库文件.由于它是一种二进制的格式,比使用xml 进行数据交换快许多.可以把它用于分布式应用之间的数据通信或者异构环境下的数据交换.作为一种效率和兼容性都很优秀的二进制数据传输格式,可以用于诸如网络传输