微软SQLHelper.cs类

   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 

   9 //微软SQLHelper.cs类 中文版

  10 

  11 namespace Sales.DAL 

  12 { 

  13     /// <summary> 

  14     /// SqlServer数据访问帮助类 

  15     /// </summary> 

  16     public sealed class SqlHelper 

  17     { 

  18         #region 私有构造函数和方法

  19 

  20         private SqlHelper() { }

  21 

  22         /// <summary> 

  23         /// 将SqlParameter参数数组(参数值)分配给SqlCommand命令. 

  24         /// 这个方法将给任何一个参数分配DBNull.Value; 

  25         /// 该操作将阻止默认值的使用. 

  26         /// </summary> 

  27         /// <param name="command">命令名</param> 

  28         /// <param name="commandParameters">SqlParameters数组</param> 

  29         private static void AttachParameters(SqlCommand command, SqlParameter[] commandParameters) 

  30         { 

  31             if (command == null) throw new ArgumentNullException("command"); 

  32             if (commandParameters != null) 

  33             { 

  34                 foreach (SqlParameter p in commandParameters) 

  35                 { 

  36                     if (p != null) 

  37                     { 

  38                         // 检查未分配值的输出参数,将其分配以DBNull.Value. 

  39                         if ((p.Direction == ParameterDirection.InputOutput || p.Direction == ParameterDirection.Input) && 

  40                             (p.Value == null)) 

  41                         { 

  42                             p.Value = DBNull.Value; 

  43                         } 

  44                         command.Parameters.Add(p); 

  45                     } 

  46                 } 

  47             } 

  48         }

  49 

  50         /// <summary> 

  51         /// 将DataRow类型的列值分配到SqlParameter参数数组. 

  52         /// </summary> 

  53         /// <param name="commandParameters">要分配值的SqlParameter参数数组</param> 

  54         /// <param name="dataRow">将要分配给存储过程参数的DataRow</param> 

  55         private static void AssignParameterValues(SqlParameter[] commandParameters, DataRow dataRow) 

  56         { 

  57             if ((commandParameters == null) || (dataRow == null)) 

  58             { 

  59                 return; 

  60             }

  61 

  62             int i = 0; 

  63             // 设置参数值 

  64             foreach (SqlParameter commandParameter in commandParameters) 

  65             { 

  66                 // 创建参数名称,如果不存在,只抛出一个异常. 

  67                 if (commandParameter.ParameterName == null || 

  68                     commandParameter.ParameterName.Length <= 1) 

  69                     throw new Exception( 

  70                         string.Format("请提供参数{0}一个有效的名称{1}.", i, commandParameter.ParameterName)); 

  71                 // 从dataRow的表中获取为参数数组中数组名称的列的索引. 

  72                 // 如果存在和参数名称相同的列,则将列值赋给当前名称的参数. 

  73                 if (dataRow.Table.Columns.IndexOf(commandParameter.ParameterName.Substring(1)) != -1) 

  74                     commandParameter.Value = dataRow[commandParameter.ParameterName.Substring(1)]; 

  75                 i++; 

  76             } 

  77         }

  78 

  79         /// <summary> 

  80         /// 将一个对象数组分配给SqlParameter参数数组. 

  81         /// </summary> 

  82         /// <param name="commandParameters">要分配值的SqlParameter参数数组</param> 

  83         /// <param name="parameterValues">将要分配给存储过程参数的对象数组</param> 

  84         private static void AssignParameterValues(SqlParameter[] commandParameters, object[] parameterValues) 

  85         { 

  86             if ((commandParameters == null) || (parameterValues == null)) 

  87             { 

  88                 return; 

  89             }

  90 

  91             // 确保对象数组个数与参数个数匹配,如果不匹配,抛出一个异常. 

  92             if (commandParameters.Length != parameterValues.Length) 

  93             { 

  94                 throw new ArgumentException("参数值个数与参数不匹配."); 

  95             }

  96 

  97             // 给参数赋值 

  98             for (int i = 0, j = commandParameters.Length; i < j; i++) 

  99             { 

 100                 // If the current array value derives from IDbDataParameter, then assign its Value property 

 101                 if (parameterValues[i] is IDbDataParameter) 

 102                 { 

 103                     IDbDataParameter paramInstance = (IDbDataParameter)parameterValues[i]; 

 104                     if (paramInstance.Value == null) 

 105                     { 

 106                         commandParameters[i].Value = DBNull.Value; 

 107                     } 

 108                     else 

 109                     { 

 110                         commandParameters[i].Value = paramInstance.Value; 

 111                     } 

 112                 } 

 113                 else if (parameterValues[i] == null) 

 114                 { 

 115                     commandParameters[i].Value = DBNull.Value; 

 116                 } 

 117                 else 

 118                 { 

 119                     commandParameters[i].Value = parameterValues[i]; 

 120                 } 

 121             } 

 122         }

 123 

 124         /// <summary> 

 125         /// 预处理用户提供的命令,数据库连接/事务/命令类型/参数 

 126         /// </summary> 

 127         /// <param name="command">要处理的SqlCommand</param> 

 128         /// <param name="connection">数据库连接</param> 

 129         /// <param name="transaction">一个有效的事务或者是null值</param> 

 130         /// <param name="commandType">命令类型 (存储过程,命令文本, 其它.)</param> 

 131         /// <param name="commandText">存储过程名或都T-SQL命令文本</param> 

 132         /// <param name="commandParameters">和命令相关联的SqlParameter参数数组,如果没有参数为'null'</param> 

 133         /// <param name="mustCloseConnection"><c>true</c> 如果连接是打开的,则为true,其它情况下为false.</param> 

 134         private static void PrepareCommand(SqlCommand command, SqlConnection connection, SqlTransaction transaction, CommandType commandType, string commandText, SqlParameter[] commandParameters, out bool mustCloseConnection) 

 135         { 

 136             if (command == null) throw new ArgumentNullException("command"); 

 137             if (commandText == null || commandText.Length == 0) throw new ArgumentNullException("commandText");

 138 

 139             // If the provided connection is not open, we will open it 

 140             if (connection.State != ConnectionState.Open) 

 141             { 

 142                 mustCloseConnection = true; 

 143                 connection.Open(); 

 144             } 

 145             else 

 146             { 

 147                 mustCloseConnection = false; 

 148             }

 149 

 150             // 给命令分配一个数据库连接. 

 151             command.Connection = connection;

 152 

 153             // 设置命令文本(存储过程名或SQL语句) 

 154             command.CommandText = commandText;

 155 

 156             // 分配事务 

 157             if (transaction != null) 

 158             { 

 159                 if (transaction.Connection == null) throw new ArgumentException("The transaction was rollbacked or commited, please provide an open transaction.", "transaction"); 

 160                 command.Transaction = transaction; 

 161             }

 162 

 163             // 设置命令类型. 

 164             command.CommandType = commandType;

 165 

 166             // 分配命令参数 

 167             if (commandParameters != null) 

 168             { 

 169                 AttachParameters(command, commandParameters); 

 170             } 

 171             return; 

 172         }

 173 

 174         #endregion 私有构造函数和方法结束

 175 

 176         #region 数据库连接 

 177         /// <summary> 

 178         /// 一个有效的数据库连接字符串 

 179         /// </summary> 

 180         /// <returns></returns> 

 181         public static string GetConnSting() 

 182         { 

 183             return ConfigurationManager.ConnectionStrings["ConStr"].ConnectionString; 

 184         } 

 185         /// <summary> 

 186         /// 一个有效的数据库连接对象 

 187         /// </summary> 

 188         /// <returns></returns> 

 189         public static SqlConnection GetConnection() 

 190         { 

 191             SqlConnection Connection = new SqlConnection(SqlHelper.GetConnSting()); 

 192             return Connection; 

 193         } 

 194         #endregion

 195 

 196         #region ExecuteNonQuery命令

 197 

 198         /// <summary> 

 199         /// 执行指定连接字符串,类型的SqlCommand. 

 200         /// </summary> 

 201         /// <remarks> 

 202         /// 示例:  

 203         ///  int result = ExecuteNonQuery(connString, CommandType.StoredProcedure, "PublishOrders"); 

 204         /// </remarks> 

 205         /// <param name="connectionString">一个有效的数据库连接字符串</param> 

 206         /// <param name="commandType">命令类型 (存储过程,命令文本, 其它.)</param> 

 207         /// <param name="commandText">存储过程名称或SQL语句</param> 

 208         /// <returns>返回命令影响的行数</returns> 

 209         public static int ExecuteNonQuery(string connectionString, CommandType commandType, string commandText) 

 210         { 

 211             return ExecuteNonQuery(connectionString, commandType, commandText, (SqlParameter[])null); 

 212         }

 213 

 214         /// <summary> 

 215         /// 执行指定连接字符串,类型的SqlCommand.如果没有提供参数,不返回结果. 

 216         /// </summary> 

 217         /// <remarks> 

 218         /// 示例:  

 219         ///  int result = ExecuteNonQuery(connString, CommandType.StoredProcedure, "PublishOrders", new SqlParameter("@prodid", 24)); 

 220         /// </remarks> 

 221         /// <param name="connectionString">一个有效的数据库连接字符串</param> 

 222         /// <param name="commandType">命令类型 (存储过程,命令文本, 其它.)</param> 

 223         /// <param name="commandText">存储过程名称或SQL语句</param> 

 224         /// <param name="commandParameters">SqlParameter参数数组</param> 

 225         /// <returns>返回命令影响的行数</returns> 

 226         public static int ExecuteNonQuery(string connectionString, CommandType commandType, string commandText, params SqlParameter[] commandParameters) 

 227         { 

 228             if (connectionString == null || connectionString.Length == 0) throw new ArgumentNullException("connectionString");

 229 

 230             using (SqlConnection connection = new SqlConnection(connectionString)) 

 231             { 

 232                 connection.Open();

 233 

 234                 return ExecuteNonQuery(connection, commandType, commandText, commandParameters); 

 235             } 

 236         }

 237 

 238         /// <summary> 

 239         /// 执行指定连接字符串的存储过程,将对象数组的值赋给存储过程参数, 

 240         /// 此方法需要在参数缓存方法中探索参数并生成参数. 

 241         /// </summary> 

 242         /// <remarks> 

 243         /// 这个方法没有提供访问输出参数和返回值. 

 244         /// 示例:  

 245         ///  int result = ExecuteNonQuery(connString, "PublishOrders", 24, 36); 

 246         /// </remarks> 

 247         /// <param name="connectionString">一个有效的数据库连接字符串/param> 

 248         /// <param name="spName">存储过程名称</param> 

 249         /// <param name="parameterValues">分配到存储过程输入参数的对象数组</param> 

 250         /// <returns>返回受影响的行数</returns> 

 251         public static int ExecuteNonQuery(string connectionString, string spName, params object[] parameterValues) 

 252         { 

 253             if (connectionString == null || connectionString.Length == 0) throw new ArgumentNullException("connectionString"); 

 254             if (spName == null || spName.Length == 0) throw new ArgumentNullException("spName");

 255 

 256             // 如果存在参数值 

 257             if ((parameterValues != null) && (parameterValues.Length > 0)) 

 258             { 

 259                 // 从探索存储过程参数(加载到缓存)并分配给存储过程参数数组. 

 260                 SqlParameter[] commandParameters = SqlHelperParameterCache.GetSpParameterSet(connectionString, spName);

 261 

 262                 // 给存储过程参数赋值 

 263                 AssignParameterValues(commandParameters, parameterValues);

 264 

 265                 return ExecuteNonQuery(connectionString, CommandType.StoredProcedure, spName, commandParameters); 

 266             } 

 267             else 

 268             { 

 269                 // 没有参数情况下 

 270                 return ExecuteNonQuery(connectionString, CommandType.StoredProcedure, spName); 

 271             } 

 272         }

 273 

 274         /// <summary> 

 275         /// 执行指定数据库连接对象的命令 

 276         /// </summary> 

 277         /// <remarks> 

 278         /// 示例:  

 279         ///  int result = ExecuteNonQuery(conn, CommandType.StoredProcedure, "PublishOrders"); 

 280         /// </remarks> 

 281         /// <param name="connection">一个有效的数据库连接对象</param> 

 282         /// <param name="commandType">命令类型(存储过程,命令文本或其它.)</param> 

 283         /// <param name="commandText">存储过程名称或T-SQL语句</param> 

 284         /// <returns>返回影响的行数</returns> 

 285         public static int ExecuteNonQuery(SqlConnection connection, CommandType commandType, string commandText) 

 286         { 

 287             return ExecuteNonQuery(connection, commandType, commandText, (SqlParameter[])null); 

 288         }

 289 

 290         /// <summary> 

 291         /// 执行指定数据库连接对象的命令 

 292         /// </summary> 

 293         /// <remarks> 

 294         /// 示例:  

 295         ///  int result = ExecuteNonQuery(conn, CommandType.StoredProcedure, "PublishOrders", new SqlParameter("@prodid", 24)); 

 296         /// </remarks> 

 297         /// <param name="connection">一个有效的数据库连接对象</param> 

 298         /// <param name="commandType">命令类型(存储过程,命令文本或其它.)</param> 

 299         /// <param name="commandText">T存储过程名称或T-SQL语句</param> 

 300         /// <param name="commandParameters">SqlParamter参数数组</param> 

 301         /// <returns>返回影响的行数</returns> 

 302         public static int ExecuteNonQuery(SqlConnection connection, CommandType commandType, string commandText, params SqlParameter[] commandParameters) 

 303         { 

 304             if (connection == null) throw new ArgumentNullException("connection");

 305 

 306             // 创建SqlCommand命令,并进行预处理 

 307             SqlCommand cmd = new SqlCommand(); 

 308             bool mustCloseConnection = false; 

 309             PrepareCommand(cmd, connection, (SqlTransaction)null, commandType, commandText, commandParameters, out mustCloseConnection);

 310 

 311             // Finally, execute the command 

 312             int retval = cmd.ExecuteNonQuery();

 313 

 314             // 清除参数,以便再次使用. 

 315             cmd.Parameters.Clear(); 

 316             if (mustCloseConnection) 

 317                 connection.Close(); 

 318             return retval; 

 319         }

 320 

 321         /// <summary> 

 322         /// 执行指定数据库连接对象的命令,将对象数组的值赋给存储过程参数. 

 323         /// </summary> 

 324         /// <remarks> 

 325         /// 此方法不提供访问存储过程输出参数和返回值 

 326         /// 示例:  

 327         ///  int result = ExecuteNonQuery(conn, "PublishOrders", 24, 36); 

 328         /// </remarks> 

 329         /// <param name="connection">一个有效的数据库连接对象</param> 

 330         /// <param name="spName">存储过程名</param> 

 331         /// <param name="parameterValues">分配给存储过程输入参数的对象数组</param> 

 332         /// <returns>返回影响的行数</returns> 

 333         public static int ExecuteNonQuery(SqlConnection connection, string spName, params object[] parameterValues) 

 334         { 

 335             if (connection == null) throw new ArgumentNullException("connection"); 

 336             if (spName == null || spName.Length == 0) throw new ArgumentNullException("spName");

 337 

 338             // 如果有参数值 

 339             if ((parameterValues != null) && (parameterValues.Length > 0)) 

 340             { 

 341                 // 从缓存中加载存储过程参数 

 342                 SqlParameter[] commandParameters = SqlHelperParameterCache.GetSpParameterSet(connection, spName);

 343 

 344                 // 给存储过程分配参数值 

 345                 AssignParameterValues(commandParameters, parameterValues);

 346 

 347                 return ExecuteNonQuery(connection, CommandType.StoredProcedure, spName, commandParameters); 

 348             } 

 349             else 

 350             { 

 351                 return ExecuteNonQuery(connection, CommandType.StoredProcedure, spName); 

 352             } 

 353         }

 354 

 355         /// <summary> 

 356         /// 执行带事务的SqlCommand. 

 357         /// </summary> 

 358         /// <remarks> 

 359         /// 示例.:  

 360         ///  int result = ExecuteNonQuery(trans, CommandType.StoredProcedure, "PublishOrders"); 

 361         /// </remarks> 

 362         /// <param name="transaction">一个有效的数据库连接对象</param> 

 363         /// <param name="commandType">命令类型(存储过程,命令文本或其它.)</param> 

 364         /// <param name="commandText">存储过程名称或T-SQL语句</param> 

 365         /// <returns>返回影响的行数/returns> 

 366         public static int ExecuteNonQuery(SqlTransaction transaction, CommandType commandType, string commandText) 

 367         { 

 368             return ExecuteNonQuery(transaction, commandType, commandText, (SqlParameter[])null); 

 369         }

 370 

 371         /// <summary> 

 372         /// 执行带事务的SqlCommand(指定参数). 

 373         /// </summary> 

 374         /// <remarks> 

 375         /// 示例:  

 376         ///  int result = ExecuteNonQuery(trans, CommandType.StoredProcedure, "GetOrders", new SqlParameter("@prodid", 24)); 

 377         /// </remarks> 

 378         /// <param name="transaction">一个有效的数据库连接对象</param> 

 379         /// <param name="commandType">命令类型(存储过程,命令文本或其它.)</param> 

 380         /// <param name="commandText">存储过程名称或T-SQL语句</param> 

 381         /// <param name="commandParameters">SqlParamter参数数组</param> 

 382         /// <returns>返回影响的行数</returns> 

 383         public static int ExecuteNonQuery(SqlTransaction transaction, CommandType commandType, string commandText, params SqlParameter[] commandParameters) 

 384         { 

 385             if (transaction == null) throw new ArgumentNullException("transaction"); 

 386             if (transaction != null && transaction.Connection == null) throw new ArgumentException("The transaction was rollbacked or commited, please provide an open transaction.", "transaction");

 387 

 388             // 预处理 

 389             SqlCommand cmd = new SqlCommand(); 

 390             bool mustCloseConnection = false; 

 391             PrepareCommand(cmd, transaction.Connection, transaction, commandType, commandText, commandParameters, out mustCloseConnection);

 392 

 393             // 执行 

 394             int retval = cmd.ExecuteNonQuery();

 395 

 396             // 清除参数集,以便再次使用. 

 397             cmd.Parameters.Clear(); 

 398             return retval; 

 399         }

 400 

 401         /// <summary> 

 402         /// 执行带事务的SqlCommand(指定参数值). 

 403         /// </summary> 

 404         /// <remarks> 

 405         /// 此方法不提供访问存储过程输出参数和返回值 

 406         /// 示例:  

 407         ///  int result = ExecuteNonQuery(conn, trans, "PublishOrders", 24, 36); 

 408         /// </remarks> 

 409         /// <param name="transaction">一个有效的数据库连接对象</param> 

 410         /// <param name="spName">存储过程名</param> 

 411         /// <param name="parameterValues">分配给存储过程输入参数的对象数组</param> 

 412         /// <returns>返回受影响的行数</returns> 

 413         public static int ExecuteNonQuery(SqlTransaction transaction, string spName, params object[] parameterValues) 

 414         { 

 415             if (transaction == null) throw new ArgumentNullException("transaction"); 

 416             if (transaction != null && transaction.Connection == null) throw new ArgumentException("The transaction was rollbacked or commited, please provide an open transaction.", "transaction"); 

 417             if (spName == null || spName.Length == 0) throw new ArgumentNullException("spName");

 418 

 419             // 如果有参数值 

 420             if ((parameterValues != null) && (parameterValues.Length > 0)) 

 421             { 

 422                 // 从缓存中加载存储过程参数,如果缓存中不存在则从数据库中检索参数信息并加载到缓存中. () 

 423                 SqlParameter[] commandParameters = SqlHelperParameterCache.GetSpParameterSet(transaction.Connection, spName);

 424 

 425                 // 给存储过程参数赋值 

 426                 AssignParameterValues(commandParameters, parameterValues);

 427 

 428                 // 调用重载方法 

 429                 return ExecuteNonQuery(transaction, CommandType.StoredProcedure, spName, commandParameters); 

 430             } 

 431             else 

 432             { 

 433                 // 没有参数值 

 434                 return ExecuteNonQuery(transaction, CommandType.StoredProcedure, spName); 

 435             } 

 436         }

 437 

 438         #endregion ExecuteNonQuery方法结束

 439 

 440         #region ExecuteDataset方法

 441 

 442         /// <summary> 

 443         /// 执行指定数据库连接字符串的命令,返回DataSet. 

 444         /// </summary> 

 445         /// <remarks> 

 446         /// 示例:  

 447         ///  DataSet ds = ExecuteDataset(connString, CommandType.StoredProcedure, "GetOrders"); 

 448         /// </remarks> 

 449         /// <param name="connectionString">一个有效的数据库连接字符串</param> 

 450         /// <param name="commandType">命令类型 (存储过程,命令文本或其它)</param> 

 451         /// <param name="commandText">存储过程名称或T-SQL语句</param> 

 452         /// <returns>返回一个包含结果集的DataSet</returns> 

 453         public static DataSet ExecuteDataset(string connectionString, CommandType commandType, string commandText) 

 454         { 

 455             return ExecuteDataset(connectionString, commandType, commandText, (SqlParameter[])null); 

 456         }

 457 

 458         /// <summary> 

 459         /// 执行指定数据库连接字符串的命令,返回DataSet. 

 460         /// </summary> 

 461         /// <remarks> 

 462         /// 示例: 

 463         ///  DataSet ds = ExecuteDataset(connString, CommandType.StoredProcedure, "GetOrders", new SqlParameter("@prodid", 24)); 

 464         /// </remarks> 

 465         /// <param name="connectionString">一个有效的数据库连接字符串</param> 

 466         /// <param name="commandType">命令类型 (存储过程,命令文本或其它)</param> 

 467         /// <param name="commandText">存储过程名称或T-SQL语句</param> 

 468         /// <param name="commandParameters">SqlParamters参数数组</param> 

 469         /// <returns>返回一个包含结果集的DataSet</returns> 

 470         public static DataSet ExecuteDataset(string connectionString, CommandType commandType, string commandText, params SqlParameter[] commandParameters) 

 471         { 

 472             if (connectionString == null || connectionString.Length == 0) throw new ArgumentNullException("connectionString");

 473 

 474             // 创建并打开数据库连接对象,操作完成释放对象. 

 475             using (SqlConnection connection = new SqlConnection(connectionString)) 

 476             { 

 477                 connection.Open();

 478 

 479                 // 调用指定数据库连接字符串重载方法. 

 480                 return ExecuteDataset(connection, commandType, commandText, commandParameters); 

 481             } 

 482         }

 483 

 484         /// <summary> 

 485         /// 执行指定数据库连接字符串的命令,直接提供参数值,返回DataSet. 

 486         /// </summary> 

 487         /// <remarks> 

 488         /// 此方法不提供访问存储过程输出参数和返回值. 

 489         /// 示例: 

 490         ///  DataSet ds = ExecuteDataset(connString, "GetOrders", 24, 36); 

 491         /// </remarks> 

 492         /// <param name="connectionString">一个有效的数据库连接字符串</param> 

 493         /// <param name="spName">存储过程名</param> 

 494         /// <param name="parameterValues">分配给存储过程输入参数的对象数组</param> 

 495         /// <returns>返回一个包含结果集的DataSet</returns> 

 496         public static DataSet ExecuteDataset(string connectionString, string spName, params object[] parameterValues) 

 497         { 

 498             if (connectionString == null || connectionString.Length == 0) throw new ArgumentNullException("connectionString"); 

 499             if (spName == null || spName.Length == 0) throw new ArgumentNullException("spName");

 500 

 501             if ((parameterValues != null) && (parameterValues.Length > 0)) 

 502             { 

 503                 // 从缓存中检索存储过程参数 

 504                 SqlParameter[] commandParameters = SqlHelperParameterCache.GetSpParameterSet(connectionString, spName);

 505 

 506                 // 给存储过程参数分配值 

 507                 AssignParameterValues(commandParameters, parameterValues);

 508 

 509                 return ExecuteDataset(connectionString, CommandType.StoredProcedure, spName, commandParameters); 

 510             } 

 511             else 

 512             { 

 513                 return ExecuteDataset(connectionString, CommandType.StoredProcedure, spName); 

 514             } 

 515         }

 516 

 517         /// <summary> 

 518         /// 执行指定数据库连接对象的命令,返回DataSet. 

 519         /// </summary> 

 520         /// <remarks> 

 521         /// 示例:  

 522         ///  DataSet ds = ExecuteDataset(conn, CommandType.StoredProcedure, "GetOrders"); 

 523         /// </remarks> 

 524         /// <param name="connection">一个有效的数据库连接对象</param> 

 525         /// <param name="commandType">命令类型 (存储过程,命令文本或其它)</param> 

 526         /// <param name="commandText">存储过程名或T-SQL语句</param> 

 527         /// <returns>返回一个包含结果集的DataSet</returns> 

 528         public static DataSet ExecuteDataset(SqlConnection connection, CommandType commandType, string commandText) 

 529         { 

 530             return ExecuteDataset(connection, commandType, commandText, (SqlParameter[])null); 

 531         }

 532 

 533         /// <summary> 

 534         /// 执行指定数据库连接对象的命令,指定存储过程参数,返回DataSet. 

 535         /// </summary> 

 536         /// <remarks> 

 537         /// 示例:  

 538         ///  DataSet ds = ExecuteDataset(conn, CommandType.StoredProcedure, "GetOrders", new SqlParameter("@prodid", 24)); 

 539         /// </remarks> 

 540         /// <param name="connection">一个有效的数据库连接对象</param> 

 541         /// <param name="commandType">命令类型 (存储过程,命令文本或其它)</param> 

 542         /// <param name="commandText">存储过程名或T-SQL语句</param> 

 543         /// <param name="commandParameters">SqlParamter参数数组</param> 

 544         /// <returns>返回一个包含结果集的DataSet</returns> 

 545         public static DataSet ExecuteDataset(SqlConnection connection, CommandType commandType, string commandText, params SqlParameter[] commandParameters) 

 546         { 

 547             if (connection == null) throw new ArgumentNullException("connection");

 548 

 549             // 预处理 

 550             SqlCommand cmd = new SqlCommand(); 

 551             bool mustCloseConnection = false; 

 552             PrepareCommand(cmd, connection, (SqlTransaction)null, commandType, commandText, commandParameters, out mustCloseConnection);

 553 

 554             // 创建SqlDataAdapter和DataSet. 

 555             using (SqlDataAdapter da = new SqlDataAdapter(cmd)) 

 556             { 

 557                 DataSet ds = new DataSet();

 558 

 559                 // 填充DataSet. 

 560                 da.Fill(ds);

 561 

 562                 cmd.Parameters.Clear();

 563 

 564                 if (mustCloseConnection) 

 565                     connection.Close();

 566 

 567                 return ds; 

 568             } 

 569         }

 570 

 571         /// <summary> 

 572         /// 执行指定数据库连接对象的命令,指定参数值,返回DataSet. 

 573         /// </summary> 

 574         /// <remarks> 

 575         /// 此方法不提供访问存储过程输入参数和返回值. 

 576         /// 示例.:  

 577         ///  DataSet ds = ExecuteDataset(conn, "GetOrders", 24, 36); 

 578         /// </remarks> 

 579         /// <param name="connection">一个有效的数据库连接对象</param> 

 580         /// <param name="spName">存储过程名</param> 

 581         /// <param name="parameterValues">分配给存储过程输入参数的对象数组</param> 

 582         /// <returns>返回一个包含结果集的DataSet</returns> 

 583         public static DataSet ExecuteDataset(SqlConnection connection, string spName, params object[] parameterValues) 

 584         { 

 585             if (connection == null) throw new ArgumentNullException("connection"); 

 586             if (spName == null || spName.Length == 0) throw new ArgumentNullException("spName");

 587 

 588             if ((parameterValues != null) && (parameterValues.Length > 0)) 

 589             { 

 590                 // 比缓存中加载存储过程参数 

 591                 SqlParameter[] commandParameters = SqlHelperParameterCache.GetSpParameterSet(connection, spName);

 592 

 593                 // 给存储过程参数分配值 

 594                 AssignParameterValues(commandParameters, parameterValues);

 595 

 596                 return ExecuteDataset(connection, CommandType.StoredProcedure, spName, commandParameters); 

 597             } 

 598             else 

 599             { 

 600                 return ExecuteDataset(connection, CommandType.StoredProcedure, spName); 

 601             } 

 602         }

 603 

 604         /// <summary> 

 605         /// 执行指定事务的命令,返回DataSet. 

 606         /// </summary> 

 607         /// <remarks> 

 608         /// 示例:  

 609         ///  DataSet ds = ExecuteDataset(trans, CommandType.StoredProcedure, "GetOrders"); 

 610         /// </remarks> 

 611         /// <param name="transaction">事务</param> 

 612         /// <param name="commandType">命令类型 (存储过程,命令文本或其它)</param> 

 613         /// <param name="commandText">存储过程名或T-SQL语句</param> 

 614         /// <returns>返回一个包含结果集的DataSet</returns> 

 615         public static DataSet ExecuteDataset(SqlTransaction transaction, CommandType commandType, string commandText) 

 616         { 

 617             return ExecuteDataset(transaction, commandType, commandText, (SqlParameter[])null); 

 618         }

 619 

 620         /// <summary> 

 621         /// 执行指定事务的命令,指定参数,返回DataSet. 

 622         /// </summary> 

 623         /// <remarks> 

 624         /// 示例:  

 625         ///  DataSet ds = ExecuteDataset(trans, CommandType.StoredProcedure, "GetOrders", new SqlParameter("@prodid", 24)); 

 626         /// </remarks> 

 627         /// <param name="transaction">事务</param> 

 628         /// <param name="commandType">命令类型 (存储过程,命令文本或其它)</param> 

 629         /// <param name="commandText">存储过程名或T-SQL语句</param> 

 630         /// <param name="commandParameters">SqlParamter参数数组</param> 

 631         /// <returns>返回一个包含结果集的DataSet</returns> 

 632         public static DataSet ExecuteDataset(SqlTransaction transaction, CommandType commandType, string commandText, params SqlParameter[] commandParameters) 

 633         { 

 634             if (transaction == null) throw new ArgumentNullException("transaction"); 

 635             if (transaction != null && transaction.Connection == null) throw new ArgumentException("The transaction was rollbacked or commited, please provide an open transaction.", "transaction");

 636 

 637             // 预处理 

 638             SqlCommand cmd = new SqlCommand(); 

 639             bool mustCloseConnection = false; 

 640             PrepareCommand(cmd, transaction.Connection, transaction, commandType, commandText, commandParameters, out mustCloseConnection);

 641 

 642             // 创建 DataAdapter & DataSet 

 643             using (SqlDataAdapter da = new SqlDataAdapter(cmd)) 

 644             { 

 645                 DataSet ds = new DataSet(); 

 646                 da.Fill(ds); 

 647                 cmd.Parameters.Clear(); 

 648                 return ds; 

 649             } 

 650         }

 651 

 652         /// <summary> 

 653         /// 执行指定事务的命令,指定参数值,返回DataSet. 

 654         /// </summary> 

 655         /// <remarks> 

 656         /// 此方法不提供访问存储过程输入参数和返回值. 

 657         /// 示例.:  

 658         ///  DataSet ds = ExecuteDataset(trans, "GetOrders", 24, 36); 

 659         /// </remarks> 

 660         /// <param name="transaction">事务</param> 

 661         /// <param name="spName">存储过程名</param> 

 662         /// <param name="parameterValues">分配给存储过程输入参数的对象数组</param> 

 663         /// <returns>返回一个包含结果集的DataSet</returns> 

 664         public static DataSet ExecuteDataset(SqlTransaction transaction, string spName, params object[] parameterValues) 

 665         { 

 666             if (transaction == null) throw new ArgumentNullException("transaction"); 

 667             if (transaction != null && transaction.Connection == null) throw new ArgumentException("The transaction was rollbacked or commited, please provide an open transaction.", "transaction"); 

 668             if (spName == null || spName.Length == 0) throw new ArgumentNullException("spName");

 669 

 670             if ((parameterValues != null) && (parameterValues.Length > 0)) 

 671             { 

 672                 // 从缓存中加载存储过程参数 

 673                 SqlParameter[] commandParameters = SqlHelperParameterCache.GetSpParameterSet(transaction.Connection, spName);

 674 

 675                 // 给存储过程参数分配值 

 676                 AssignParameterValues(commandParameters, parameterValues);

 677 

 678                 return ExecuteDataset(transaction, CommandType.StoredProcedure, spName, commandParameters); 

 679             } 

 680             else 

 681             { 

 682                 return ExecuteDataset(transaction, CommandType.StoredProcedure, spName); 

 683             } 

 684         }

 685 

 686         #endregion ExecuteDataset数据集命令结束

 687 

 688         #region ExecuteReader 数据阅读器

 689 

 690         /// <summary> 

 691         /// 枚举,标识数据库连接是由SqlHelper提供还是由调用者提供 

 692         /// </summary> 

 693         private enum SqlConnectionOwnership 

 694         { 

 695             /// <summary>由SqlHelper提供连接</summary> 

 696             Internal, 

 697             /// <summary>由调用者提供连接</summary> 

 698             External 

 699         }

 700 

 701         /// <summary> 

 702         /// 执行指定数据库连接对象的数据阅读器. 

 703         /// </summary> 

 704         /// <remarks> 

 705         /// 如果是SqlHelper打开连接,当连接关闭DataReader也将关闭. 

 706         /// 如果是调用都打开连接,DataReader由调用都管理. 

 707         /// </remarks> 

 708         /// <param name="connection">一个有效的数据库连接对象</param> 

 709         /// <param name="transaction">一个有效的事务,或者为 'null'</param> 

 710         /// <param name="commandType">命令类型 (存储过程,命令文本或其它)</param> 

 711         /// <param name="commandText">存储过程名或T-SQL语句</param> 

 712         /// <param name="commandParameters">SqlParameters参数数组,如果没有参数则为'null'</param> 

 713         /// <param name="connectionOwnership">标识数据库连接对象是由调用者提供还是由SqlHelper提供</param> 

 714         /// <returns>返回包含结果集的SqlDataReader</returns> 

 715         private static SqlDataReader ExecuteReader(SqlConnection connection, SqlTransaction transaction, CommandType commandType, string commandText, SqlParameter[] commandParameters, SqlConnectionOwnership connectionOwnership) 

 716         { 

 717             if (connection == null) throw new ArgumentNullException("connection");

 718 

 719             bool mustCloseConnection = false; 

 720             // 创建命令 

 721             SqlCommand cmd = new SqlCommand(); 

 722             try 

 723             { 

 724                 PrepareCommand(cmd, connection, transaction, commandType, commandText, commandParameters, out mustCloseConnection);

 725 

 726                 // 创建数据阅读器 

 727                 SqlDataReader dataReader;

 728 

 729                 if (connectionOwnership == SqlConnectionOwnership.External) 

 730                 { 

 731                     dataReader = cmd.ExecuteReader(); 

 732                 } 

 733                 else 

 734                 { 

 735                     dataReader = cmd.ExecuteReader(CommandBehavior.CloseConnection); 

 736                 }

 737 

 738                 // 清除参数,以便再次使用.. 

 739                 // HACK: There is a problem here, the output parameter values are fletched 

 740                 // when the reader is closed, so if the parameters are detached from the command 

 741                 // then the SqlReader can磘 set its values. 

 742                 // When this happen, the parameters can磘 be used again in other command. 

 743                 bool canClear = true; 

 744                 foreach (SqlParameter commandParameter in cmd.Parameters) 

 745                 { 

 746                     if (commandParameter.Direction != ParameterDirection.Input) 

 747                         canClear = false; 

 748                 }

 749 

 750                 if (canClear) 

 751                 { 

 752                     cmd.Parameters.Clear(); 

 753                 }

 754 

 755                 return dataReader; 

 756             } 

 757             catch 

 758             { 

 759                 if (mustCloseConnection) 

 760                     connection.Close(); 

 761                 throw; 

 762             } 

 763         }

 764 

 765         /// <summary> 

 766         /// 执行指定数据库连接字符串的数据阅读器. 

 767         /// </summary> 

 768         /// <remarks> 

 769         /// 示例:  

 770         ///  SqlDataReader dr = ExecuteReader(connString, CommandType.StoredProcedure, "GetOrders"); 

 771         /// </remarks> 

 772         /// <param name="connectionString">一个有效的数据库连接字符串</param> 

 773         /// <param name="commandType">命令类型 (存储过程,命令文本或其它)</param> 

 774         /// <param name="commandText">存储过程名或T-SQL语句</param> 

 775         /// <returns>返回包含结果集的SqlDataReader</returns> 

 776         public static SqlDataReader ExecuteReader(string connectionString, CommandType commandType, string commandText) 

 777         { 

 778             return ExecuteReader(connectionString, commandType, commandText, (SqlParameter[])null); 

 779         }

 780 

 781         /// <summary> 

 782         /// 执行指定数据库连接字符串的数据阅读器,指定参数. 

 783         /// </summary> 

 784         /// <remarks> 

 785         /// 示例:  

 786         ///  SqlDataReader dr = ExecuteReader(connString, CommandType.StoredProcedure, "GetOrders", new SqlParameter("@prodid", 24)); 

 787         /// </remarks> 

 788         /// <param name="connectionString">一个有效的数据库连接字符串</param> 

 789         /// <param name="commandType">命令类型 (存储过程,命令文本或其它)</param> 

 790         /// <param name="commandText">存储过程名或T-SQL语句</param> 

 791         /// <param name="commandParameters">SqlParamter参数数组(new SqlParameter("@prodid", 24))</param> 

 792         /// <returns>返回包含结果集的SqlDataReader</returns> 

 793         public static SqlDataReader ExecuteReader(string connectionString, CommandType commandType, string commandText, params SqlParameter[] commandParameters) 

 794         { 

 795             if (connectionString == null || connectionString.Length == 0) throw new ArgumentNullException("connectionString"); 

 796             SqlConnection connection = null; 

 797             try 

 798             { 

 799                 connection = new SqlConnection(connectionString); 

 800                 connection.Open();

 801 

 802                 return ExecuteReader(connection, null, commandType, commandText, commandParameters, SqlConnectionOwnership.Internal); 

 803             } 

 804             catch 

 805             { 

 806                 // If we fail to return the SqlDatReader, we need to close the connection ourselves 

 807                 if (connection != null) connection.Close(); 

 808                 throw; 

 809             }

 810 

 811         }

 812 

 813         /// <summary> 

 814         /// 执行指定数据库连接字符串的数据阅读器,指定参数值. 

 815         /// </summary> 

 816         /// <remarks> 

 817         /// 此方法不提供访问存储过程输出参数和返回值参数. 

 818         /// 示例:  

 819         ///  SqlDataReader dr = ExecuteReader(connString, "GetOrders", 24, 36); 

 820         /// </remarks> 

 821         /// <param name="connectionString">一个有效的数据库连接字符串</param> 

 822         /// <param name="spName">存储过程名</param> 

 823         /// <param name="parameterValues">分配给存储过程输入参数的对象数组</param> 

 824         /// <returns>返回包含结果集的SqlDataReader</returns> 

 825         public static SqlDataReader ExecuteReader(string connectionString, string spName, params object[] parameterValues) 

 826         { 

 827             if (connectionString == null || connectionString.Length == 0) throw new ArgumentNullException("connectionString"); 

 828             if (spName == null || spName.Length == 0) throw new ArgumentNullException("spName");

 829 

 830             if ((parameterValues != null) && (parameterValues.Length > 0)) 

 831             { 

 832                 SqlParameter[] commandParameters = SqlHelperParameterCache.GetSpParameterSet(connectionString, spName);

 833 

 834                 AssignParameterValues(commandParameters, parameterValues);

 835 

 836                 return ExecuteReader(connectionString, CommandType.StoredProcedure, spName, commandParameters); 

 837             } 

 838             else 

 839             { 

 840                 return ExecuteReader(connectionString, CommandType.StoredProcedure, spName); 

 841             } 

 842         }

 843 

 844         /// <summary> 

 845         /// 执行指定数据库连接对象的数据阅读器. 

 846         /// </summary> 

 847         /// <remarks> 

 848         /// 示例:  

 849         ///  SqlDataReader dr = ExecuteReader(conn, CommandType.StoredProcedure, "GetOrders"); 

 850         /// </remarks> 

 851         /// <param name="connection">一个有效的数据库连接对象</param> 

 852         /// <param name="commandType">命令类型 (存储过程,命令文本或其它)</param> 

 853         /// <param name="commandText">存储过程名或T-SQL语句</param> 

 854         /// <returns>返回包含结果集的SqlDataReader</returns> 

 855         public static SqlDataReader ExecuteReader(SqlConnection connection, CommandType commandType, string commandText) 

 856         { 

 857             return ExecuteReader(connection, commandType, commandText, (SqlParameter[])null); 

 858         }

 859 

 860         /// <summary> 

 861         /// [调用者方式]执行指定数据库连接对象的数据阅读器,指定参数. 

 862         /// </summary> 

 863         /// <remarks> 

 864         /// 示例:  

 865         ///  SqlDataReader dr = ExecuteReader(conn, CommandType.StoredProcedure, "GetOrders", new SqlParameter("@prodid", 24)); 

 866         /// </remarks> 

 867         /// <param name="connection">一个有效的数据库连接对象</param> 

 868         /// <param name="commandType">命令类型 (存储过程,命令文本或其它)</param> 

 869         /// <param name="commandText">命令类型 (存储过程,命令文本或其它)</param> 

 870         /// <param name="commandParameters">SqlParamter参数数组</param> 

 871         /// <returns>返回包含结果集的SqlDataReader</returns> 

 872         public static SqlDataReader ExecuteReader(SqlConnection connection, CommandType commandType, string commandText, params SqlParameter[] commandParameters) 

 873         { 

 874             return ExecuteReader(connection, (SqlTransaction)null, commandType, commandText, commandParameters, SqlConnectionOwnership.External); 

 875         }

 876 

 877         /// <summary> 

 878         /// [调用者方式]执行指定数据库连接对象的数据阅读器,指定参数值. 

 879         /// </summary> 

 880         /// <remarks> 

 881         /// 此方法不提供访问存储过程输出参数和返回值参数. 

 882         /// 示例:  

 883         ///  SqlDataReader dr = ExecuteReader(conn, "GetOrders", 24, 36); 

 884         /// </remarks> 

 885         /// <param name="connection">一个有效的数据库连接对象</param> 

 886         /// <param name="spName">T存储过程名</param> 

 887         /// <param name="parameterValues">分配给存储过程输入参数的对象数组</param> 

 888         /// <returns>返回包含结果集的SqlDataReader</returns> 

 889         public static SqlDataReader ExecuteReader(SqlConnection connection, string spName, params object[] parameterValues) 

 890         { 

 891             if (connection == null) throw new ArgumentNullException("connection"); 

 892             if (spName == null || spName.Length == 0) throw new ArgumentNullException("spName");

 893 

 894             if ((parameterValues != null) && (parameterValues.Length > 0)) 

 895             { 

 896                 SqlParameter[] commandParameters = SqlHelperParameterCache.GetSpParameterSet(connection, spName);

 897 

 898                 AssignParameterValues(commandParameters, parameterValues);

 899 

 900                 return ExecuteReader(connection, CommandType.StoredProcedure, spName, commandParameters); 

 901             } 

 902             else 

 903             { 

 904                 return ExecuteReader(connection, CommandType.StoredProcedure, spName); 

 905             } 

 906         }

 907 

 908         /// <summary> 

 909         /// [调用者方式]执行指定数据库事务的数据阅读器,指定参数值. 

 910         /// </summary> 

 911         /// <remarks> 

 912         /// 示例:  

 913         ///  SqlDataReader dr = ExecuteReader(trans, CommandType.StoredProcedure, "GetOrders"); 

 914         /// </remarks> 

 915         /// <param name="transaction">一个有效的连接事务</param> 

 916         /// <param name="commandType">命令类型 (存储过程,命令文本或其它)</param> 

 917         /// <param name="commandText">存储过程名称或T-SQL语句</param> 

 918         /// <returns>返回包含结果集的SqlDataReader</returns> 

 919         public static SqlDataReader ExecuteReader(SqlTransaction transaction, CommandType commandType, string commandText) 

 920         { 

 921             return ExecuteReader(transaction, commandType, commandText, (SqlParameter[])null); 

 922         }

 923 

 924         /// <summary> 

 925         /// [调用者方式]执行指定数据库事务的数据阅读器,指定参数. 

 926         /// </summary> 

 927         /// <remarks> 

 928         /// 示例:  

 929         ///   SqlDataReader dr = ExecuteReader(trans, CommandType.StoredProcedure, "GetOrders", new SqlParameter("@prodid", 24)); 

 930         /// </remarks> 

 931         /// <param name="transaction">一个有效的连接事务</param> 

 932         /// <param name="commandType">命令类型 (存储过程,命令文本或其它)</param> 

 933         /// <param name="commandText">存储过程名称或T-SQL语句</param> 

 934         /// <param name="commandParameters">分配给命令的SqlParamter参数数组</param> 

 935         /// <returns>返回包含结果集的SqlDataReader</returns> 

 936         public static SqlDataReader ExecuteReader(SqlTransaction transaction, CommandType commandType, string commandText, params SqlParameter[] commandParameters) 

 937         { 

 938             if (transaction == null) throw new ArgumentNullException("transaction"); 

 939             if (transaction != null && transaction.Connection == null) throw new ArgumentException("The transaction was rollbacked or commited, please provide an open transaction.", "transaction");

 940 

 941             return ExecuteReader(transaction.Connection, transaction, commandType, commandText, commandParameters, SqlConnectionOwnership.External); 

 942         }

 943 

 944         /// <summary> 

 945         /// [调用者方式]执行指定数据库事务的数据阅读器,指定参数值. 

 946         /// </summary> 

 947         /// <remarks> 

 948         /// 此方法不提供访问存储过程输出参数和返回值参数. 

 949         /// 

 950         /// 示例:  

 951         ///  SqlDataReader dr = ExecuteReader(trans, "GetOrders", 24, 36); 

 952         /// </remarks> 

 953         /// <param name="transaction">一个有效的连接事务</param> 

 954         /// <param name="spName">存储过程名称</param> 

 955         /// <param name="parameterValues">分配给存储过程输入参数的对象数组</param> 

 956         /// <returns>返回包含结果集的SqlDataReader</returns> 

 957         public static SqlDataReader ExecuteReader(SqlTransaction transaction, string spName, params object[] parameterValues) 

 958         { 

 959             if (transaction == null) throw new ArgumentNullException("transaction"); 

 960             if (transaction != null && transaction.Connection == null) throw new ArgumentException("The transaction was rollbacked or commited, please provide an open transaction.", "transaction"); 

 961             if (spName == null || spName.Length == 0) throw new ArgumentNullException("spName");

 962 

 963             // 如果有参数值 

 964             if ((parameterValues != null) && (parameterValues.Length > 0)) 

 965             { 

 966                 SqlParameter[] commandParameters = SqlHelperParameterCache.GetSpParameterSet(transaction.Connection, spName);

 967 

 968                 AssignParameterValues(commandParameters, parameterValues);

 969 

 970                 return ExecuteReader(transaction, CommandType.StoredProcedure, spName, commandParameters); 

 971             } 

 972             else 

 973             { 

 974                 // 没有参数值 

 975                 return ExecuteReader(transaction, CommandType.StoredProcedure, spName); 

 976             } 

 977         }

 978 

 979         #endregion ExecuteReader数据阅读器

 980 

 981         #region ExecuteScalar 返回结果集中的第一行第一列

 982 

 983         /// <summary> 

 984         /// 执行指定数据库连接字符串的命令,返回结果集中的第一行第一列. 

 985         /// </summary> 

 986         /// <remarks> 

 987         /// 示例:  

 988         ///  int orderCount = (int)ExecuteScalar(connString, CommandType.StoredProcedure, "GetOrderCount");

 989         /// </remarks> 

 990         /// <param name="connectionString">一个有效的数据库连接字符串</param> 

 991         /// <param name="commandType">命令类型 (存储过程,命令文本或其它)</param> 

 992         /// <param name="commandText">存储过程名称或T-SQL语句</param> 

 993         /// <returns>返回结果集中的第一行第一列</returns> 

 994         public static object ExecuteScalar(string connectionString, CommandType commandType, string commandText) 

 995         { 

 996             // 执行参数为空的方法 

 997             return ExecuteScalar(connectionString, commandType, commandText, (SqlParameter[])null); 

 998         }

 999 

1000         /// <summary> 

1001         /// 执行指定数据库连接字符串的命令,指定参数,返回结果集中的第一行第一列. 

1002         /// </summary> 

1003         /// <remarks> 

1004         /// 示例:  

1005         ///  int orderCount = (int)ExecuteScalar(connString, CommandType.StoredProcedure, "GetOrderCount", new SqlParameter("@prodid", 24)); 

1006         /// </remarks> 

1007         /// <param name="connectionString">一个有效的数据库连接字符串</param> 

1008         /// <param name="commandType">命令类型 (存储过程,命令文本或其它)</param> 

1009         /// <param name="commandText">存储过程名称或T-SQL语句</param> 

1010         /// <param name="commandParameters">分配给命令的SqlParamter参数数组</param> 

1011         /// <returns>返回结果集中的第一行第一列</returns> 

1012         public static object ExecuteScalar(string connectionString, CommandType commandType, string commandText, params SqlParameter[] commandParameters) 

1013         { 

1014             if (connectionString == null || connectionString.Length == 0) throw new ArgumentNullException("connectionString"); 

1015             // 创建并打开数据库连接对象,操作完成释放对象. 

1016             using (SqlConnection connection = new SqlConnection(connectionString)) 

1017             { 

1018                 connection.Open();

1019 

1020                 // 调用指定数据库连接字符串重载方法. 

1021                 return ExecuteScalar(connection, commandType, commandText, commandParameters); 

1022             } 

1023         }

1024 

1025         /// <summary> 

1026         /// 执行指定数据库连接字符串的命令,指定参数值,返回结果集中的第一行第一列. 

1027         /// </summary> 

1028         /// <remarks> 

1029         /// 此方法不提供访问存储过程输出参数和返回值参数. 

1030         /// 

1031         /// 示例:  

1032         ///  int orderCount = (int)ExecuteScalar(connString, "GetOrderCount", 24, 36); 

1033         /// </remarks> 

1034         /// <param name="connectionString">一个有效的数据库连接字符串</param> 

1035         /// <param name="spName">存储过程名称</param> 

1036         /// <param name="parameterValues">分配给存储过程输入参数的对象数组</param> 

1037         /// <returns>返回结果集中的第一行第一列</returns> 

1038         public static object ExecuteScalar(string connectionString, string spName, params object[] parameterValues) 

1039         { 

1040             if (connectionString == null || connectionString.Length == 0) throw new ArgumentNullException("connectionString"); 

1041             if (spName == null || spName.Length == 0) throw new ArgumentNullException("spName");

1042 

1043             // 如果有参数值 

1044             if ((parameterValues != null) && (parameterValues.Length > 0)) 

1045             { 

1046                 // 从缓存中加载存储过程参数,如果缓存中不存在则从数据库中检索参数信息并加载到缓存中. () 

1047                 SqlParameter[] commandParameters = SqlHelperParameterCache.GetSpParameterSet(connectionString, spName);

1048 

1049                 // 给存储过程参数赋值 

1050                 AssignParameterValues(commandParameters, parameterValues);

1051 

1052                 // 调用重载方法 

1053                 return ExecuteScalar(connectionString, CommandType.StoredProcedure, spName, commandParameters); 

1054             } 

1055             else 

1056             { 

1057                 // 没有参数值 

1058                 return ExecuteScalar(connectionString, CommandType.StoredProcedure, spName); 

1059             } 

1060         }

1061 

1062         /// <summary> 

1063         /// 执行指定数据库连接对象的命令,返回结果集中的第一行第一列. 

1064         /// </summary> 

1065         /// <remarks> 

1066         /// 示例:  

1067         ///  int orderCount = (int)ExecuteScalar(conn, CommandType.StoredProcedure, "GetOrderCount"); 

1068         /// </remarks> 

1069         /// <param name="connection">一个有效的数据库连接对象</param> 

1070         /// <param name="commandType">命令类型 (存储过程,命令文本或其它)</param> 

1071         /// <param name="commandText">存储过程名称或T-SQL语句</param> 

1072         /// <returns>返回结果集中的第一行第一列</returns> 

1073         public static object ExecuteScalar(SqlConnection connection, CommandType commandType, string commandText) 

1074         { 

1075             // 执行参数为空的方法 

1076             return ExecuteScalar(connection, commandType, commandText, (SqlParameter[])null); 

1077         }

1078 

1079         /// <summary> 

1080         /// 执行指定数据库连接对象的命令,指定参数,返回结果集中的第一行第一列. 

1081         /// </summary> 

1082         /// <remarks> 

1083         /// 示例:  

1084         ///  int orderCount = (int)ExecuteScalar(conn, CommandType.StoredProcedure, "GetOrderCount", new SqlParameter("@prodid", 24)); 

1085         /// </remarks> 

1086         /// <param name="connection">一个有效的数据库连接对象</param> 

1087         /// <param name="commandType">命令类型 (存储过程,命令文本或其它)</param> 

1088         /// <param name="commandText">存储过程名称或T-SQL语句</param> 

1089         /// <param name="commandParameters">分配给命令的SqlParamter参数数组</param> 

1090         /// <returns>返回结果集中的第一行第一列</returns> 

1091         public static object ExecuteScalar(SqlConnection connection, CommandType commandType, string commandText, params SqlParameter[] commandParameters) 

1092         { 

1093             if (connection == null) throw new ArgumentNullException("connection");

1094 

1095             // 创建SqlCommand命令,并进行预处理 

1096             SqlCommand cmd = new SqlCommand();

1097 

1098             bool mustCloseConnection = false; 

1099             PrepareCommand(cmd, connection, (SqlTransaction)null, commandType, commandText, commandParameters, out mustCloseConnection);

1100 

1101             // 执行SqlCommand命令,并返回结果. 

1102             object retval = cmd.ExecuteScalar();

1103 

1104             // 清除参数,以便再次使用. 

1105             cmd.Parameters.Clear();

1106 

1107             if (mustCloseConnection) 

1108                 connection.Close();

1109 

1110             return retval; 

1111         }

1112 

1113         /// <summary> 

1114         /// 执行指定数据库连接对象的命令,指定参数值,返回结果集中的第一行第一列. 

1115         /// </summary> 

1116         /// <remarks> 

1117         /// 此方法不提供访问存储过程输出参数和返回值参数. 

1118         /// 

1119         /// 示例:  

1120         ///  int orderCount = (int)ExecuteScalar(conn, "GetOrderCount", 24, 36); 

1121         /// </remarks> 

1122         /// <param name="connection">一个有效的数据库连接对象</param> 

1123         /// <param name="spName">存储过程名称</param> 

1124         /// <param name="parameterValues">分配给存储过程输入参数的对象数组</param> 

1125         /// <returns>返回结果集中的第一行第一列</returns> 

1126         public static object ExecuteScalar(SqlConnection connection, string spName, params object[] parameterValues) 

1127         { 

1128             if (connection == null) throw new ArgumentNullException("connection"); 

1129             if (spName == null || spName.Length == 0) throw new ArgumentNullException("spName");

1130 

1131             // 如果有参数值 

1132             if ((parameterValues != null) && (parameterValues.Length > 0)) 

1133             { 

1134                 // 从缓存中加载存储过程参数,如果缓存中不存在则从数据库中检索参数信息并加载到缓存中. () 

1135                 SqlParameter[] commandParameters = SqlHelperParameterCache.GetSpParameterSet(connection, spName);

1136 

1137                 // 给存储过程参数赋值 

1138                 AssignParameterValues(commandParameters, parameterValues);

1139 

1140                 // 调用重载方法 

1141                 return ExecuteScalar(connection, CommandType.StoredProcedure, spName, commandParameters); 

1142             } 

1143             else 

1144             { 

1145                 // 没有参数值 

1146                 return ExecuteScalar(connection, CommandType.StoredProcedure, spName); 

1147             } 

1148         }

1149 

1150         /// <summary> 

1151         /// 执行指定数据库事务的命令,返回结果集中的第一行第一列. 

1152         /// </summary> 

1153         /// <remarks> 

1154         /// 示例:  

1155         ///  int orderCount = (int)ExecuteScalar(trans, CommandType.StoredProcedure, "GetOrderCount"); 

1156         /// </remarks> 

1157         /// <param name="transaction">一个有效的连接事务</param> 

1158         /// <param name="commandType">命令类型 (存储过程,命令文本或其它)</param> 

1159         /// <param name="commandText">存储过程名称或T-SQL语句</param> 

1160         /// <returns>返回结果集中的第一行第一列</returns> 

1161         public static object ExecuteScalar(SqlTransaction transaction, CommandType commandType, string commandText) 

1162         { 

1163             // 执行参数为空的方法 

1164             return ExecuteScalar(transaction, commandType, commandText, (SqlParameter[])null); 

1165         }

1166 

1167         /// <summary> 

1168         /// 执行指定数据库事务的命令,指定参数,返回结果集中的第一行第一列. 

1169         /// </summary> 

1170         /// <remarks> 

1171         /// 示例:  

1172         ///  int orderCount = (int)ExecuteScalar(trans, CommandType.StoredProcedure, "GetOrderCount", new SqlParameter("@prodid", 24)); 

1173         /// </remarks> 

1174         /// <param name="transaction">一个有效的连接事务</param> 

1175         /// <param name="commandType">命令类型 (存储过程,命令文本或其它)</param> 

1176         /// <param name="commandText">存储过程名称或T-SQL语句</param> 

1177         /// <param name="commandParameters">分配给命令的SqlParamter参数数组</param> 

1178         /// <returns>返回结果集中的第一行第一列</returns> 

1179         public static object ExecuteScalar(SqlTransaction transaction, CommandType commandType, string commandText, params SqlParameter[] commandParameters) 

1180         { 

1181             if (transaction == null) throw new ArgumentNullException("transaction"); 

1182             if (transaction != null && transaction.Connection == null) throw new ArgumentException("The transaction was rollbacked or commited, please provide an open transaction.", "transaction");

1183 

1184             // 创建SqlCommand命令,并进行预处理 

1185             SqlCommand cmd = new SqlCommand(); 

1186             bool mustCloseConnection = false; 

1187             PrepareCommand(cmd, transaction.Connection, transaction, commandType, commandText, commandParameters, out mustCloseConnection);

1188 

1189             // 执行SqlCommand命令,并返回结果. 

1190             object retval = cmd.ExecuteScalar();

1191 

1192             // 清除参数,以便再次使用. 

1193             cmd.Parameters.Clear(); 

1194             return retval; 

1195         }

1196 

1197         /// <summary> 

1198         /// 执行指定数据库事务的命令,指定参数值,返回结果集中的第一行第一列. 

1199         /// </summary> 

1200         /// <remarks> 

1201         /// 此方法不提供访问存储过程输出参数和返回值参数. 

1202         /// 

1203         /// 示例:  

1204         ///  int orderCount = (int)ExecuteScalar(trans, "GetOrderCount", 24, 36); 

1205         /// </remarks> 

1206         /// <param name="transaction">一个有效的连接事务</param> 

1207         /// <param name="spName">存储过程名称</param> 

1208         /// <param name="parameterValues">分配给存储过程输入参数的对象数组</param> 

1209         /// <returns>返回结果集中的第一行第一列</returns> 

1210         public static object ExecuteScalar(SqlTransaction transaction, string spName, params object[] parameterValues) 

1211         { 

1212             if (transaction == null) throw new ArgumentNullException("transaction"); 

1213             if (transaction != null && transaction.Connection == null) throw new ArgumentException("The transaction was rollbacked or commited, please provide an open transaction.", "transaction"); 

1214             if (spName == null || spName.Length == 0) throw new ArgumentNullException("spName");

1215 

1216             // 如果有参数值 

1217             if ((parameterValues != null) && (parameterValues.Length > 0)) 

1218             { 

1219                 // PPull the parameters for this stored procedure from the parameter cache () 

1220                 SqlParameter[] commandParameters = SqlHelperParameterCache.GetSpParameterSet(transaction.Connection, spName);

1221 

1222                 // 给存储过程参数赋值 

1223                 AssignParameterValues(commandParameters, parameterValues);

1224 

1225                 // 调用重载方法 

1226                 return ExecuteScalar(transaction, CommandType.StoredProcedure, spName, commandParameters); 

1227             } 

1228             else 

1229             { 

1230                 // 没有参数值 

1231                 return ExecuteScalar(transaction, CommandType.StoredProcedure, spName); 

1232             } 

1233         }

1234 

1235         #endregion ExecuteScalar

1236 

1237         #region ExecuteXmlReader XML阅读器 

1238         /// <summary> 

1239         /// 执行指定数据库连接对象的SqlCommand命令,并产生一个XmlReader对象做为结果集返回. 

1240         /// </summary> 

1241         /// <remarks> 

1242         /// 示例:  

1243         ///  XmlReader r = ExecuteXmlReader(conn, CommandType.StoredProcedure, "GetOrders"); 

1244         /// </remarks> 

1245         /// <param name="connection">一个有效的数据库连接对象</param> 

1246         /// <param name="commandType">命令类型 (存储过程,命令文本或其它)</param> 

1247         /// <param name="commandText">存储过程名称或T-SQL语句 using "FOR XML AUTO"</param> 

1248         /// <returns>返回XmlReader结果集对象.</returns> 

1249         public static XmlReader ExecuteXmlReader(SqlConnection connection, CommandType commandType, string commandText) 

1250         { 

1251             // 执行参数为空的方法 

1252             return ExecuteXmlReader(connection, commandType, commandText, (SqlParameter[])null); 

1253         }

1254 

1255         /// <summary> 

1256         /// 执行指定数据库连接对象的SqlCommand命令,并产生一个XmlReader对象做为结果集返回,指定参数. 

1257         /// </summary> 

1258         /// <remarks> 

1259         /// 示例:  

1260         ///  XmlReader r = ExecuteXmlReader(conn, CommandType.StoredProcedure, "GetOrders", new SqlParameter("@prodid", 24)); 

1261         /// </remarks> 

1262         /// <param name="connection">一个有效的数据库连接对象</param> 

1263         /// <param name="commandType">命令类型 (存储过程,命令文本或其它)</param> 

1264         /// <param name="commandText">存储过程名称或T-SQL语句 using "FOR XML AUTO"</param> 

1265         /// <param name="commandParameters">分配给命令的SqlParamter参数数组</param> 

1266         /// <returns>返回XmlReader结果集对象.</returns> 

1267         public static XmlReader ExecuteXmlReader(SqlConnection connection, CommandType commandType, string commandText, params SqlParameter[] commandParameters) 

1268         { 

1269             if (connection == null) throw new ArgumentNullException("connection");

1270 

1271             bool mustCloseConnection = false; 

1272             // 创建SqlCommand命令,并进行预处理 

1273             SqlCommand cmd = new SqlCommand(); 

1274             try 

1275             { 

1276                 PrepareCommand(cmd, connection, (SqlTransaction)null, commandType, commandText, commandParameters, out mustCloseConnection);

1277 

1278                 // 执行命令 

1279                 XmlReader retval = cmd.ExecuteXmlReader();

1280 

1281                 // 清除参数,以便再次使用. 

1282                 cmd.Parameters.Clear();

1283 

1284                 return retval; 

1285             } 

1286             catch 

1287             { 

1288                 if (mustCloseConnection) 

1289                     connection.Close(); 

1290                 throw; 

1291             } 

1292         }

1293 

1294         /// <summary> 

1295         /// 执行指定数据库连接对象的SqlCommand命令,并产生一个XmlReader对象做为结果集返回,指定参数值. 

1296         /// </summary> 

1297         /// <remarks> 

1298         /// 此方法不提供访问存储过程输出参数和返回值参数. 

1299         /// 

1300         /// 示例:  

1301         ///  XmlReader r = ExecuteXmlReader(conn, "GetOrders", 24, 36); 

1302         /// </remarks> 

1303         /// <param name="connection">一个有效的数据库连接对象</param> 

1304         /// <param name="spName">存储过程名称 using "FOR XML AUTO"</param> 

1305         /// <param name="parameterValues">分配给存储过程输入参数的对象数组</param> 

1306         /// <returns>返回XmlReader结果集对象.</returns> 

1307         public static XmlReader ExecuteXmlReader(SqlConnection connection, string spName, params object[] parameterValues) 

1308         { 

1309             if (connection == null) throw new ArgumentNullException("connection"); 

1310             if (spName == null || spName.Length == 0) throw new ArgumentNullException("spName");

1311 

1312             // 如果有参数值 

1313             if ((parameterValues != null) && (parameterValues.Length > 0)) 

1314             { 

1315                 // 从缓存中加载存储过程参数,如果缓存中不存在则从数据库中检索参数信息并加载到缓存中. () 

1316                 SqlParameter[] commandParameters = SqlHelperParameterCache.GetSpParameterSet(connection, spName);

1317 

1318                 // 给存储过程参数赋值 

1319                 AssignParameterValues(commandParameters, parameterValues);

1320 

1321                 // 调用重载方法 

1322                 return ExecuteXmlReader(connection, CommandType.StoredProcedure, spName, commandParameters); 

1323             } 

1324             else 

1325             { 

1326                 // 没有参数值 

1327                 return ExecuteXmlReader(connection, CommandType.StoredProcedure, spName); 

1328             } 

1329         }

1330 

1331         /// <summary> 

1332         /// 执行指定数据库事务的SqlCommand命令,并产生一个XmlReader对象做为结果集返回. 

1333         /// </summary> 

1334         /// <remarks> 

1335         /// 示例:  

1336         ///  XmlReader r = ExecuteXmlReader(trans, CommandType.StoredProcedure, "GetOrders"); 

1337         /// </remarks> 

1338         /// <param name="transaction">一个有效的连接事务</param> 

1339         /// <param name="commandType">命令类型 (存储过程,命令文本或其它)</param> 

1340         /// <param name="commandText">存储过程名称或T-SQL语句 using "FOR XML AUTO"</param> 

1341         /// <returns>返回XmlReader结果集对象.</returns> 

1342         public static XmlReader ExecuteXmlReader(SqlTransaction transaction, CommandType commandType, string commandText) 

1343         { 

1344             // 执行参数为空的方法 

1345             return ExecuteXmlReader(transaction, commandType, commandText, (SqlParameter[])null); 

1346         }

1347 

1348         /// <summary> 

1349         /// 执行指定数据库事务的SqlCommand命令,并产生一个XmlReader对象做为结果集返回,指定参数. 

1350         /// </summary> 

1351         /// <remarks> 

1352         /// 示例:  

1353         ///  XmlReader r = ExecuteXmlReader(trans, CommandType.StoredProcedure, "GetOrders", new SqlParameter("@prodid", 24)); 

1354         /// </remarks> 

1355         /// <param name="transaction">一个有效的连接事务</param> 

1356         /// <param name="commandType">命令类型 (存储过程,命令文本或其它)</param> 

1357         /// <param name="commandText">存储过程名称或T-SQL语句 using "FOR XML AUTO"</param> 

1358         /// <param name="commandParameters">分配给命令的SqlParamter参数数组</param> 

1359         /// <returns>返回XmlReader结果集对象.</returns> 

1360         public static XmlReader ExecuteXmlReader(SqlTransaction transaction, CommandType commandType, string commandText, params SqlParameter[] commandParameters) 

1361         { 

1362             if (transaction == null) throw new ArgumentNullException("transaction"); 

1363             if (transaction != null && transaction.Connection == null) throw new ArgumentException("The transaction was rollbacked or commited, please provide an open transaction.", "transaction");

1364 

1365             // 创建SqlCommand命令,并进行预处理 

1366             SqlCommand cmd = new SqlCommand(); 

1367             bool mustCloseConnection = false; 

1368             PrepareCommand(cmd, transaction.Connection, transaction, commandType, commandText, commandParameters, out mustCloseConnection);

1369 

1370             // 执行命令 

1371             XmlReader retval = cmd.ExecuteXmlReader();

1372 

1373             // 清除参数,以便再次使用. 

1374             cmd.Parameters.Clear(); 

1375             return retval; 

1376         }

1377 

1378         /// <summary> 

1379         /// 执行指定数据库事务的SqlCommand命令,并产生一个XmlReader对象做为结果集返回,指定参数值. 

1380         /// </summary> 

1381         /// <remarks> 

1382         /// 此方法不提供访问存储过程输出参数和返回值参数. 

1383         /// 

1384         /// 示例:  

1385         ///  XmlReader r = ExecuteXmlReader(trans, "GetOrders", 24, 36); 

1386         /// </remarks> 

1387         /// <param name="transaction">一个有效的连接事务</param> 

1388         /// <param name="spName">存储过程名称</param> 

1389         /// <param name="parameterValues">分配给存储过程输入参数的对象数组</param> 

1390         /// <returns>返回一个包含结果集的DataSet.</returns> 

1391         public static XmlReader ExecuteXmlReader(SqlTransaction transaction, string spName, params object[] parameterValues) 

1392         { 

1393             if (transaction == null) throw new ArgumentNullException("transaction"); 

1394             if (transaction != null && transaction.Connection == null) throw new ArgumentException("The transaction was rollbacked or commited, please provide an open transaction.", "transaction"); 

1395             if (spName == null || spName.Length == 0) throw new ArgumentNullException("spName");

1396 

1397             // 如果有参数值 

1398             if ((parameterValues != null) && (parameterValues.Length > 0)) 

1399             { 

1400                 // 从缓存中加载存储过程参数,如果缓存中不存在则从数据库中检索参数信息并加载到缓存中. () 

1401                 SqlParameter[] commandParameters = SqlHelperParameterCache.GetSpParameterSet(transaction.Connection, spName);

1402 

1403                 // 给存储过程参数赋值 

1404                 AssignParameterValues(commandParameters, parameterValues);

1405 

1406                 // 调用重载方法 

1407                 return ExecuteXmlReader(transaction, CommandType.StoredProcedure, spName, commandParameters); 

1408             } 

1409             else 

1410             { 

1411                 // 没有参数值 

1412                 return ExecuteXmlReader(transaction, CommandType.StoredProcedure, spName); 

1413             } 

1414         }

1415 

1416         #endregion ExecuteXmlReader 阅读器结束

1417 

1418         #region FillDataset 填充数据集 

1419         /// <summary> 

1420         /// 执行指定数据库连接字符串的命令,映射数据表并填充数据集. 

1421         /// </summary> 

1422         /// <remarks> 

1423         /// 示例:  

1424         ///  FillDataset(connString, CommandType.StoredProcedure, "GetOrders", ds, new string[] {"orders"}); 

1425         /// </remarks> 

1426         /// <param name="connectionString">一个有效的数据库连接字符串</param> 

1427         /// <param name="commandType">命令类型 (存储过程,命令文本或其它)</param> 

1428         /// <param name="commandText">存储过程名称或T-SQL语句</param> 

1429         /// <param name="dataSet">要填充结果集的DataSet实例</param> 

1430         /// <param name="tableNames">表映射的数据表数组 

1431         /// 用户定义的表名 (可有是实际的表名.)</param> 

1432         public static void FillDataset(string connectionString, CommandType commandType, string commandText, DataSet dataSet, string[] tableNames) 

1433         { 

1434             if (connectionString == null || connectionString.Length == 0) throw new ArgumentNullException("connectionString"); 

1435             if (dataSet == null) throw new ArgumentNullException("dataSet");

1436 

1437             // 创建并打开数据库连接对象,操作完成释放对象. 

1438             using (SqlConnection connection = new SqlConnection(connectionString)) 

1439             { 

1440                 connection.Open();

1441 

1442                 // 调用指定数据库连接字符串重载方法. 

1443                 FillDataset(connection, commandType, commandText, dataSet, tableNames); 

1444             } 

1445         }

1446 

1447         /// <summary> 

1448         /// 执行指定数据库连接字符串的命令,映射数据表并填充数据集.指定命令参数. 

1449         /// </summary> 

1450         /// <remarks> 

1451         /// 示例:  

1452         ///  FillDataset(connString, CommandType.StoredProcedure, "GetOrders", ds, new string[] {"orders"}, new SqlParameter("@prodid", 24)); 

1453         /// </remarks> 

1454         /// <param name="connectionString">一个有效的数据库连接字符串</param> 

1455         /// <param name="commandType">命令类型 (存储过程,命令文本或其它)</param> 

1456         /// <param name="commandText">存储过程名称或T-SQL语句</param> 

1457         /// <param name="commandParameters">分配给命令的SqlParamter参数数组</param> 

1458         /// <param name="dataSet">要填充结果集的DataSet实例</param> 

1459         /// <param name="tableNames">表映射的数据表数组 

1460         /// 用户定义的表名 (可有是实际的表名.) 

1461         /// </param> 

1462         public static void FillDataset(string connectionString, CommandType commandType, 

1463             string commandText, DataSet dataSet, string[] tableNames, 

1464             params SqlParameter[] commandParameters) 

1465         { 

1466             if (connectionString == null || connectionString.Length == 0) throw new ArgumentNullException("connectionString"); 

1467             if (dataSet == null) throw new ArgumentNullException("dataSet"); 

1468             // 创建并打开数据库连接对象,操作完成释放对象. 

1469             using (SqlConnection connection = new SqlConnection(connectionString)) 

1470             { 

1471                 connection.Open();

1472 

1473                 // 调用指定数据库连接字符串重载方法. 

1474                 FillDataset(connection, commandType, commandText, dataSet, tableNames, commandParameters); 

1475             } 

1476         }

1477 

1478         /// <summary> 

1479         /// 执行指定数据库连接字符串的命令,映射数据表并填充数据集,指定存储过程参数值. 

1480         /// </summary> 

1481         /// <remarks> 

1482         /// 此方法不提供访问存储过程输出参数和返回值参数. 

1483         /// 

1484         /// 示例:  

1485         ///  FillDataset(connString, CommandType.StoredProcedure, "GetOrders", ds, new string[] {"orders"}, 24); 

1486         /// </remarks> 

1487         /// <param name="connectionString">一个有效的数据库连接字符串</param> 

1488         /// <param name="spName">存储过程名称</param> 

1489         /// <param name="dataSet">要填充结果集的DataSet实例</param> 

1490         /// <param name="tableNames">表映射的数据表数组 

1491         /// 用户定义的表名 (可有是实际的表名.) 

1492         /// </param>    

1493         /// <param name="parameterValues">分配给存储过程输入参数的对象数组</param> 

1494         public static void FillDataset(string connectionString, string spName, 

1495             DataSet dataSet, string[] tableNames, 

1496             params object[] parameterValues) 

1497         { 

1498             if (connectionString == null || connectionString.Length == 0) throw new ArgumentNullException("connectionString"); 

1499             if (dataSet == null) throw new ArgumentNullException("dataSet"); 

1500             // 创建并打开数据库连接对象,操作完成释放对象. 

1501             using (SqlConnection connection = new SqlConnection(connectionString)) 

1502             { 

1503                 connection.Open();

1504 

1505                 // 调用指定数据库连接字符串重载方法. 

1506                 FillDataset(connection, spName, dataSet, tableNames, parameterValues); 

1507             } 

1508         }

1509 

1510         /// <summary> 

1511         /// 执行指定数据库连接对象的命令,映射数据表并填充数据集. 

1512         /// </summary> 

1513         /// <remarks> 

1514         /// 示例:  

1515         ///  FillDataset(conn, CommandType.StoredProcedure, "GetOrders", ds, new string[] {"orders"}); 

1516         /// </remarks> 

1517         /// <param name="connection">一个有效的数据库连接对象</param> 

1518         /// <param name="commandType">命令类型 (存储过程,命令文本或其它)</param> 

1519         /// <param name="commandText">存储过程名称或T-SQL语句</param> 

1520         /// <param name="dataSet">要填充结果集的DataSet实例</param> 

1521         /// <param name="tableNames">表映射的数据表数组 

1522         /// 用户定义的表名 (可有是实际的表名.) 

1523         /// </param>    

1524         public static void FillDataset(SqlConnection connection, CommandType commandType, 

1525             string commandText, DataSet dataSet, string[] tableNames) 

1526         { 

1527             FillDataset(connection, commandType, commandText, dataSet, tableNames, null); 

1528         }

1529 

1530         /// <summary> 

1531         /// 执行指定数据库连接对象的命令,映射数据表并填充数据集,指定参数. 

1532         /// </summary> 

1533         /// <remarks> 

1534         /// 示例:  

1535         ///  FillDataset(conn, CommandType.StoredProcedure, "GetOrders", ds, new string[] {"orders"}, new SqlParameter("@prodid", 24)); 

1536         /// </remarks> 

1537         /// <param name="connection">一个有效的数据库连接对象</param> 

1538         /// <param name="commandType">命令类型 (存储过程,命令文本或其它)</param> 

1539         /// <param name="commandText">存储过程名称或T-SQL语句</param> 

1540         /// <param name="dataSet">要填充结果集的DataSet实例</param> 

1541         /// <param name="tableNames">表映射的数据表数组 

1542         /// 用户定义的表名 (可有是实际的表名.) 

1543         /// </param> 

1544         /// <param name="commandParameters">分配给命令的SqlParamter参数数组</param> 

1545         public static void FillDataset(SqlConnection connection, CommandType commandType, 

1546             string commandText, DataSet dataSet, string[] tableNames, 

1547             params SqlParameter[] commandParameters) 

1548         { 

1549             FillDataset(connection, null, commandType, commandText, dataSet, tableNames, commandParameters); 

1550         }

1551 

1552         /// <summary> 

1553         /// 执行指定数据库连接对象的命令,映射数据表并填充数据集,指定存储过程参数值. 

1554         /// </summary> 

1555         /// <remarks> 

1556         /// 此方法不提供访问存储过程输出参数和返回值参数. 

1557         /// 

1558         /// 示例:  

1559         ///  FillDataset(conn, "GetOrders", ds, new string[] {"orders"}, 24, 36); 

1560         /// </remarks> 

1561         /// <param name="connection">一个有效的数据库连接对象</param> 

1562         /// <param name="spName">存储过程名称</param> 

1563         /// <param name="dataSet">要填充结果集的DataSet实例</param> 

1564         /// <param name="tableNames">表映射的数据表数组 

1565         /// 用户定义的表名 (可有是实际的表名.) 

1566         /// </param> 

1567         /// <param name="parameterValues">分配给存储过程输入参数的对象数组</param> 

1568         public static void FillDataset(SqlConnection connection, string spName, 

1569             DataSet dataSet, string[] tableNames, 

1570             params object[] parameterValues) 

1571         { 

1572             if (connection == null) throw new ArgumentNullException("connection"); 

1573             if (dataSet == null) throw new ArgumentNullException("dataSet"); 

1574             if (spName == null || spName.Length == 0) throw new ArgumentNullException("spName");

1575 

1576             // 如果有参数值 

1577             if ((parameterValues != null) && (parameterValues.Length > 0)) 

1578             { 

1579                 // 从缓存中加载存储过程参数,如果缓存中不存在则从数据库中检索参数信息并加载到缓存中. () 

1580                 SqlParameter[] commandParameters = SqlHelperParameterCache.GetSpParameterSet(connection, spName);

1581 

1582                 // 给存储过程参数赋值 

1583                 AssignParameterValues(commandParameters, parameterValues);

1584 

1585                 // 调用重载方法 

1586                 FillDataset(connection, CommandType.StoredProcedure, spName, dataSet, tableNames, commandParameters); 

1587             } 

1588             else 

1589             { 

1590                 // 没有参数值 

1591                 FillDataset(connection, CommandType.StoredProcedure, spName, dataSet, tableNames); 

1592             } 

1593         }

1594 

1595         /// <summary> 

1596         /// 执行指定数据库事务的命令,映射数据表并填充数据集. 

1597         /// </summary> 

1598         /// <remarks> 

1599         /// 示例:  

1600         ///  FillDataset(trans, CommandType.StoredProcedure, "GetOrders", ds, new string[] {"orders"}); 

1601         /// </remarks> 

1602         /// <param name="transaction">一个有效的连接事务</param> 

1603         /// <param name="commandType">命令类型 (存储过程,命令文本或其它)</param> 

1604         /// <param name="commandText">存储过程名称或T-SQL语句</param> 

1605         /// <param name="dataSet">要填充结果集的DataSet实例</param> 

1606         /// <param name="tableNames">表映射的数据表数组 

1607         /// 用户定义的表名 (可有是实际的表名.) 

1608         /// </param> 

1609         public static void FillDataset(SqlTransaction transaction, CommandType commandType, 

1610             string commandText, 

1611             DataSet dataSet, string[] tableNames) 

1612         { 

1613             FillDataset(transaction, commandType, commandText, dataSet, tableNames, null); 

1614         }

1615 

1616         /// <summary> 

1617         /// 执行指定数据库事务的命令,映射数据表并填充数据集,指定参数. 

1618         /// </summary> 

1619         /// <remarks> 

1620         /// 示例:  

1621         ///  FillDataset(trans, CommandType.StoredProcedure, "GetOrders", ds, new string[] {"orders"}, new SqlParameter("@prodid", 24)); 

1622         /// </remarks> 

1623         /// <param name="transaction">一个有效的连接事务</param> 

1624         /// <param name="commandType">命令类型 (存储过程,命令文本或其它)</param> 

1625         /// <param name="commandText">存储过程名称或T-SQL语句</param> 

1626         /// <param name="dataSet">要填充结果集的DataSet实例</param> 

1627         /// <param name="tableNames">表映射的数据表数组 

1628         /// 用户定义的表名 (可有是实际的表名.) 

1629         /// </param> 

1630         /// <param name="commandParameters">分配给命令的SqlParamter参数数组</param> 

1631         public static void FillDataset(SqlTransaction transaction, CommandType commandType, 

1632             string commandText, DataSet dataSet, string[] tableNames, 

1633             params SqlParameter[] commandParameters) 

1634         { 

1635             FillDataset(transaction.Connection, transaction, commandType, commandText, dataSet, tableNames, commandParameters); 

1636         }

1637 

1638         /// <summary> 

1639         /// 执行指定数据库事务的命令,映射数据表并填充数据集,指定存储过程参数值. 

1640         /// </summary> 

1641         /// <remarks> 

1642         /// 此方法不提供访问存储过程输出参数和返回值参数. 

1643         /// 

1644         /// 示例:  

1645         ///  FillDataset(trans, "GetOrders", ds, new string[]{"orders"}, 24, 36); 

1646         /// </remarks> 

1647         /// <param name="transaction">一个有效的连接事务</param> 

1648         /// <param name="spName">存储过程名称</param> 

1649         /// <param name="dataSet">要填充结果集的DataSet实例</param> 

1650         /// <param name="tableNames">表映射的数据表数组 

1651         /// 用户定义的表名 (可有是实际的表名.) 

1652         /// </param> 

1653         /// <param name="parameterValues">分配给存储过程输入参数的对象数组</param> 

1654         public static void FillDataset(SqlTransaction transaction, string spName, 

1655             DataSet dataSet, string[] tableNames, 

1656             params object[] parameterValues) 

1657         { 

1658             if (transaction == null) throw new ArgumentNullException("transaction"); 

1659             if (transaction != null && transaction.Connection == null) throw new ArgumentException("The transaction was rollbacked or commited, please provide an open transaction.", "transaction"); 

1660             if (dataSet == null) throw new ArgumentNullException("dataSet"); 

1661             if (spName == null || spName.Length == 0) throw new ArgumentNullException("spName");

1662 

1663             // 如果有参数值 

1664             if ((parameterValues != null) && (parameterValues.Length > 0)) 

1665             { 

1666                 // 从缓存中加载存储过程参数,如果缓存中不存在则从数据库中检索参数信息并加载到缓存中. () 

1667                 SqlParameter[] commandParameters = SqlHelperParameterCache.GetSpParameterSet(transaction.Connection, spName);

1668 

1669                 // 给存储过程参数赋值 

1670                 AssignParameterValues(commandParameters, parameterValues);

1671 

1672                 // 调用重载方法 

1673                 FillDataset(transaction, CommandType.StoredProcedure, spName, dataSet, tableNames, commandParameters); 

1674             } 

1675             else 

1676             { 

1677                 // 没有参数值 

1678                 FillDataset(transaction, CommandType.StoredProcedure, spName, dataSet, tableNames); 

1679             } 

1680         }

1681 

1682         /// <summary> 

1683         /// [私有方法][内部调用]执行指定数据库连接对象/事务的命令,映射数据表并填充数据集,DataSet/TableNames/SqlParameters. 

1684         /// </summary> 

1685         /// <remarks> 

1686         /// 示例:  

1687         ///  FillDataset(conn, trans, CommandType.StoredProcedure, "GetOrders", ds, new string[] {"orders"}, new SqlParameter("@prodid", 24)); 

1688         /// </remarks> 

1689         /// <param name="connection">一个有效的数据库连接对象</param> 

1690         /// <param name="transaction">一个有效的连接事务</param> 

1691         /// <param name="commandType">命令类型 (存储过程,命令文本或其它)</param> 

1692         /// <param name="commandText">存储过程名称或T-SQL语句</param> 

1693         /// <param name="dataSet">要填充结果集的DataSet实例</param> 

1694         /// <param name="tableNames">表映射的数据表数组 

1695         /// 用户定义的表名 (可有是实际的表名.) 

1696         /// </param> 

1697         /// <param name="commandParameters">分配给命令的SqlParamter参数数组</param> 

1698         private static void FillDataset(SqlConnection connection, SqlTransaction transaction, CommandType commandType, 

1699             string commandText, DataSet dataSet, string[] tableNames, 

1700             params SqlParameter[] commandParameters) 

1701         { 

1702             if (connection == null) throw new ArgumentNullException("connection"); 

1703             if (dataSet == null) throw new ArgumentNullException("dataSet");

1704 

1705             // 创建SqlCommand命令,并进行预处理 

1706             SqlCommand command = new SqlCommand(); 

1707             bool mustCloseConnection = false; 

1708             PrepareCommand(command, connection, transaction, commandType, commandText, commandParameters, out mustCloseConnection);

1709 

1710             // 执行命令 

1711             using (SqlDataAdapter dataAdapter = new SqlDataAdapter(command)) 

1712             {

1713 

1714                 // 追加表映射 

1715                 if (tableNames != null && tableNames.Length > 0) 

1716                 { 

1717                     string tableName = "Table"; 

1718                     for (int index = 0; index < tableNames.Length; index++) 

1719                     { 

1720                         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"); 

1721                         dataAdapter.TableMappings.Add(tableName, tableNames[index]); 

1722                         tableName += (index + 1).ToString(); 

1723                     } 

1724                 }

1725 

1726                 // 填充数据集使用默认表名称 

1727                 dataAdapter.Fill(dataSet);

1728 

1729                 // 清除参数,以便再次使用. 

1730                 command.Parameters.Clear(); 

1731             }

1732 

1733             if (mustCloseConnection) 

1734                 connection.Close(); 

1735         } 

1736         #endregion

1737 

1738         #region UpdateDataset 更新数据集 

1739         /// <summary> 

1740         /// 执行数据集更新到数据库,指定inserted, updated, or deleted命令. 

1741         /// </summary> 

1742         /// <remarks> 

1743         /// 示例:  

1744         ///  UpdateDataset(conn, insertCommand, deleteCommand, updateCommand, dataSet, "Order"); 

1745         /// </remarks> 

1746         /// <param name="insertCommand">[追加记录]一个有效的T-SQL语句或存储过程</param> 

1747         /// <param name="deleteCommand">[删除记录]一个有效的T-SQL语句或存储过程</param> 

1748         /// <param name="updateCommand">[更新记录]一个有效的T-SQL语句或存储过程</param> 

1749         /// <param name="dataSet">要更新到数据库的DataSet</param> 

1750         /// <param name="tableName">要更新到数据库的DataTable</param> 

1751         public static void UpdateDataset(SqlCommand insertCommand, SqlCommand deleteCommand, SqlCommand updateCommand, DataSet dataSet, string tableName) 

1752         { 

1753             if (insertCommand == null) throw new ArgumentNullException("insertCommand"); 

1754             if (deleteCommand == null) throw new ArgumentNullException("deleteCommand"); 

1755             if (updateCommand == null) throw new ArgumentNullException("updateCommand"); 

1756             if (tableName == null || tableName.Length == 0) throw new ArgumentNullException("tableName");

1757 

1758             // 创建SqlDataAdapter,当操作完成后释放. 

1759             using (SqlDataAdapter dataAdapter = new SqlDataAdapter()) 

1760             { 

1761                 // 设置数据适配器命令 

1762                 dataAdapter.UpdateCommand = updateCommand; 

1763                 dataAdapter.InsertCommand = insertCommand; 

1764                 dataAdapter.DeleteCommand = deleteCommand;

1765 

1766                 // 更新数据集改变到数据库 

1767                 dataAdapter.Update(dataSet, tableName);

1768 

1769                 // 提交所有改变到数据集. 

1770                 dataSet.AcceptChanges(); 

1771             } 

1772         } 

1773         #endregion

1774 

1775         #region CreateCommand 创建一条SqlCommand命令 

1776         /// <summary> 

1777         /// 创建SqlCommand命令,指定数据库连接对象,存储过程名和参数. 

1778         /// </summary> 

1779         /// <remarks> 

1780         /// 示例:  

1781         ///  SqlCommand command = CreateCommand(conn, "AddCustomer", "CustomerID", "CustomerName"); 

1782         /// </remarks> 

1783         /// <param name="connection">一个有效的数据库连接对象</param> 

1784         /// <param name="spName">存储过程名称</param> 

1785         /// <param name="sourceColumns">源表的列名称数组</param> 

1786         /// <returns>返回SqlCommand命令</returns> 

1787         public static SqlCommand CreateCommand(SqlConnection connection, string spName, params string[] sourceColumns) 

1788         { 

1789             if (connection == null) throw new ArgumentNullException("connection"); 

1790             if (spName == null || spName.Length == 0) throw new ArgumentNullException("spName");

1791 

1792             // 创建命令 

1793             SqlCommand cmd = new SqlCommand(spName, connection); 

1794             cmd.CommandType = CommandType.StoredProcedure;

1795 

1796             // 如果有参数值 

1797             if ((sourceColumns != null) && (sourceColumns.Length > 0)) 

1798             { 

1799                 // 从缓存中加载存储过程参数,如果缓存中不存在则从数据库中检索参数信息并加载到缓存中. () 

1800                 SqlParameter[] commandParameters = SqlHelperParameterCache.GetSpParameterSet(connection, spName);

1801 

1802                 // 将源表的列到映射到DataSet命令中. 

1803                 for (int index = 0; index < sourceColumns.Length; index++) 

1804                     commandParameters[index].SourceColumn = sourceColumns[index];

1805 

1806                 // Attach the discovered parameters to the SqlCommand object 

1807                 AttachParameters(cmd, commandParameters); 

1808             }

1809 

1810             return cmd; 

1811         } 

1812         #endregion

1813 

1814         #region ExecuteNonQueryTypedParams 类型化参数(DataRow) 

1815         /// <summary> 

1816         /// 执行指定连接数据库连接字符串的存储过程,使用DataRow做为参数值,返回受影响的行数. 

1817         /// </summary> 

1818         /// <param name="connectionString">一个有效的数据库连接字符串</param> 

1819         /// <param name="spName">存储过程名称</param> 

1820         /// <param name="dataRow">使用DataRow作为参数值</param> 

1821         /// <returns>返回影响的行数</returns> 

1822         public static int ExecuteNonQueryTypedParams(String connectionString, String spName, DataRow dataRow) 

1823         { 

1824             if (connectionString == null || connectionString.Length == 0) throw new ArgumentNullException("connectionString"); 

1825             if (spName == null || spName.Length == 0) throw new ArgumentNullException("spName");

1826 

1827             // 如果row有值,存储过程必须初始化. 

1828             if (dataRow != null && dataRow.ItemArray.Length > 0) 

1829             { 

1830                 // 从缓存中加载存储过程参数,如果缓存中不存在则从数据库中检索参数信息并加载到缓存中. () 

1831                 SqlParameter[] commandParameters = SqlHelperParameterCache.GetSpParameterSet(connectionString, spName);

1832 

1833                 // 分配参数值 

1834                 AssignParameterValues(commandParameters, dataRow);

1835 

1836                 return SqlHelper.ExecuteNonQuery(connectionString, CommandType.StoredProcedure, spName, commandParameters); 

1837             } 

1838             else 

1839             { 

1840                 return SqlHelper.ExecuteNonQuery(connectionString, CommandType.StoredProcedure, spName); 

1841             } 

1842         }

1843 

1844         /// <summary> 

1845         /// 执行指定连接数据库连接对象的存储过程,使用DataRow做为参数值,返回受影响的行数. 

1846         /// </summary> 

1847         /// <param name="connection">一个有效的数据库连接对象</param> 

1848         /// <param name="spName">存储过程名称</param> 

1849         /// <param name="dataRow">使用DataRow作为参数值</param> 

1850         /// <returns>返回影响的行数</returns> 

1851         public static int ExecuteNonQueryTypedParams(SqlConnection connection, String spName, DataRow dataRow) 

1852         { 

1853             if (connection == null) throw new ArgumentNullException("connection"); 

1854             if (spName == null || spName.Length == 0) throw new ArgumentNullException("spName");

1855 

1856             // 如果row有值,存储过程必须初始化. 

1857             if (dataRow != null && dataRow.ItemArray.Length > 0) 

1858             { 

1859                 // 从缓存中加载存储过程参数,如果缓存中不存在则从数据库中检索参数信息并加载到缓存中. () 

1860                 SqlParameter[] commandParameters = SqlHelperParameterCache.GetSpParameterSet(connection, spName);

1861 

1862                 // 分配参数值 

1863                 AssignParameterValues(commandParameters, dataRow);

1864 

1865                 return SqlHelper.ExecuteNonQuery(connection, CommandType.StoredProcedure, spName, commandParameters); 

1866             } 

1867             else 

1868             { 

1869                 return SqlHelper.ExecuteNonQuery(connection, CommandType.StoredProcedure, spName); 

1870             } 

1871         }

1872 

1873         /// <summary> 

1874         /// 执行指定连接数据库事物的存储过程,使用DataRow做为参数值,返回受影响的行数. 

1875         /// </summary> 

1876         /// <param name="transaction">一个有效的连接事务 object</param> 

1877         /// <param name="spName">存储过程名称</param> 

1878         /// <param name="dataRow">使用DataRow作为参数值</param> 

1879         /// <returns>返回影响的行数</returns> 

1880         public static int ExecuteNonQueryTypedParams(SqlTransaction transaction, String spName, DataRow dataRow) 

1881         { 

1882             if (transaction == null) throw new ArgumentNullException("transaction"); 

1883             if (transaction != null && transaction.Connection == null) throw new ArgumentException("The transaction was rollbacked or commited, please provide an open transaction.", "transaction"); 

1884             if (spName == null || spName.Length == 0) throw new ArgumentNullException("spName");

1885 

1886             // Sf the row has values, the store procedure parameters must be initialized 

1887             if (dataRow != null && dataRow.ItemArray.Length > 0) 

1888             { 

1889                 // 从缓存中加载存储过程参数,如果缓存中不存在则从数据库中检索参数信息并加载到缓存中. () 

1890                 SqlParameter[] commandParameters = SqlHelperParameterCache.GetSpParameterSet(transaction.Connection, spName);

1891 

1892                 // 分配参数值 

1893                 AssignParameterValues(commandParameters, dataRow);

1894 

1895                 return SqlHelper.ExecuteNonQuery(transaction, CommandType.StoredProcedure, spName, commandParameters); 

1896             } 

1897             else 

1898             { 

1899                 return SqlHelper.ExecuteNonQuery(transaction, CommandType.StoredProcedure, spName); 

1900             } 

1901         } 

1902         #endregion

1903 

1904         #region ExecuteDatasetTypedParams 类型化参数(DataRow) 

1905         /// <summary> 

1906         /// 执行指定连接数据库连接字符串的存储过程,使用DataRow做为参数值,返回DataSet. 

1907         /// </summary> 

1908         /// <param name="connectionString">一个有效的数据库连接字符串</param> 

1909         /// <param name="spName">存储过程名称</param> 

1910         /// <param name="dataRow">使用DataRow作为参数值</param> 

1911         /// <returns>返回一个包含结果集的DataSet.</returns> 

1912         public static DataSet ExecuteDatasetTypedParams(string connectionString, String spName, DataRow dataRow) 

1913         { 

1914             if (connectionString == null || connectionString.Length == 0) throw new ArgumentNullException("connectionString"); 

1915             if (spName == null || spName.Length == 0) throw new ArgumentNullException("spName");

1916 

1917             //如果row有值,存储过程必须初始化. 

1918             if (dataRow != null && dataRow.ItemArray.Length > 0) 

1919             { 

1920                 // 从缓存中加载存储过程参数,如果缓存中不存在则从数据库中检索参数信息并加载到缓存中. () 

1921                 SqlParameter[] commandParameters = SqlHelperParameterCache.GetSpParameterSet(connectionString, spName);

1922 

1923                 // 分配参数值 

1924                 AssignParameterValues(commandParameters, dataRow);

1925 

1926                 return SqlHelper.ExecuteDataset(connectionString, CommandType.StoredProcedure, spName, commandParameters); 

1927             } 

1928             else 

1929             { 

1930                 return SqlHelper.ExecuteDataset(connectionString, CommandType.StoredProcedure, spName); 

1931             } 

1932         }

1933 

1934         /// <summary> 

1935         /// 执行指定连接数据库连接对象的存储过程,使用DataRow做为参数值,返回DataSet. 

1936         /// </summary> 

1937         /// <param name="connection">一个有效的数据库连接对象</param> 

1938         /// <param name="spName">存储过程名称</param> 

1939         /// <param name="dataRow">使用DataRow作为参数值</param> 

1940         /// <returns>返回一个包含结果集的DataSet.</returns> 

1941         /// 

1942         public static DataSet ExecuteDatasetTypedParams(SqlConnection connection, String spName, DataRow dataRow) 

1943         { 

1944             if (connection == null) throw new ArgumentNullException("connection"); 

1945             if (spName == null || spName.Length == 0) throw new ArgumentNullException("spName");

1946 

1947             // 如果row有值,存储过程必须初始化. 

1948             if (dataRow != null && dataRow.ItemArray.Length > 0) 

1949             { 

1950                 // 从缓存中加载存储过程参数,如果缓存中不存在则从数据库中检索参数信息并加载到缓存中. () 

1951                 SqlParameter[] commandParameters = SqlHelperParameterCache.GetSpParameterSet(connection, spName);

1952 

1953                 // 分配参数值 

1954                 AssignParameterValues(commandParameters, dataRow);

1955 

1956                 return SqlHelper.ExecuteDataset(connection, CommandType.StoredProcedure, spName, commandParameters); 

1957             } 

1958             else 

1959             { 

1960                 return SqlHelper.ExecuteDataset(connection, CommandType.StoredProcedure, spName); 

1961             } 

1962         }

1963 

1964         /// <summary> 

1965         /// 执行指定连接数据库事务的存储过程,使用DataRow做为参数值,返回DataSet. 

1966         /// </summary> 

1967         /// <param name="transaction">一个有效的连接事务 object</param> 

1968         /// <param name="spName">存储过程名称</param> 

1969         /// <param name="dataRow">使用DataRow作为参数值</param> 

1970         /// <returns>返回一个包含结果集的DataSet.</returns> 

1971         public static DataSet ExecuteDatasetTypedParams(SqlTransaction transaction, String spName, DataRow dataRow) 

1972         { 

1973             if (transaction == null) throw new ArgumentNullException("transaction"); 

1974             if (transaction != null && transaction.Connection == null) throw new ArgumentException("The transaction was rollbacked or commited, please provide an open transaction.", "transaction"); 

1975             if (spName == null || spName.Length == 0) throw new ArgumentNullException("spName");

1976 

1977             // 如果row有值,存储过程必须初始化. 

1978             if (dataRow != null && dataRow.ItemArray.Length > 0) 

1979             { 

1980                 // 从缓存中加载存储过程参数,如果缓存中不存在则从数据库中检索参数信息并加载到缓存中. () 

1981                 SqlParameter[] commandParameters = SqlHelperParameterCache.GetSpParameterSet(transaction.Connection, spName);

1982 

1983                 // 分配参数值 

1984                 AssignParameterValues(commandParameters, dataRow);

1985 

1986                 return SqlHelper.ExecuteDataset(transaction, CommandType.StoredProcedure, spName, commandParameters); 

1987             } 

1988             else 

1989             { 

1990                 return SqlHelper.ExecuteDataset(transaction, CommandType.StoredProcedure, spName); 

1991             } 

1992         }

1993 

1994         #endregion

1995 

1996         #region ExecuteReaderTypedParams 类型化参数(DataRow) 

1997         /// <summary> 

1998         /// 执行指定连接数据库连接字符串的存储过程,使用DataRow做为参数值,返回DataReader. 

1999         /// </summary> 

2000         /// <param name="connectionString">一个有效的数据库连接字符串</param> 

2001         /// <param name="spName">存储过程名称</param> 

2002         /// <param name="dataRow">使用DataRow作为参数值</param> 

2003         /// <returns>返回包含结果集的SqlDataReader</returns> 

2004         public static SqlDataReader ExecuteReaderTypedParams(String connectionString, String spName, DataRow dataRow) 

2005         { 

2006             if (connectionString == null || connectionString.Length == 0) throw new ArgumentNullException("connectionString"); 

2007             if (spName == null || spName.Length == 0) throw new ArgumentNullException("spName");

2008 

2009             // 如果row有值,存储过程必须初始化. 

2010             if (dataRow != null && dataRow.ItemArray.Length > 0) 

2011             { 

2012                 // 从缓存中加载存储过程参数,如果缓存中不存在则从数据库中检索参数信息并加载到缓存中. () 

2013                 SqlParameter[] commandParameters = SqlHelperParameterCache.GetSpParameterSet(connectionString, spName);

2014 

2015                 // 分配参数值 

2016                 AssignParameterValues(commandParameters, dataRow);

2017 

2018                 return SqlHelper.ExecuteReader(connectionString, CommandType.StoredProcedure, spName, commandParameters); 

2019             } 

2020             else 

2021             { 

2022                 return SqlHelper.ExecuteReader(connectionString, CommandType.StoredProcedure, spName); 

2023             } 

2024         }

2025 

2026 

2027         /// <summary> 

2028         /// 执行指定连接数据库连接对象的存储过程,使用DataRow做为参数值,返回DataReader. 

2029         /// </summary> 

2030         /// <param name="connection">一个有效的数据库连接对象</param> 

2031         /// <param name="spName">存储过程名称</param> 

2032         /// <param name="dataRow">使用DataRow作为参数值</param> 

2033         /// <returns>返回包含结果集的SqlDataReader</returns> 

2034         public static SqlDataReader ExecuteReaderTypedParams(SqlConnection connection, String spName, DataRow dataRow) 

2035         { 

2036             if (connection == null) throw new ArgumentNullException("connection"); 

2037             if (spName == null || spName.Length == 0) throw new ArgumentNullException("spName");

2038 

2039             // 如果row有值,存储过程必须初始化. 

2040             if (dataRow != null && dataRow.ItemArray.Length > 0) 

2041             { 

2042                 // 从缓存中加载存储过程参数,如果缓存中不存在则从数据库中检索参数信息并加载到缓存中. () 

2043                 SqlParameter[] commandParameters = SqlHelperParameterCache.GetSpParameterSet(connection, spName);

2044 

2045                 // 分配参数值 

2046                 AssignParameterValues(commandParameters, dataRow);

2047 

2048                 return SqlHelper.ExecuteReader(connection, CommandType.StoredProcedure, spName, commandParameters); 

2049             } 

2050             else 

2051             { 

2052                 return SqlHelper.ExecuteReader(connection, CommandType.StoredProcedure, spName); 

2053             } 

2054         }

2055 

2056         /// <summary> 

2057         /// 执行指定连接数据库事物的存储过程,使用DataRow做为参数值,返回DataReader. 

2058         /// </summary> 

2059         /// <param name="transaction">一个有效的连接事务 object</param> 

2060         /// <param name="spName">存储过程名称</param> 

2061         /// <param name="dataRow">使用DataRow作为参数值</param> 

2062         /// <returns>返回包含结果集的SqlDataReader</returns> 

2063         public static SqlDataReader ExecuteReaderTypedParams(SqlTransaction transaction, String spName, DataRow dataRow) 

2064         { 

2065             if (transaction == null) throw new ArgumentNullException("transaction"); 

2066             if (transaction != null && transaction.Connection == null) throw new ArgumentException("The transaction was rollbacked or commited, please provide an open transaction.", "transaction"); 

2067             if (spName == null || spName.Length == 0) throw new ArgumentNullException("spName");

2068 

2069             // 如果row有值,存储过程必须初始化. 

2070             if (dataRow != null && dataRow.ItemArray.Length > 0) 

2071             { 

2072                 // 从缓存中加载存储过程参数,如果缓存中不存在则从数据库中检索参数信息并加载到缓存中. () 

2073                 SqlParameter[] commandParameters = SqlHelperParameterCache.GetSpParameterSet(transaction.Connection, spName);

2074 

2075                 // 分配参数值 

2076                 AssignParameterValues(commandParameters, dataRow);

2077 

2078                 return SqlHelper.ExecuteReader(transaction, CommandType.StoredProcedure, spName, commandParameters); 

2079             } 

2080             else 

2081             { 

2082                 return SqlHelper.ExecuteReader(transaction, CommandType.StoredProcedure, spName); 

2083             } 

2084         } 

2085         #endregion

2086 

2087         #region ExecuteScalarTypedParams 类型化参数(DataRow) 

2088         /// <summary> 

2089         /// 执行指定连接数据库连接字符串的存储过程,使用DataRow做为参数值,返回结果集中的第一行第一列. 

2090         /// </summary> 

2091         /// <param name="connectionString">一个有效的数据库连接字符串</param> 

2092         /// <param name="spName">存储过程名称</param> 

2093         /// <param name="dataRow">使用DataRow作为参数值</param> 

2094         /// <returns>返回结果集中的第一行第一列</returns> 

2095         public static object ExecuteScalarTypedParams(String connectionString, String spName, DataRow dataRow) 

2096         { 

2097             if (connectionString == null || connectionString.Length == 0) throw new ArgumentNullException("connectionString"); 

2098             if (spName == null || spName.Length == 0) throw new ArgumentNullException("spName");

2099 

2100             // 如果row有值,存储过程必须初始化. 

2101             if (dataRow != null && dataRow.ItemArray.Length > 0) 

2102             { 

2103                 // 从缓存中加载存储过程参数,如果缓存中不存在则从数据库中检索参数信息并加载到缓存中. () 

2104                 SqlParameter[] commandParameters = SqlHelperParameterCache.GetSpParameterSet(connectionString, spName);

2105 

2106                 // 分配参数值 

2107                 AssignParameterValues(commandParameters, dataRow);

2108 

2109                 return SqlHelper.ExecuteScalar(connectionString, CommandType.StoredProcedure, spName, commandParameters); 

2110             } 

2111             else 

2112             { 

2113                 return SqlHelper.ExecuteScalar(connectionString, CommandType.StoredProcedure, spName); 

2114             } 

2115         }

2116 

2117         /// <summary> 

2118         /// 执行指定连接数据库连接对象的存储过程,使用DataRow做为参数值,返回结果集中的第一行第一列. 

2119         /// </summary> 

2120         /// <param name="connection">一个有效的数据库连接对象</param> 

2121         /// <param name="spName">存储过程名称</param> 

2122         /// <param name="dataRow">使用DataRow作为参数值</param> 

2123         /// <returns>返回结果集中的第一行第一列</returns> 

2124         public static object ExecuteScalarTypedParams(SqlConnection connection, String spName, DataRow dataRow) 

2125         { 

2126             if (connection == null) throw new ArgumentNullException("connection"); 

2127             if (spName == null || spName.Length == 0) throw new ArgumentNullException("spName");

2128 

2129             // 如果row有值,存储过程必须初始化. 

2130             if (dataRow != null && dataRow.ItemArray.Length > 0) 

2131             { 

2132                 // 从缓存中加载存储过程参数,如果缓存中不存在则从数据库中检索参数信息并加载到缓存中. () 

2133                 SqlParameter[] commandParameters = SqlHelperParameterCache.GetSpParameterSet(connection, spName);

2134 

2135                 // 分配参数值 

2136                 AssignParameterValues(commandParameters, dataRow);

2137 

2138                 return SqlHelper.ExecuteScalar(connection, CommandType.StoredProcedure, spName, commandParameters); 

2139             } 

2140             else 

2141             { 

2142                 return SqlHelper.ExecuteScalar(connection, CommandType.StoredProcedure, spName); 

2143             } 

2144         }

2145 

2146         /// <summary> 

2147         /// 执行指定连接数据库事务的存储过程,使用DataRow做为参数值,返回结果集中的第一行第一列. 

2148         /// </summary> 

2149         /// <param name="transaction">一个有效的连接事务 object</param> 

2150         /// <param name="spName">存储过程名称</param> 

2151         /// <param name="dataRow">使用DataRow作为参数值</param> 

2152         /// <returns>返回结果集中的第一行第一列</returns> 

2153         public static object ExecuteScalarTypedParams(SqlTransaction transaction, String spName, DataRow dataRow) 

2154         { 

2155             if (transaction == null) throw new ArgumentNullException("transaction"); 

2156             if (transaction != null && transaction.Connection == null) throw new ArgumentException("The transaction was rollbacked or commited, please provide an open transaction.", "transaction"); 

2157             if (spName == null || spName.Length == 0) throw new ArgumentNullException("spName");

2158 

2159             // 如果row有值,存储过程必须初始化. 

2160             if (dataRow != null && dataRow.ItemArray.Length > 0) 

2161             { 

2162                 // 从缓存中加载存储过程参数,如果缓存中不存在则从数据库中检索参数信息并加载到缓存中. () 

2163                 SqlParameter[] commandParameters = SqlHelperParameterCache.GetSpParameterSet(transaction.Connection, spName);

2164 

2165                 // 分配参数值 

2166                 AssignParameterValues(commandParameters, dataRow);

2167 

2168                 return SqlHelper.ExecuteScalar(transaction, CommandType.StoredProcedure, spName, commandParameters); 

2169             } 

2170             else 

2171             { 

2172                 return SqlHelper.ExecuteScalar(transaction, CommandType.StoredProcedure, spName); 

2173             } 

2174         } 

2175         #endregion

2176 

2177         #region ExecuteXmlReaderTypedParams 类型化参数(DataRow) 

2178         /// <summary> 

2179         /// 执行指定连接数据库连接对象的存储过程,使用DataRow做为参数值,返回XmlReader类型的结果集. 

2180         /// </summary> 

2181         /// <param name="connection">一个有效的数据库连接对象</param> 

2182         /// <param name="spName">存储过程名称</param> 

2183         /// <param name="dataRow">使用DataRow作为参数值</param> 

2184         /// <returns>返回XmlReader结果集对象.</returns> 

2185         public static XmlReader ExecuteXmlReaderTypedParams(SqlConnection connection, String spName, DataRow dataRow) 

2186         { 

2187             if (connection == null) throw new ArgumentNullException("connection"); 

2188             if (spName == null || spName.Length == 0) throw new ArgumentNullException("spName");

2189 

2190             // 如果row有值,存储过程必须初始化. 

2191             if (dataRow != null && dataRow.ItemArray.Length > 0) 

2192             { 

2193                 // 从缓存中加载存储过程参数,如果缓存中不存在则从数据库中检索参数信息并加载到缓存中. () 

2194                 SqlParameter[] commandParameters = SqlHelperParameterCache.GetSpParameterSet(connection, spName);

2195 

2196                 // 分配参数值 

2197                 AssignParameterValues(commandParameters, dataRow);

2198 

2199                 return SqlHelper.ExecuteXmlReader(connection, CommandType.StoredProcedure, spName, commandParameters); 

2200             } 

2201             else 

2202             { 

2203                 return SqlHelper.ExecuteXmlReader(connection, CommandType.StoredProcedure, spName); 

2204             } 

2205         }

2206 

2207         /// <summary> 

2208         /// 执行指定连接数据库事务的存储过程,使用DataRow做为参数值,返回XmlReader类型的结果集. 

2209         /// </summary> 

2210         /// <param name="transaction">一个有效的连接事务 object</param> 

2211         /// <param name="spName">存储过程名称</param> 

2212         /// <param name="dataRow">使用DataRow作为参数值</param> 

2213         /// <returns>返回XmlReader结果集对象.</returns> 

2214         public static XmlReader ExecuteXmlReaderTypedParams(SqlTransaction transaction, String spName, DataRow dataRow) 

2215         { 

2216             if (transaction == null) throw new ArgumentNullException("transaction"); 

2217             if (transaction != null && transaction.Connection == null) throw new ArgumentException("The transaction was rollbacked or commited, please provide an open transaction.", "transaction"); 

2218             if (spName == null || spName.Length == 0) throw new ArgumentNullException("spName");

2219 

2220             // 如果row有值,存储过程必须初始化. 

2221             if (dataRow != null && dataRow.ItemArray.Length > 0) 

2222             { 

2223                 // 从缓存中加载存储过程参数,如果缓存中不存在则从数据库中检索参数信息并加载到缓存中. () 

2224                 SqlParameter[] commandParameters = SqlHelperParameterCache.GetSpParameterSet(transaction.Connection, spName);

2225 

2226                 // 分配参数值 

2227                 AssignParameterValues(commandParameters, dataRow);

2228 

2229                 return SqlHelper.ExecuteXmlReader(transaction, CommandType.StoredProcedure, spName, commandParameters); 

2230             } 

2231             else 

2232             { 

2233                 return SqlHelper.ExecuteXmlReader(transaction, CommandType.StoredProcedure, spName); 

2234             } 

2235         } 

2236         #endregion

2237 

2238     }

2239 

2240     /// <summary> 

2241     /// SqlHelperParameterCache提供缓存存储过程参数,并能够在运行时从存储过程中探索参数. 

2242     /// </summary> 

2243     public sealed class SqlHelperParameterCache 

2244     { 

2245         #region 私有方法,字段,构造函数 

2246         // 私有构造函数,妨止类被实例化. 

2247         private SqlHelperParameterCache() { }

2248 

2249         // 这个方法要注意 

2250         private static Hashtable paramCache = Hashtable.Synchronized(new Hashtable());

2251 

2252         /// <summary> 

2253         /// 探索运行时的存储过程,返回SqlParameter参数数组. 

2254         /// 初始化参数值为 DBNull.Value. 

2255         /// </summary> 

2256         /// <param name="connection">一个有效的数据库连接</param> 

2257         /// <param name="spName">存储过程名称</param> 

2258         /// <param name="includeReturnValueParameter">是否包含返回值参数</param> 

2259         /// <returns>返回SqlParameter参数数组</returns> 

2260         private static SqlParameter[] DiscoverSpParameterSet(SqlConnection connection, string spName, bool includeReturnValueParameter) 

2261         { 

2262             if (connection == null) throw new ArgumentNullException("connection"); 

2263             if (spName == null || spName.Length == 0) throw new ArgumentNullException("spName");

2264 

2265             SqlCommand cmd = new SqlCommand(spName, connection); 

2266             cmd.CommandType = CommandType.StoredProcedure;

2267 

2268             connection.Open(); 

2269             // 检索cmd指定的存储过程的参数信息,并填充到cmd的Parameters参数集中. 

2270             SqlCommandBuilder.DeriveParameters(cmd); 

2271             connection.Close(); 

2272             // 如果不包含返回值参数,将参数集中的每一个参数删除. 

2273             if (!includeReturnValueParameter) 

2274             { 

2275                 cmd.Parameters.RemoveAt(0); 

2276             }

2277 

2278             // 创建参数数组 

2279             SqlParameter[] discoveredParameters = new SqlParameter[cmd.Parameters.Count]; 

2280             // 将cmd的Parameters参数集复制到discoveredParameters数组. 

2281             cmd.Parameters.CopyTo(discoveredParameters, 0);

2282 

2283             // 初始化参数值为 DBNull.Value. 

2284             foreach (SqlParameter discoveredParameter in discoveredParameters) 

2285             { 

2286                 discoveredParameter.Value = DBNull.Value; 

2287             } 

2288             return discoveredParameters; 

2289         }

2290 

2291         /// <summary> 

2292         /// SqlParameter参数数组的深层拷贝. 

2293         /// </summary> 

2294         /// <param name="originalParameters">原始参数数组</param> 

2295         /// <returns>返回一个同样的参数数组</returns> 

2296         private static SqlParameter[] CloneParameters(SqlParameter[] originalParameters) 

2297         { 

2298             SqlParameter[] clonedParameters = new SqlParameter[originalParameters.Length];

2299 

2300             for (int i = 0, j = originalParameters.Length; i < j; i++) 

2301             { 

2302                 clonedParameters[i] = (SqlParameter)((ICloneable)originalParameters[i]).Clone(); 

2303             }

2304 

2305             return clonedParameters; 

2306         }

2307 

2308         #endregion 私有方法,字段,构造函数结束

2309 

2310         #region 缓存方法

2311 

2312         /// <summary> 

2313         /// 追加参数数组到缓存. 

2314         /// </summary> 

2315         /// <param name="connectionString">一个有效的数据库连接字符串</param> 

2316         /// <param name="commandText">存储过程名或SQL语句</param> 

2317         /// <param name="commandParameters">要缓存的参数数组</param> 

2318         public static void CacheParameterSet(string connectionString, string commandText, params SqlParameter[] commandParameters) 

2319         { 

2320             if (connectionString == null || connectionString.Length == 0) throw new ArgumentNullException("connectionString"); 

2321             if (commandText == null || commandText.Length == 0) throw new ArgumentNullException("commandText");

2322 

2323             string hashKey = connectionString + ":" + commandText;

2324 

2325             paramCache[hashKey] = commandParameters; 

2326         }

2327 

2328         /// <summary> 

2329         /// 从缓存中获取参数数组. 

2330         /// </summary> 

2331         /// <param name="connectionString">一个有效的数据库连接字符</param> 

2332         /// <param name="commandText">存储过程名或SQL语句</param> 

2333         /// <returns>参数数组</returns> 

2334         public static SqlParameter[] GetCachedParameterSet(string connectionString, string commandText) 

2335         { 

2336             if (connectionString == null || connectionString.Length == 0) throw new ArgumentNullException("connectionString"); 

2337             if (commandText == null || commandText.Length == 0) throw new ArgumentNullException("commandText");

2338 

2339             string hashKey = connectionString + ":" + commandText;

2340 

2341             SqlParameter[] cachedParameters = paramCache[hashKey] as SqlParameter[]; 

2342             if (cachedParameters == null) 

2343             { 

2344                 return null; 

2345             } 

2346             else 

2347             { 

2348                 return CloneParameters(cachedParameters); 

2349             } 

2350         }

2351 

2352         #endregion 缓存方法结束

2353 

2354         #region 检索指定的存储过程的参数集

2355 

2356         /// <summary> 

2357         /// 返回指定的存储过程的参数集 

2358         /// </summary> 

2359         /// <remarks> 

2360         /// 这个方法将查询数据库,并将信息存储到缓存. 

2361         /// </remarks> 

2362         /// <param name="connectionString">一个有效的数据库连接字符</param> 

2363         /// <param name="spName">存储过程名</param> 

2364         /// <returns>返回SqlParameter参数数组</returns> 

2365         public static SqlParameter[] GetSpParameterSet(string connectionString, string spName) 

2366         { 

2367             return GetSpParameterSet(connectionString, spName, false); 

2368         }

2369 

2370         /// <summary> 

2371         /// 返回指定的存储过程的参数集 

2372         /// </summary> 

2373         /// <remarks> 

2374         /// 这个方法将查询数据库,并将信息存储到缓存. 

2375         /// </remarks> 

2376         /// <param name="connectionString">一个有效的数据库连接字符.</param> 

2377         /// <param name="spName">存储过程名</param> 

2378         /// <param name="includeReturnValueParameter">是否包含返回值参数</param> 

2379         /// <returns>返回SqlParameter参数数组</returns> 

2380         public static SqlParameter[] GetSpParameterSet(string connectionString, string spName, bool includeReturnValueParameter) 

2381         { 

2382             if (connectionString == null || connectionString.Length == 0) throw new ArgumentNullException("connectionString"); 

2383             if (spName == null || spName.Length == 0) throw new ArgumentNullException("spName");

2384 

2385             using (SqlConnection connection = new SqlConnection(connectionString)) 

2386             { 

2387                 return GetSpParameterSetInternal(connection, spName, includeReturnValueParameter); 

2388             } 

2389         }

2390 

2391         /// <summary> 

2392         /// [内部]返回指定的存储过程的参数集(使用连接对象). 

2393         /// </summary> 

2394         /// <remarks> 

2395         /// 这个方法将查询数据库,并将信息存储到缓存. 

2396         /// </remarks> 

2397         /// <param name="connection">一个有效的数据库连接字符</param> 

2398         /// <param name="spName">存储过程名</param> 

2399         /// <returns>返回SqlParameter参数数组</returns> 

2400         internal static SqlParameter[] GetSpParameterSet(SqlConnection connection, string spName) 

2401         { 

2402             return GetSpParameterSet(connection, spName, false); 

2403         }

2404 

2405         /// <summary> 

2406         /// [内部]返回指定的存储过程的参数集(使用连接对象) 

2407         /// </summary> 

2408         /// <remarks> 

2409         /// 这个方法将查询数据库,并将信息存储到缓存. 

2410         /// </remarks> 

2411         /// <param name="connection">一个有效的数据库连接对象</param> 

2412         /// <param name="spName">存储过程名</param> 

2413         /// <param name="includeReturnValueParameter"> 

2414         /// 是否包含返回值参数 

2415         /// </param> 

2416         /// <returns>返回SqlParameter参数数组</returns> 

2417         internal static SqlParameter[] GetSpParameterSet(SqlConnection connection, string spName, bool includeReturnValueParameter) 

2418         { 

2419             if (connection == null) throw new ArgumentNullException("connection"); 

2420             using (SqlConnection clonedConnection = (SqlConnection)((ICloneable)connection).Clone()) 

2421             { 

2422                 return GetSpParameterSetInternal(clonedConnection, spName, includeReturnValueParameter); 

2423             } 

2424         }

2425 

2426         /// <summary> 

2427         /// [私有]返回指定的存储过程的参数集(使用连接对象) 

2428         /// </summary> 

2429         /// <param name="connection">一个有效的数据库连接对象</param> 

2430         /// <param name="spName">存储过程名</param> 

2431         /// <param name="includeReturnValueParameter">是否包含返回值参数</param> 

2432         /// <returns>返回SqlParameter参数数组</returns> 

2433         private static SqlParameter[] GetSpParameterSetInternal(SqlConnection connection, string spName, bool includeReturnValueParameter) 

2434         { 

2435             if (connection == null) throw new ArgumentNullException("connection"); 

2436             if (spName == null || spName.Length == 0) throw new ArgumentNullException("spName");

2437 

2438             string hashKey = connection.ConnectionString + ":" + spName + (includeReturnValueParameter ? ":include ReturnValue Parameter" : "");

2439 

2440             SqlParameter[] cachedParameters;

2441 

2442             cachedParameters = paramCache[hashKey] as SqlParameter[]; 

2443             if (cachedParameters == null) 

2444             { 

2445                 SqlParameter[] spParameters = DiscoverSpParameterSet(connection, spName, includeReturnValueParameter); 

2446                 paramCache[hashKey] = spParameters; 

2447                 cachedParameters = spParameters; 

2448             }

2449 

2450             return CloneParameters(cachedParameters); 

2451         }

2452 

2453         #endregion 参数集检索结束

2454 

2455     } 

2456 }

2457 

2458 //如果要获取连接数据连接对象或字符串的话,先要修改SQLHelper类中GetConnSting() 方法中的ConfigurationManager.ConnectionStrings["ConStr"].ConnectionString;才能调用。

 

你可能感兴趣的:(help)