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