/Files/yuanyuan/SqlHelper详细中文注释.txt

Code
1
using System;
2
using System.Data;
3
using System.Xml;
4
using System.Data.SqlClient;
5
using System.Collections;
6
7
/**//// <summary>
8
/// SqlServer 数据访问帮助类
9
/// </summary>
10
public sealed class SqlHelper
11
{
12
私有构造函数和方法#region 私有构造函数和方法
13
14
private SqlHelper()
{ }
15
16
/**//// <summary>
17
/// 将SqlParameter参数数组(参数值)分配给SqlCommand命令.
18
/// 这个方法将给任何一个参数分配DBNull.Value;
19
/// 该操作将阻止默认值的使用.
20
/// </summary>
21
/// <param name="command">命令名</param>
22
/// <param name="commandParameters">SqlParameters数组</param>
23
private static void AttachParameters(SqlCommand command, SqlParameter[] commandParameters)
24
{
25
if (command == null) throw new ArgumentNullException("command");
26
if (commandParameters != null)
27
{
28
foreach (SqlParameter p in commandParameters)
29
{
30
if (p != null)
31
{
32
// 检查未分配值的输出参数,将其分配以DBNull.Value.
33
if ((p.Direction == ParameterDirection.InputOutput || p.Direction == ParameterDirection.Input) &&
34
(p.Value == null))
35
{
36
p.Value = DBNull.Value;
37
38
}
39
command.Parameters.Add(p);
40
}
41
}
42
}
43
}
44
45
/**//// <summary>
46
/// 将DataRow类型的列值分配到SqlParameter参数数组.
47
/// </summary>
48
/// <param name="commandParameters">要分配值的SqlParameter参数数组</param>
49
/// <param name="dataRow">将要分配给存储过程参数的DataRow</param>
50
private static void AssignParameterValues(SqlParameter[] commandParameters, DataRow dataRow)
51
{
52
if ((commandParameters == null) || (dataRow == null))
53
{
54
return;
55
}
56
int i = 0;
57
// 设置参数值
58
foreach (SqlParameter commandParameter in commandParameters)
59
{
60
// 创建参数名称,如果不存在,只抛出一个异常.
61
if (commandParameter.ParameterName == null ||
62
commandParameter.ParameterName.Length <= 1)
63
throw new Exception(
64
string.Format("请提供参数{0}一个有效的名称{1}.", i, commandParameter.ParameterName));
65
66
// 从dataRow的表中获取为参数数组中数组名称的列的索引.
67
68
// 如果存在和参数名称相同的列,则将列值赋给当前名称的参数.
69
70
if (dataRow.Table.Columns.IndexOf(commandParameter.ParameterName.Substring(1)) != -1)
71
commandParameter.Value = dataRow[commandParameter.ParameterName.Substring(1)];
72
i++;
73
}
74
}
75
76
/**//// <summary>
77
/// 将一个对象数组分配给SqlParameter参数数组.
78
/// </summary>
79
/// <param name="commandParameters">要分配值的SqlParameter参数数组</param>
80
/// <param name="parameterValues">将要分配给存储过程参数的对象数组</param>
81
private static void AssignParameterValues(SqlParameter[] commandParameters, object[] parameterValues)
82
{
83
if ((commandParameters == null) || (parameterValues == null))
84
{
85
return;
86
}
87
// 确保对象数组个数与参数个数匹配,如果不匹配,抛出一个异常.
88
if (commandParameters.Length != parameterValues.Length)
89
{
90
throw new ArgumentException("参数值个数与参数不匹配.");
91
}
92
93
// 给参数赋值
94
for (int i = 0, j = commandParameters.Length; i < j; i++)
95
{
96
// If the current array value derives from IDbDataParameter, then assign its Value property
97
if (parameterValues[i] is IDbDataParameter)
98
{
99
IDbDataParameter paramInstance = (IDbDataParameter)parameterValues[i];
100
if (paramInstance.Value == null)
101
{
102
commandParameters[i].Value = DBNull.Value;
103
}
104
else
105
{
106
commandParameters[i].Value = paramInstance.Value;
107
}
108
}
109
else if (parameterValues[i] == null)
110
{
111
commandParameters[i].Value = DBNull.Value;
112
113
}
114
else
115
{
116
commandParameters[i].Value = parameterValues[i];
117
}
118
}
119
}
120
121
/**//// <summary>
122
/// 预处理用户提供的命令,数据库连接/事务/命令类型/参数
123
/// </summary>
124
/// <param name="command">要处理的SqlCommand</param>
125
/// <param name="connection">数据库连接</param>
126
/// <param name="transaction">一个有效的事务或者是null值</param>
127
/// <param name="commandType">命令类型 (存储过程,命令文本, 其它.)</param>
128
/// <param name="commandText">存储过程名或都T-SQL命令文本</param>
129
/// <param name="commandParameters">和命令相关联的SqlParameter参数数组,如果没有参数为'null'</param>
130
/// <param name="mustCloseConnection"><c>true</c> 如果连接是打开的,则为true,其它情况下为false.</param>
131
private static void PrepareCommand(SqlCommand command, SqlConnection connection, SqlTransaction transaction, CommandType commandType, string commandText, SqlParameter[] commandParameters, out bool mustCloseConnection)
132
{
133
if (command == null) throw new ArgumentNullException("command");
134
if (commandText == null || commandText.Length == 0) throw new ArgumentNullException("commandText");
135
// If the provided connection is not open, we will open it
136
if (connection.State != ConnectionState.Open)
137
{
138
mustCloseConnection = true;
139
connection.Open();
140
}
141
else
142
{
143
mustCloseConnection = false;
144
}
145
146
// 给命令分配一个数据库连接.
147
command.Connection = connection;
148
// 设置命令文本(存储过程名或SQL语句)
149
command.CommandText = commandText;
150
// 分配事务
151
if (transaction != null)
152
{
153
if (transaction.Connection == null) throw new ArgumentException("The transaction was rollbacked or commited, please provide an open transaction.", "transaction");
154
command.Transaction = transaction;
155
}
156
157
158
159
// 设置命令类型.
160
161
command.CommandType = commandType;
162
163
164
165
// 分配命令参数
166
167
if (commandParameters != null)
168
{
169
170
AttachParameters(command, commandParameters);
171
172
}
173
174
return;
175
176
}
177
178
#endregion 私有构造函数和方法结束
179
180
ExecuteNonQuery命令#region ExecuteNonQuery命令
181
182
/**//// <summary>
183
/// 执行指定连接字符串,类型的SqlCommand.
184
/// </summary>
185
/// <remarks>
186
/// 示例:
187
/// int result = ExecuteNonQuery(connString, CommandType.StoredProcedure, "PublishOrders");
188
/// </remarks>
189
/// <param name="connectionString">一个有效的数据库连接字符串</param>
190
/// <param name="commandType">命令类型 (存储过程,命令文本, 其它.)</param>
191
/// <param name="commandText">存储过程名称或SQL语句</param>
192
/// <returns>返回命令影响的行数</returns>
193
public static int ExecuteNonQuery(string connectionString, CommandType commandType, string commandText)
194
{
195
return ExecuteNonQuery(connectionString, commandType, commandText, (SqlParameter[])null);
196
}
197
198
/**//// <summary>
199
/// 执行指定连接字符串,类型的SqlCommand.如果没有提供参数,不返回结果.
200
/// </summary>
201
/// <remarks>
202
/// 示例:
203
/// int result = ExecuteNonQuery(connString, CommandType.StoredProcedure, "PublishOrders", new SqlParameter("@prodid", 24));
204
/// </remarks>
205
/// <param name="connectionString">一个有效的数据库连接字符串</param>
206
/// <param name="commandType">命令类型 (存储过程,命令文本, 其它.)</param>
207
/// <param name="commandText">存储过程名称或SQL语句</param>
208
/// <param name="commandParameters">SqlParameter参数数组</param>
209
/// <returns>返回命令影响的行数</returns>
210
public static int ExecuteNonQuery(string connectionString, CommandType commandType, string commandText, params SqlParameter[] commandParameters)
211
{
212
if (connectionString == null || connectionString.Length == 0) throw new ArgumentNullException("connectionString");
213
using (SqlConnection connection = new SqlConnection(connectionString))
214
{
215
connection.Open();
216
return ExecuteNonQuery(connection, commandType, commandText, commandParameters);
217
}
218
}
219
220
/**//// <summary>
221
/// 执行指定连接字符串的存储过程,将对象数组的值赋给存储过程参数,
222
/// 此方法需要在参数缓存方法中探索参数并生成参数.
223
/// </summary>
224
/// <remarks>
225
/// 这个方法没有提供访问输出参数和返回值.
226
/// 示例:
227
/// int result = ExecuteNonQuery(connString, "PublishOrders", 24, 36);
228
/// </remarks>
229
/// <param name="connectionString">一个有效的数据库连接字符串/param>
230
/// <param name="spName">存储过程名称</param>
231
/// <param name="parameterValues">分配到存储过程输入参数的对象数组</param>
232
/// <returns>返回受影响的行数</returns>
233
public static int ExecuteNonQuery(string connectionString, string spName, params object[] parameterValues)
234
{
235
if (connectionString == null || connectionString.Length == 0) throw new ArgumentNullException("connectionString");
236
if (spName == null || spName.Length == 0) throw new ArgumentNullException("spName");
237
// 如果存在参数值
238
if ((parameterValues != null) && (parameterValues.Length > 0))
239
{
240
// 从探索存储过程参数(加载到缓存)并分配给存储过程参数数组.
241
SqlParameter[] commandParameters = SqlHelperParameterCache.GetSpParameterSet(connectionString, spName);
242
// 给存储过程参数赋值
243
AssignParameterValues(commandParameters, parameterValues);
244
return ExecuteNonQuery(connectionString, CommandType.StoredProcedure, spName, commandParameters);
245
}
246
else
247
{
248
// 没有参数情况下
249
return ExecuteNonQuery(connectionString, CommandType.StoredProcedure, spName);
250
}
251
}
252
253
/**//// <summary>
254
/// 执行指定数据库连接对象的命令
255
/// </summary>
256
/// <remarks>
257
/// 示例:
258
/// int result = ExecuteNonQuery(conn, CommandType.StoredProcedure, "PublishOrders");
259
/// </remarks>
260
/// <param name="connection">一个有效的数据库连接对象</param>
261
/// <param name="commandType">命令类型(存储过程,命令文本或其它.)</param>
262
/// <param name="commandText">存储过程名称或T-SQL语句</param>
263
/// <returns>返回影响的行数</returns>
264
public static int ExecuteNonQuery(SqlConnection connection, CommandType commandType, string commandText)
265
{
266
return ExecuteNonQuery(connection, commandType, commandText, (SqlParameter[])null);
267
}
268
269
/**//// <summary>
270
/// 执行指定数据库连接对象的命令
271
/// </summary>
272
/// <remarks>
273
/// 示例:
274
/// int result = ExecuteNonQuery(conn, CommandType.StoredProcedure, "PublishOrders", new SqlParameter("@prodid", 24));
275
/// </remarks>
276
/// <param name="connection">一个有效的数据库连接对象</param>
277
/// <param name="commandType">命令类型(存储过程,命令文本或其它.)</param>
278
/// <param name="commandText">T存储过程名称或T-SQL语句</param>
279
/// <param name="commandParameters">SqlParamter参数数组</param>
280
/// <returns>返回影响的行数</returns>
281
public static int ExecuteNonQuery(SqlConnection connection, CommandType commandType, string commandText, params SqlParameter[] commandParameters)
282
{
283
if (connection == null) throw new ArgumentNullException("connection");
284
// 创建SqlCommand命令,并进行预处理
285
SqlCommand cmd = new SqlCommand();
286
bool mustCloseConnection = false;
287
PrepareCommand(cmd, connection, (SqlTransaction)null, commandType, commandText, commandParameters, out mustCloseConnection);
288
// Finally, execute the command
289
int retval = cmd.ExecuteNonQuery();
290
291
// 清除参数,以便再次使用.
292
cmd.Parameters.Clear();
293
if (mustCloseConnection)
294
connection.Close();
295
return retval;
296
}
297
298
/**//// <summary>
299
/// 执行指定数据库连接对象的命令,将对象数组的值赋给存储过程参数.
300
/// </summary>
301
/// <remarks>
302
/// 此方法不提供访问存储过程输出参数和返回值
303
/// 示例:
304
/// int result = ExecuteNonQuery(conn, "PublishOrders", 24, 36);
305
/// </remarks>
306
/// <param name="connection">一个有效的数据库连接对象</param>
307
/// <param name="spName">存储过程名</param>
308
/// <param name="parameterValues">分配给存储过程输入参数的对象数组</param>
309
/// <returns>返回影响的行数</returns>
310
public static int ExecuteNonQuery(SqlConnection connection, string spName, params object[] parameterValues)
311
{
312
if (connection == null) throw new ArgumentNullException("connection");
313
if (spName == null || spName.Length == 0) throw new ArgumentNullException("spName");
314
// 如果有参数值
315
if ((parameterValues != null) && (parameterValues.Length > 0))
316
{
317
// 从缓存中加载存储过程参数
318
SqlParameter[] commandParameters = SqlHelperParameterCache.GetSpParameterSet(connection, spName);
319
// 给存储过程分配参数值
320
AssignParameterValues(commandParameters, parameterValues);
321
return ExecuteNonQuery(connection, CommandType.StoredProcedure, spName, commandParameters);
322
}
323
else
324
{
325
return ExecuteNonQuery(connection, CommandType.StoredProcedure, spName);
326
}
327
}
328
329
/**//// <summary>
330
/// 执行带事务的SqlCommand.
331
/// </summary>
332
/// <remarks>
333
/// 示例.:
334
/// int result = ExecuteNonQuery(trans, CommandType.StoredProcedure, "PublishOrders");
335
/// </remarks>
336
/// <param name="transaction">一个有效的数据库连接对象</param>
337
/// <param name="commandType">命令类型(存储过程,命令文本或其它.)</param>
338
/// <param name="commandText">存储过程名称或T-SQL语句</param>
339
/// <returns>返回影响的行数/returns>
340
public static int ExecuteNonQuery(SqlTransaction transaction, CommandType commandType, string commandText)
341
{
342
return ExecuteNonQuery(transaction, commandType, commandText, (SqlParameter[])null);
343
}
344
345
/**//// <summary>
346
/// 执行带事务的SqlCommand(指定参数).
347
/// </summary>
348
/// <remarks>
349
/// 示例:
350
/// int result = ExecuteNonQuery(trans, CommandType.StoredProcedure, "GetOrders", new SqlParameter("@prodid", 24));
351
/// </remarks>
352
/// <param name="transaction">一个有效的数据库连接对象</param>
353
/// <param name="commandType">命令类型(存储过程,命令文本或其它.)</param>
354
/// <param name="commandText">存储过程名称或T-SQL语句</param>
355
/// <param name="commandParameters">SqlParamter参数数组</param>
356
/// <returns>返回影响的行数</returns>
357
public static int ExecuteNonQuery(SqlTransaction transaction, CommandType commandType, string commandText, params SqlParameter[] commandParameters)
358
{
359
if (transaction == null) throw new ArgumentNullException("transaction");
360
if (transaction != null && transaction.Connection == null) throw new ArgumentException("The transaction was rollbacked or commited, please provide an open transaction.", "transaction");
361
// 预处理
362
SqlCommand cmd = new SqlCommand();
363
bool mustCloseConnection = false;
364
PrepareCommand(cmd, transaction.Connection, transaction, commandType, commandText, commandParameters, out mustCloseConnection);
365
// 执行
366
int retval = cmd.ExecuteNonQuery();
367
// 清除参数集,以便再次使用.
368
cmd.Parameters.Clear();
369
return retval;
370
}
371
372
373
/**//// <summary>
374
/// 执行带事务的SqlCommand(指定参数值).
375
/// </summary>
376
/// <remarks>
377
/// 此方法不提供访问存储过程输出参数和返回值
378
/// 示例:
379
/// int result = ExecuteNonQuery(conn, trans, "PublishOrders", 24, 36);
380
/// </remarks>
381
/// <param name="transaction">一个有效的数据库连接对象</param>
382
/// <param name="spName">存储过程名</param>
383
/// <param name="parameterValues">分配给存储过程输入参数的对象数组</param>
384
/// <returns>返回受影响的行数</returns>
385
public static int ExecuteNonQuery(SqlTransaction transaction, string spName, params object[] parameterValues)
386
{
387
if (transaction == null) throw new ArgumentNullException("transaction");
388
if (transaction != null && transaction.Connection == null) throw new ArgumentException("The transaction was rollbacked or commited, please provide an open transaction.", "transaction");
389
if (spName == null || spName.Length == 0) throw new ArgumentNullException("spName");
390
// 如果有参数值
391
if ((parameterValues != null) && (parameterValues.Length > 0))
392
{
393
// 从缓存中加载存储过程参数,如果缓存中不存在则从数据库中检索参数信息并加载到缓存中. ()
394
SqlParameter[] commandParameters = SqlHelperParameterCache.GetSpParameterSet(transaction.Connection, spName);
395
// 给存储过程参数赋值
396
AssignParameterValues(commandParameters, parameterValues);
397
// 调用重载方法
398
return ExecuteNonQuery(transaction, CommandType.StoredProcedure, spName, commandParameters);
399
}
400
else
401
{
402
// 没有参数值
403
return ExecuteNonQuery(transaction, CommandType.StoredProcedure, spName);
404
}
405
}
406
407
#endregion ExecuteNonQuery方法结束
408
409
ExecuteDataset方法#region ExecuteDataset方法
410
411
412
/**//// <summary>
413
/// 执行指定数据库连接字符串的命令,返回DataSet.
414
/// </summary>
415
/// <remarks>
416
/// 示例:
417
/// DataSet ds = ExecuteDataset(connString, CommandType.StoredProcedure, "GetOrders");
418
/// </remarks>
419
/// <param name="connectionString">一个有效的数据库连接字符串</param>
420
/// <param name="commandType">命令类型 (存储过程,命令文本或其它)</param>
421
/// <param name="commandText">存储过程名称或T-SQL语句</param>
422
/// <returns>返回一个包含结果集的DataSet</returns>
423
public static DataSet ExecuteDataset(string connectionString, CommandType commandType, string commandText)
424
{
425
return ExecuteDataset(connectionString, commandType, commandText, (SqlParameter[])null);
426
}
427
428
/**//// <summary>
429
/// 执行指定数据库连接字符串的命令,返回DataSet.
430
/// </summary>
431
/// <remarks>
432
/// 示例:
433
/// DataSet ds = ExecuteDataset(connString, CommandType.StoredProcedure, "GetOrders", new SqlParameter("@prodid", 24));
434
/// </remarks>
435
/// <param name="connectionString">一个有效的数据库连接字符串</param>
436
/// <param name="commandType">命令类型 (存储过程,命令文本或其它)</param>
437
/// <param name="commandText">存储过程名称或T-SQL语句</param>
438
/// <param name="commandParameters">SqlParamters参数数组</param>
439
/// <returns>返回一个包含结果集的DataSet</returns>
440
public static DataSet ExecuteDataset(string connectionString, CommandType commandType, string commandText, params SqlParameter[] commandParameters)
441
{
442
if (connectionString == null || connectionString.Length == 0) throw new ArgumentNullException("connectionString");
443
444
// 创建并打开数据库连接对象,操作完成释放对象.
445
using (SqlConnection connection = new SqlConnection(connectionString))
446
{
447
connection.Open();
448
// 调用指定数据库连接字符串重载方法.
449
return ExecuteDataset(connection, commandType, commandText, commandParameters);
450
}
451
}
452
453
/**//// <summary>
454
/// 执行指定数据库连接字符串的命令,直接提供参数值,返回DataSet.
455
/// </summary>
456
/// <remarks>
457
/// 此方法不提供访问存储过程输出参数和返回值.
458
/// 示例:
459
/// DataSet ds = ExecuteDataset(connString, "GetOrders", 24, 36);
460
/// </remarks>
461
/// <param name="connectionString">一个有效的数据库连接字符串</param>
462
/// <param name="spName">存储过程名</param>
463
/// <param name="parameterValues">分配给存储过程输入参数的对象数组</param>
464
/// <returns>返回一个包含结果集的DataSet</returns>
465
public static DataSet ExecuteDataset(string connectionString, string spName, params object[] parameterValues)
466
{
467
if (connectionString == null || connectionString.Length == 0) throw new ArgumentNullException("connectionString");
468
if (spName == null || spName.Length == 0) throw new ArgumentNullException("spName");
469
if ((parameterValues != null) && (parameterValues.Length > 0))
470
{
471
// 从缓存中检索存储过程参数
472
SqlParameter[] commandParameters = SqlHelperParameterCache.GetSpParameterSet(connectionString, spName);
473
// 给存储过程参数分配值
474
AssignParameterValues(commandParameters, parameterValues);
475
return ExecuteDataset(connectionString, CommandType.StoredProcedure, spName, commandParameters);
476
}
477
else
478
{
479
return ExecuteDataset(connectionString, CommandType.StoredProcedure, spName);
480
}
481
}
482
/**//// <summary>
483
/// 执行指定数据库连接对象的命令,返回DataSet.
484
/// </summary>
485
/// <remarks>
486
/// 示例:
487
/// DataSet ds = ExecuteDataset(conn, CommandType.StoredProcedure, "GetOrders");
488
/// </remarks>
489
/// <param name="connection">一个有效的数据库连接对象</param>
490
/// <param name="commandType">命令类型 (存储过程,命令文本或其它)</param>
491
/// <param name="commandText">存储过程名或T-SQL语句</param>
492
/// <returns>返回一个包含结果集的DataSet</returns>
493
public static DataSet ExecuteDataset(SqlConnection connection, CommandType commandType, string commandText)
494
{
495
return ExecuteDataset(connection, commandType, commandText, (SqlParameter[])null);
496
}
497
498
/**//// <summary>
499
/// 执行指定数据库连接对象的命令,指定存储过程参数,返回DataSet.
500
/// </summary>
501
/// <remarks>
502
/// 示例:
503
/// DataSet ds = ExecuteDataset(conn, CommandType.StoredProcedure, "GetOrders", new SqlParameter("@prodid", 24));
504
/// </remarks>
505
/// <param name="connection">一个有效的数据库连接对象</param>
506
/// <param name="commandType">命令类型 (存储过程,命令文本或其它)</param>
507
/// <param name="commandText">存储过程名或T-SQL语句</param>
508
/// <param name="commandParameters">SqlParamter参数数组</param>
509
/// <returns>返回一个包含结果集的DataSet</returns>
510
public static DataSet ExecuteDataset(SqlConnection connection, CommandType commandType, string commandText, params SqlParameter[] commandParameters)
511
{
512
if (connection == null) throw new ArgumentNullException("connection");
513
514
// 预处理
515
SqlCommand cmd = new SqlCommand();
516
bool mustCloseConnection = false;
517
PrepareCommand(cmd, connection, (SqlTransaction)null, commandType, commandText, commandParameters, out mustCloseConnection);
518
519
// 创建SqlDataAdapter和DataSet.
520
using (SqlDataAdapter da = new SqlDataAdapter(cmd))
521
{
522
DataSet ds = new DataSet();
523
524
// 填充DataSet.
525
da.Fill(ds);
526
527
cmd.Parameters.Clear();
528
529
if (mustCloseConnection)
530
connection.Close();
531
return ds;
532
}
533
}
534
535
/**//// <summary>
536
/// 执行指定数据库连接对象的命令,指定参数值,返回DataSet.
537
/// </summary>
538
/// <remarks>
539
/// 此方法不提供访问存储过程输入参数和返回值.
540
/// 示例.:
541
/// DataSet ds = ExecuteDataset(conn, "GetOrders", 24, 36);
542
/// </remarks>
543
/// <param name="connection">一个有效的数据库连接对象</param>
544
/// <param name="spName">存储过程名</param>
545
/// <param name="parameterValues">分配给存储过程输入参数的对象数组</param>
546
/// <returns>返回一个包含结果集的DataSet</returns>
547
public static DataSet ExecuteDataset(SqlConnection connection, string spName, params object[] parameterValues)
548
{
549
if (connection == null) throw new ArgumentNullException("connection");
550
if (spName == null || spName.Length == 0) throw new ArgumentNullException("spName");
551
if ((parameterValues != null) && (parameterValues.Length > 0))
552
{
553
// 比缓存中加载存储过程参数
554
SqlParameter[] commandParameters = SqlHelperParameterCache.GetSpParameterSet(connection, spName);
555
556
// 给存储过程参数分配值
557
AssignParameterValues(commandParameters, parameterValues);
558
return ExecuteDataset(connection, CommandType.StoredProcedure, spName, commandParameters);
559
}
560
else
561
{
562
return ExecuteDataset(connection, CommandType.StoredProcedure, spName);
563
}
564
}
565
566
/**//// <summary>
567
/// 执行指定事务的命令,返回DataSet.
568
/// </summary>
569
/// <remarks>
570
/// 示例:
571
/// DataSet ds = ExecuteDataset(trans, CommandType.StoredProcedure, "GetOrders");
572
/// </remarks>
573
/// <param name="transaction">事务</param>
574
/// <param name="commandType">命令类型 (存储过程,命令文本或其它)</param>
575
/// <param name="commandText">存储过程名或T-SQL语句</param>
576
/// <returns>返回一个包含结果集的DataSet</returns>
577
public static DataSet ExecuteDataset(SqlTransaction transaction, CommandType commandType, string commandText)
578
{
579
return ExecuteDataset(transaction, commandType, commandText, (SqlParameter[])null);
580
}
581
582
583
/**//// <summary>
584
/// 执行指定事务的命令,指定参数,返回DataSet.
585
/// </summary>
586
/// <remarks>
587
/// 示例:
588
/// DataSet ds = ExecuteDataset(trans, CommandType.StoredProcedure, "GetOrders", new SqlParameter("@prodid", 24));
589
/// </remarks>
590
/// <param name="transaction">事务</param>
591
/// <param name="commandType">命令类型 (存储过程,命令文本或其它)</param>
592
/// <param name="commandText">存储过程名或T-SQL语句</param>
593
/// <param name="commandParameters">SqlParamter参数数组</param>
594
/// <returns>返回一个包含结果集的DataSet</returns>
595
public static DataSet ExecuteDataset(SqlTransaction transaction, CommandType commandType, string commandText, params SqlParameter[] commandParameters)
596
{
597
if (transaction == null) throw new ArgumentNullException("transaction");
598
if (transaction != null && transaction.Connection == null) throw new ArgumentException("The transaction was rollbacked or commited, please provide an open transaction.", "transaction");
599
600
// 预处理
601
SqlCommand cmd = new SqlCommand();
602
bool mustCloseConnection = false;
603
PrepareCommand(cmd, transaction.Connection, transaction, commandType, commandText, commandParameters, out mustCloseConnection);
604
// 创建 DataAdapter & DataSet
605
using (SqlDataAdapter da = new SqlDataAdapter(cmd))
606
{
607
DataSet ds = new DataSet();
608
da.Fill(ds);
609
cmd.Parameters.Clear();
610
return ds;
611
}
612
}
613
614
/**//// <summary>
615
/// 执行指定事务的命令,指定参数值,返回DataSet.
616
/// </summary>
617
/// <remarks>
618
/// 此方法不提供访问存储过程输入参数和返回值.
619
/// 示例.:
620
/// DataSet ds = ExecuteDataset(trans, "GetOrders", 24, 36);
621
/// </remarks>
622
/// <param name="transaction">事务</param>
623
/// <param name="spName">存储过程名</param>
624
/// <param name="parameterValues">分配给存储过程输入参数的对象数组</param>
625
/// <returns>返回一个包含结果集的DataSet</returns>
626
public static DataSet ExecuteDataset(SqlTransaction transaction, string spName, params object[] parameterValues)
627
{
628
if (transaction == null) throw new ArgumentNullException("transaction");
629
if (transaction != null && transaction.Connection == null) throw new ArgumentException("The transaction was rollbacked or commited, please provide an open transaction.", "transaction");
630
if (spName == null || spName.Length == 0) throw new ArgumentNullException("spName");
631
if ((parameterValues != null) && (parameterValues.Length > 0))
632
{
633
// 从缓存中加载存储过程参数
634
SqlParameter[] commandParameters = SqlHelperParameterCache.GetSpParameterSet(transaction.Connection, spName);
635
636
// 给存储过程参数分配值
637
AssignParameterValues(commandParameters, parameterValues);
638
return ExecuteDataset(transaction, CommandType.StoredProcedure, spName, commandParameters);
639
}
640
else
641
{
642
return ExecuteDataset(transaction, CommandType.StoredProcedure, spName);
643
}
644
}
645
646
#endregion ExecuteDataset数据集命令结束
647
648
ExecuteReader 数据阅读器#region ExecuteReader 数据阅读器
649
650
651
/**//// <summary>
652
/// 枚举,标识数据库连接是由SqlHelper提供还是由调用者提供
653
/// </summary>
654
private enum SqlConnectionOwnership
655
{
656
/**//// <summary>由SqlHelper提供连接</summary>
657
Internal,
658
/**//// <summary>由调用者提供连接</summary>
659
External
660
}
661
662
663
/**//// <summary>
664
/// 执行指定数据库连接对象的数据阅读器.
665
/// </summary>
666
/// <remarks>
667
/// 如果是SqlHelper打开连接,当连接关闭DataReader也将关闭.
668
/// 如果是调用都打开连接,DataReader由调用都管理.
669
/// </remarks>
670
/// <param name="connection">一个有效的数据库连接对象</param>
671
/// <param name="transaction">一个有效的事务,或者为 'null'</param>
672
/// <param name="commandType">命令类型 (存储过程,命令文本或其它)</param>
673
/// <param name="commandText">存储过程名或T-SQL语句</param>
674
/// <param name="commandParameters">SqlParameters参数数组,如果没有参数则为'null'</param>
675
/// <param name="connectionOwnership">标识数据库连接对象是由调用者提供还是由SqlHelper提供</param>
676
/// <returns>返回包含结果集的SqlDataReader</returns>
677
private static SqlDataReader ExecuteReader(SqlConnection connection, SqlTransaction transaction, CommandType commandType, string commandText, SqlParameter[] commandParameters, SqlConnectionOwnership connectionOwnership)
678
{
679
if (connection == null) throw new ArgumentNullException("connection");
680
bool mustCloseConnection = false;
681
// 创建命令
682
SqlCommand cmd = new SqlCommand();
683
try
684
{
685
PrepareCommand(cmd, connection, transaction, commandType, commandText, commandParameters, out mustCloseConnection);
686
687
// 创建数据阅读器
688
SqlDataReader dataReader;
689
if (connectionOwnership == SqlConnectionOwnership.External)
690
{
691
dataReader = cmd.ExecuteReader();
692
}
693
else
694
{
695
dataReader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
696
}
697
698
// 清除参数,以便再次使用..
699
// HACK: There is a problem here, the output parameter values are fletched
700
// when the reader is closed, so if the parameters are detached from the command
701
// then the SqlReader can磘 set its values.
702
// When this happen, the parameters can磘 be used again in other command.
703
bool canClear = true;
704
foreach (SqlParameter commandParameter in cmd.Parameters)
705
{
706
if (commandParameter.Direction != ParameterDirection.Input)
707
canClear = false;
708
}
709
if (canClear)
710
{
711
cmd.Parameters.Clear();
712
}
713
return dataReader;
714
}
715
catch
716
{
717
if (mustCloseConnection)
718
connection.Close();
719
throw;
720
}
721
}
722
723
/**//// <summary>
724
/// 执行指定数据库连接字符串的数据阅读器.
725
/// </summary>
726
/// <remarks>
727
/// 示例:
728
/// SqlDataReader dr = ExecuteReader(connString, CommandType.StoredProcedure, "GetOrders");
729
/// </remarks>
730
/// <param name="connectionString">一个有效的数据库连接字符串</param>
731
/// <param name="commandType">命令类型 (存储过程,命令文本或其它)</param>
732
/// <param name="commandText">存储过程名或T-SQL语句</param>
733
/// <returns>返回包含结果集的SqlDataReader</returns>
734
public static SqlDataReader ExecuteReader(string connectionString, CommandType commandType, string commandText)
735
{
736
return ExecuteReader(connectionString, commandType, commandText, (SqlParameter[])null);
737
}
738
739
/**//// <summary>
740
/// 执行指定数据库连接字符串的数据阅读器,指定参数.
741
/// </summary>
742
/// <remarks>
743
/// 示例:
744
/// SqlDataReader dr = ExecuteReader(connString, CommandType.StoredProcedure, "GetOrders", new SqlParameter("@prodid", 24));
745
/// </remarks>
746
/// <param name="connectionString">一个有效的数据库连接字符串</param>
747
/// <param name="commandType">命令类型 (存储过程,命令文本或其它)</param>
748
/// <param name="commandText">存储过程名或T-SQL语句</param>
749
/// <param name="commandParameters">SqlParamter参数数组(new SqlParameter("@prodid", 24))</param>
750
/// <returns>返回包含结果集的SqlDataReader</returns>
751
public static SqlDataReader ExecuteReader(string connectionString, CommandType commandType, string commandText, params SqlParameter[] commandParameters)
752
{
753
if (connectionString == null || connectionString.Length == 0) throw new ArgumentNullException("connectionString");
754
SqlConnection connection = null;
755
try
756
{
757
connection = new SqlConnection(connectionString);
758
connection.Open();
759
760
return ExecuteReader(connection, null, commandType, commandText, commandParameters, SqlConnectionOwnership.Internal);
761
}
762
catch
763
{
764
// If we fail to return the SqlDatReader, we need to close the connection ourselves
765
766
if (connection != null) connection.Close();
767
throw;
768
769
}
770
}
771
772
773
774
/**//// <summary>
775
/// 执行指定数据库连接字符串的数据阅读器,指定参数值.
776
/// </summary>
777
/// <remarks>
778
/// 此方法不提供访问存储过程输出参数和返回值参数.
779
/// 示例:
780
/// SqlDataReader dr = ExecuteReader(connString, "GetOrders", 24, 36);
781
/// </remarks>
782
/// <param name="connectionString">一个有效的数据库连接字符串</param>
783
/// <param name="spName">存储过程名</param>
784
/// <param name="parameterValues">分配给存储过程输入参数的对象数组</param>
785
/// <returns>返回包含结果集的SqlDataReader</returns>
786
public static SqlDataReader ExecuteReader(string connectionString, string spName, params object[] parameterValues)
787
{
788
if (connectionString == null || connectionString.Length == 0) throw new ArgumentNullException("connectionString");
789
if (spName == null || spName.Length == 0) throw new ArgumentNullException("spName");
790
791
if ((parameterValues != null) && (parameterValues.Length > 0))
792
{
793
SqlParameter[] commandParameters = SqlHelperParameterCache.GetSpParameterSet(connectionString, spName);
794
795
AssignParameterValues(commandParameters, parameterValues);
796
797
return ExecuteReader(connectionString, CommandType.StoredProcedure, spName, commandParameters);
798
}
799
else
800
{
801
return ExecuteReader(connectionString, CommandType.StoredProcedure, spName);
802
}
803
}
804
805
/**//// <summary>
806
/// 执行指定数据库连接对象的数据阅读器.
807
/// </summary>
808
/// <remarks>
809
/// 示例:
810
/// SqlDataReader dr = ExecuteReader(conn, CommandType.StoredProcedure, "GetOrders");
811
/// </remarks>
812
/// <param name="connection">一个有效的数据库连接对象</param>
813
/// <param name="commandType">命令类型 (存储过程,命令文本或其它)</param>
814
/// <param name="commandText">存储过程名或T-SQL语句</param>
815
/// <returns>返回包含结果集的SqlDataReader</returns>
816
public static SqlDataReader ExecuteReader(SqlConnection connection, CommandType commandType, string commandText)
817
{
818
return ExecuteReader(connection, commandType, commandText, (SqlParameter[])null);
819
}
820
821
/**//// <summary>
822
/// [调用者方式]执行指定数据库连接对象的数据阅读器,指定参数.
823
/// </summary>
824
/// <remarks>
825
/// 示例:
826
/// SqlDataReader dr = ExecuteReader(conn, CommandType.StoredProcedure, "GetOrders", new SqlParameter("@prodid", 24));
827
/// </remarks>
828
/// <param name="connection">一个有效的数据库连接对象</param>
829
/// <param name="commandType">命令类型 (存储过程,命令文本或其它)</param>
830
/// <param name="commandText">命令类型 (存储过程,命令文本或其它)</param>
831
/// <param name="commandParameters">SqlParamter参数数组</param>
832
/// <returns>返回包含结果集的SqlDataReader</returns>
833
public static SqlDataReader ExecuteReader(SqlConnection connection, CommandType commandType, string commandText, params SqlParameter[] commandParameters)
834
{
835
return ExecuteReader(connection, (SqlTransaction)null, commandType, commandText, commandParameters, SqlConnectionOwnership.External);
836
}
837
838
/**//// <summary>
839
/// [调用者方式]执行指定数据库连接对象的数据阅读器,指定参数值.
840
/// </summary>
841
/// <remarks>
842
/// 此方法不提供访问存储过程输出参数和返回值参数.
843
/// 示例:
844
/// SqlDataReader dr = ExecuteReader(conn, "GetOrders", 24, 36);
845
/// </remarks>
846
/// <param name="connection">一个有效的数据库连接对象</param>
847
/// <param name="spName">T存储过程名</param>
848
/// <param name="parameterValues">分配给存储过程输入参数的对象数组</param>
849
/// <returns>返回包含结果集的SqlDataReader</returns>
850
public static SqlDataReader ExecuteReader(SqlConnection connection, string spName, params object[] parameterValues)
851
{
852
if (connection == null) throw new ArgumentNullException("connection");
853
if (spName == null || spName.Length == 0) throw new ArgumentNullException("spName");
854
if ((parameterValues != null) && (parameterValues.Length > 0))
855
{
856
SqlParameter[] commandParameters = SqlHelperParameterCache.GetSpParameterSet(connection, spName);
857
AssignParameterValues(commandParameters, parameterValues);
858
return ExecuteReader(connection, CommandType.StoredProcedure, spName, commandParameters);
859
}
860
else
861
{
862
return ExecuteReader(connection, CommandType.StoredProcedure, spName);
863
}
864
}
865
866
/**//// <summary>
867
/// [调用者方式]执行指定数据库事务的数据阅读器,指定参数值.
868
/// </summary>
869
/// <remarks>
870
/// 示例:
871
/// SqlDataReader dr = ExecuteReader(trans, CommandType.StoredProcedure, "GetOrders");
872
/// </remarks>
873
/// <param name="transaction">一个有效的连接事务</param>
874
/// <param name="commandType">命令类型 (存储过程,命令文本或其它)</param>
875
/// <param name="commandText">存储过程名称或T-SQL语句</param>
876
/// <returns>返回包含结果集的SqlDataReader</returns>
877
public static SqlDataReader ExecuteReader(SqlTransaction transaction, CommandType commandType, string commandText)
878
{
879
return ExecuteReader(transaction, commandType, commandText, (SqlParameter[])null);
880
}
881
882
/**//// <summary>
883
/// [调用者方式]执行指定数据库事务的数据阅读器,指定参数.
884
/// </summary>
885
/// <remarks>
886
/// 示例:
887
/// SqlDataReader dr = ExecuteReader(trans, CommandType.StoredProcedure, "GetOrders", new SqlParameter("@prodid", 24));
888
/// </remarks>
889
/// <param name="transaction">一个有效的连接事务</param>
890
/// <param name="commandType">命令类型 (存储过程,命令文本或其它)</param>
891
/// <param name="commandText">存储过程名称或T-SQL语句</param>
892
/// <param name="commandParameters">分配给命令的SqlParamter参数数组</param>
893
/// <returns>返回包含结果集的SqlDataReader</returns>
894
public static SqlDataReader ExecuteReader(SqlTransaction transaction, CommandType commandType, string commandText, params SqlParameter[] commandParameters)
895
{
896
if (transaction == null) throw new ArgumentNullException("transaction");
897
if (transaction != null && transaction.Connection == null) throw new ArgumentException("The transaction was rollbacked or commited, please provide an open transaction.", "transaction");
898
899
return ExecuteReader(transaction.Connection, transaction, commandType, commandText, commandParameters, SqlConnectionOwnership.External);
900
}
901
902
/**//// <summary>
903
/// [调用者方式]执行指定数据库事务的数据阅读器,指定参数值.
904
/// </summary>
905
/// <remarks>
906
/// 此方法不提供访问存储过程输出参数和返回值参数.
907
///
908
/// 示例:
909
/// SqlDataReader dr = ExecuteReader(trans, "GetOrders", 24, 36);
910
/// </remarks>
911
/// <param name="transaction">一个有效的连接事务</param>
912
/// <param name="spName">存储过程名称</param>
913
/// <param name="parameterValues">分配给存储过程输入参数的对象数组</param>
914
/// <returns>返回包含结果集的SqlDataReader</returns>
915
public static SqlDataReader ExecuteReader(SqlTransaction transaction, string spName, params object[] parameterValues)
916
{
917
if (transaction == null) throw new ArgumentNullException("transaction");
918
if (transaction != null && transaction.Connection == null) throw new ArgumentException("The transaction was rollbacked or commited, please provide an open transaction.", "transaction");
919
if (spName == null || spName.Length == 0) throw new ArgumentNullException("spName");
920
921
// 如果有参数值
922
if ((parameterValues != null) && (parameterValues.Length > 0))
923
{
924
SqlParameter[] commandParameters = SqlHelperParameterCache.GetSpParameterSet(transaction.Connection, spName);
925
926
AssignParameterValues(commandParameters, parameterValues);
927
return ExecuteReader(transaction, CommandType.StoredProcedure, spName, commandParameters);
928
}
929
else
930
{
931
// 没有参数值
932
return ExecuteReader(transaction, CommandType.StoredProcedure, spName);
933
}
934
}
935
936
937
#endregion ExecuteReader数据阅读器
938
939
ExecuteScalar 返回结果集中的第一行第一列#region ExecuteScalar 返回结果集中的第一行第一列
940
941
942
943
/**//// <summary>
944
/// 执行指定数据库连接字符串的命令,返回结果集中的第一行第一列.
945
/// </summary>
946
/// <remarks>
947
/// 示例:
948
/// int orderCount = (int)ExecuteScalar(connString, CommandType.StoredProcedure, "GetOrderCount");
949
/// </remarks>
950
/// <param name="connectionString">一个有效的数据库连接字符串</param>
951
/// <param name="commandType">命令类型 (存储过程,命令文本或其它)</param>
952
/// <param name="commandText">存储过程名称或T-SQL语句</param>
953
/// <returns>返回结果集中的第一行第一列</returns>
954
public static object ExecuteScalar(string connectionString, CommandType commandType, string commandText)
955
{
956
// 执行参数为空的方法
957
return ExecuteScalar(connectionString, commandType, commandText, (SqlParameter[])null);
958
}
959
960
/**//// <summary>
961
/// 执行指定数据库连接字符串的命令,指定参数,返回结果集中的第一行第一列.
962
/// </summary>
963
/// <remarks>
964
/// 示例:
965
/// int orderCount = (int)ExecuteScalar(connString, CommandType.StoredProcedure, "GetOrderCount", new SqlParameter("@prodid", 24));
966
/// </remarks>
967
/// <param name="connectionString">一个有效的数据库连接字符串</param>
968
/// <param name="commandType">命令类型 (存储过程,命令文本或其它)</param>
969
/// <param name="commandText">存储过程名称或T-SQL语句</param>
970
/// <param name="commandParameters">分配给命令的SqlParamter参数数组</param>
971
/// <returns>返回结果集中的第一行第一列</returns>
972
public static object ExecuteScalar(string connectionString, CommandType commandType, string commandText, params SqlParameter[] commandParameters)
973
{
974
if (connectionString == null || connectionString.Length == 0) throw new ArgumentNullException("connectionString");
975
// 创建并打开数据库连接对象,操作完成释放对象.
976
using (SqlConnection connection = new SqlConnection(connectionString))
977
{
978
connection.Open();
979
// 调用指定数据库连接字符串重载方法.
980
return ExecuteScalar(connection, commandType, commandText, commandParameters);
981
}
982
}
983
984
/**//// <summary>
985
/// 执行指定数据库连接字符串的命令,指定参数值,返回结果集中的第一行第一列.
986
/// </summary>
987
/// <remarks>
988
/// 此方法不提供访问存储过程输出参数和返回值参数.
989
///
990
/// 示例:
991
/// int orderCount = (int)ExecuteScalar(connString, "GetOrderCount", 24, 36);
992
/// </remarks>
993
/// <param name="connectionString">一个有效的数据库连接字符串</param>
994
/// <param name="spName">存储过程名称</param>
995
/// <param name="parameterValues">分配给存储过程输入参数的对象数组</param>
996
/// <returns>返回结果集中的第一行第一列</returns>
997
public static object ExecuteScalar(string connectionString, string spName, params object[] parameterValues)
998
{
999
if (connectionString == null || connectionString.Length == 0) throw new ArgumentNullException("connectionString");
1000
if (spName == null || spName.Length == 0) throw new ArgumentNullException("spName");
1001
1002
// 如果有参数值
1003
if ((parameterValues != null) && (parameterValues.Length > 0))
1004
{
1005
// 从缓存中加载存储过程参数,如果缓存中不存在则从数据库中检索参数信息并加载到缓存中. ()
1006
SqlParameter[] commandParameters = SqlHelperParameterCache.GetSpParameterSet(connectionString, spName);
1007
1008
// 给存储过程参数赋值
1009
AssignParameterValues(commandParameters, parameterValues);
1010
1011
// 调用重载方法
1012
return ExecuteScalar(connectionString, CommandType.StoredProcedure, spName, commandParameters);
1013
}
1014
else
1015
{
1016
// 没有参数值
1017
return ExecuteScalar(connectionString, CommandType.StoredProcedure, spName);
1018
}
1019
}
1020
1021
/**//// <summary>
1022
/// 执行指定数据库连接对象的命令,返回结果集中的第一行第一列.
1023
/// </summary>
1024
/// <remarks>
1025
/// 示例:
1026
/// int orderCount = (int)ExecuteScalar(conn, CommandType.StoredProcedure, "GetOrderCount");
1027
/// </remarks>
1028
/// <param name="connection">一个有效的数据库连接对象</param>
1029
/// <param name="commandType">命令类型 (存储过程,命令文本或其它)</param>
1030
/// <param name="commandText">存储过程名称或T-SQL语句</param>
1031
/// <returns>返回结果集中的第一行第一列</returns>
1032
public static object ExecuteScalar(SqlConnection connection, CommandType commandType, string commandText)
1033
{
1034
// 执行参数为空的方法
1035
return ExecuteScalar(connection, commandType, commandText, (SqlParameter[])null);
1036
}
1037
1038
/**//// <summary>
1039
/// 执行指定数据库连接对象的命令,指定参数,返回结果集中的第一行第一列.
1040
/// </summary>
1041
/// <remarks>
1042
/// 示例:
1043
/// int orderCount = (int)ExecuteScalar(conn, CommandType.StoredProcedure, "GetOrderCount", new SqlParameter("@prodid", 24));
1044
/// </remarks>
1045
/// <param name="connection">一个有效的数据库连接对象</param>
1046
/// <param name="commandType">命令类型 (存储过程,命令文本或其它)</param>
1047
/// <param name="commandText">存储过程名称或T-SQL语句</param>
1048
/// <param name="commandParameters">分配给命令的SqlParamter参数数组</param>
1049
/// <returns>返回结果集中的第一行第一列</returns>
1050
public static object ExecuteScalar(SqlConnection connection, CommandType commandType, string commandText, params SqlParameter[] commandParameters)
1051
{
1052
if (connection == null) throw new ArgumentNullException("connection");
1053
1054
// 创建SqlCommand命令,并进行预处理
1055
SqlCommand cmd = new SqlCommand();
1056
bool mustCloseConnection = false;
1057
PrepareCommand(cmd, connection, (SqlTransaction)null, commandType, commandText, commandParameters, out mustCloseConnection);
1058
1059
// 执行SqlCommand命令,并返回结果.
1060
object retval = cmd.ExecuteScalar();
1061
1062
// 清除参数,以便再次使用.
1063
cmd.Parameters.Clear();
1064
1065
if (mustCloseConnection)
1066
connection.Close();
1067
1068
return retval;
1069
}
1070
1071
1072
/**//// <summary>
1073
/// 执行指定数据库连接对象的命令,指定参数值,返回结果集中的第一行第一列.
1074
/// </summary>
1075
/// <remarks>
1076
/// 此方法不提供访问存储过程输出参数和返回值参数.
1077
///
1078
/// 示例:
1079
/// int orderCount = (int)ExecuteScalar(conn, "GetOrderCount", 24, 36);
1080
/// </remarks>
1081
/// <param name="connection">一个有效的数据库连接对象</param>
1082
/// <param name="spName">存储过程名称</param>
1083
/// <param name="parameterValues">分配给存储过程输入参数的对象数组</param>
1084
/// <returns>返回结果集中的第一行第一列</returns>
1085
public static object ExecuteScalar(SqlConnection connection, string spName, params object[] parameterValues)
1086
{
1087
if (connection == null) throw new ArgumentNullException("connection");
1088
if (spName == null || spName.Length == 0) throw new ArgumentNullException("spName");
1089
1090
// 如果有参数值
1091
if ((parameterValues != null) && (parameterValues.Length > 0))
1092
{
1093
// 从缓存中加载存储过程参数,如果缓存中不存在则从数据库中检索参数信息并加载到缓存中. ()
1094
SqlParameter[] commandParameters = SqlHelperParameterCache.GetSpParameterSet(connection, spName);
1095
// 给存储过程参数赋值
1096
AssignParameterValues(commandParameters, parameterValues);
1097
// 调用重载方法
1098
return ExecuteScalar(connection, CommandType.StoredProcedure, spName, commandParameters);
1099
}
1100
else
1101
{
1102
// 没有参数值
1103
return ExecuteScalar(connection, CommandType.StoredProcedure, spName);
1104
}
1105
}
1106
1107
/**//// <summary>
1108
/// 执行指定数据库事务的命令,返回结果集中的第一行第一列.
1109
/// </summary>
1110
/// <remarks>
1111
/// 示例:
1112
/// int orderCount = (int)ExecuteScalar(trans, CommandType.StoredProcedure, "GetOrderCount");
1113
/// </remarks>
1114
/// <param name="transaction">一个有效的连接事务</param>
1115
/// <param name="commandType">命令类型 (存储过程,命令文本或其它)</param>
1116
/// <param name="commandText">存储过程名称或T-SQL语句</param>
1117
/// <returns>返回结果集中的第一行第一列</returns>
1118
public static object ExecuteScalar(SqlTransaction transaction, CommandType commandType, string commandText)
1119
{
1120
// 执行参数为空的方法
1121
return ExecuteScalar(transaction, commandType, commandText, (SqlParameter[])null);
1122
1123
}
1124
1125
1126
/**//// <summary>
1127
/// 执行指定数据库事务的命令,指定参数,返回结果集中的第一行第一列.
1128
/// </summary>
1129
/// <remarks>
1130
/// 示例:
1131
/// int orderCount = (int)ExecuteScalar(trans, CommandType.StoredProcedure, "GetOrderCount", new SqlParameter("@prodid", 24));
1132
/// </remarks>
1133
/// <param name="transaction">一个有效的连接事务</param>
1134
/// <param name="commandType">命令类型 (存储过程,命令文本或其它)</param>
1135
/// <param name="commandText">存储过程名称或T-SQL语句</param>
1136
/// <param name="commandParameters">分配给命令的SqlParamter参数数组</param>
1137
/// <returns>返回结果集中的第一行第一列</returns>
1138
public static object ExecuteScalar(SqlTransaction transaction, CommandType commandType, string commandText, params SqlParameter[] commandParameters)
1139
{
1140
if (transaction == null) throw new ArgumentNullException("transaction");
1141
if (transaction != null && transaction.Connection == null) throw new ArgumentException("The transaction was rollbacked or commited, please provide an open transaction.", "transaction");
1142
1143
// 创建SqlCommand命令,并进行预处理
1144
SqlCommand cmd = new SqlCommand();
1145
bool mustCloseConnection = false;
1146
PrepareCommand(cmd, transaction.Connection, transaction, commandType, commandText, commandParameters, out mustCloseConnection);
1147
// 执行SqlCommand命令,并返回结果.
1148
object retval = cmd.ExecuteScalar();
1149
// 清除参数,以便再次使用.
1150
cmd.Parameters.Clear();
1151
return retval;
1152
}
1153
1154
/**//// <summary>
1155
/// 执行指定数据库事务的命令,指定参数值,返回结果集中的第一行第一列.
1156
/// </summary>
1157
/// <remarks>
1158
/// 此方法不提供访问存储过程输出参数和返回值参数.
1159
///
1160
/// 示例:
1161
/// int orderCount = (int)ExecuteScalar(trans, "GetOrderCount", 24, 36);
1162
/// </remarks>
1163
/// <param name="transaction">一个有效的连接事务</param>
1164
/// <param name="spName">存储过程名称</param>
1165
/// <param name="parameterValues">分配给存储过程输入参数的对象数组</param>
1166
/// <returns>返回结果集中的第一行第一列</returns>
1167
public static object ExecuteScalar(SqlTransaction transaction, string spName, params object[] parameterValues)
1168
{
1169
if (transaction == null) throw new ArgumentNullException("transaction");
1170
if (transaction != null && transaction.Connection == null) throw new ArgumentException("The transaction was rollbacked or commited, please provide an open transaction.", "transaction");
1171
if (spName == null || spName.Length == 0) throw new ArgumentNullException("spName");
1172
1173
// 如果有参数值
1174
if ((parameterValues != null) && (parameterValues.Length > 0))
1175
{
1176
// PPull the parameters for this stored procedure from the parameter cache ()
1177
SqlParameter[] commandParameters = SqlHelperParameterCache.GetSpParameterSet(transaction.Connection, spName);
1178
// 给存储过程参数赋值
1179
AssignParameterValues(commandParameters, parameterValues);
1180
1181
// 调用重载方法
1182
return ExecuteScalar(transaction, CommandType.StoredProcedure, spName, commandParameters);
1183
}
1184
else
1185
{
1186
// 没有参数值
1187
return ExecuteScalar(transaction, CommandType.StoredProcedure, spName);
1188
}
1189
}
1190
1191
#endregion ExecuteScalar
1192
1193
ExecuteXmlReader XML阅读器#region ExecuteXmlReader XML阅读器
1194
1195
/**//// <summary>
1196
/// 执行指定数据库连接对象的SqlCommand命令,并产生一个XmlReader对象做为结果集返回.
1197
/// </summary>
1198
/// <remarks>
1199
/// 示例:
1200
/// XmlReader r = ExecuteXmlReader(conn, CommandType.StoredProcedure, "GetOrders");
1201
/// </remarks>
1202
/// <param name="connection">一个有效的数据库连接对象</param>
1203
/// <param name="commandType">命令类型 (存储过程,命令文本或其它)</param>
1204
/// <param name="commandText">存储过程名称或T-SQL语句 using "FOR XML AUTO"</param>
1205
/// <returns>返回XmlReader结果集对象.</returns>
1206
public static XmlReader ExecuteXmlReader(SqlConnection connection, CommandType commandType, string commandText)
1207
{
1208
// 执行参数为空的方法
1209
return ExecuteXmlReader(connection, commandType, commandText, (SqlParameter[])null);
1210
}
1211
1212
1213
/**//// <summary>
1214
/// 执行指定数据库连接对象的SqlCommand命令,并产生一个XmlReader对象做为结果集返回,指定参数.
1215
/// </summary>
1216
/// <remarks>
1217
/// 示例:
1218
/// XmlReader r = ExecuteXmlReader(conn, CommandType.StoredProcedure, "GetOrders", new SqlParameter("@prodid", 24));
1219
/// </remarks>
1220
/// <param name="connection">一个有效的数据库连接对象</param>
1221
/// <param name="commandType">命令类型 (存储过程,命令文本或其它)</param>
1222
/// <param name="commandText">存储过程名称或T-SQL语句 using "FOR XML AUTO"</param>
1223
/// <param name="commandParameters">分配给命令的SqlParamter参数数组</param>
1224
/// <returns>返回XmlReader结果集对象.</returns>
1225
public static XmlReader ExecuteXmlReader(SqlConnection connection, CommandType commandType, string commandText, params SqlParameter[] commandParameters)
1226
{
1227
if (connection == null) throw new ArgumentNullException("connection");
1228
1229
bool mustCloseConnection = false;
1230
// 创建SqlCommand命令,并进行预处理
1231
SqlCommand cmd = new SqlCommand();
1232
try
1233
{
1234
PrepareCommand(cmd, connection, (SqlTransaction)null, commandType, commandText, commandParameters, out mustCloseConnection);
1235
1236
// 执行命令
1237
XmlReader retval = cmd.ExecuteXmlReader();
1238
// 清除参数,以便再次使用.
1239
cmd.Parameters.Clear();
1240
return retval;
1241
}
1242
catch
1243
{
1244
if (mustCloseConnection)
1245
connection.Close();
1246
throw;
1247
}
1248
}
1249
1250
/**//// <summary>
1251
/// 执行指定数据库连接对象的SqlCommand命令,并产生一个XmlReader对象做为结果集返回,指定参数值.
1252
/// </summary>
1253
/// <remarks>
1254
/// 此方法不提供访问存储过程输出参数和返回值参数.
1255
///
1256
/// 示例:
1257
/// XmlReader r = ExecuteXmlReader(conn, "GetOrders", 24, 36);
1258
/// </remarks>
1259
/// <param name="connection">一个有效的数据库连接对象</param>
1260
/// <param name="spName">存储过程名称 using "FOR XML AUTO"</param>
1261
/// <param name="parameterValues">分配给存储过程输入参数的对象数组</param>
1262
/// <returns>返回XmlReader结果集对象.</returns>
1263
public static XmlReader ExecuteXmlReader(SqlConnection connection, string spName, params object[] parameterValues)
1264
{
1265
if (connection == null) throw new ArgumentNullException("connection");
1266
if (spName == null || spName.Length == 0) throw new ArgumentNullException("spName");
1267
1268
// 如果有参数值
1269
if ((parameterValues != null) && (parameterValues.Length > 0))
1270
{
1271
// 从缓存中加载存储过程参数,如果缓存中不存在则从数据库中检索参数信息并加载到缓存中. ()
1272
SqlParameter[] commandParameters = SqlHelperParameterCache.GetSpParameterSet(connection, spName);
1273
1274
// 给存储过程参数赋值
1275
AssignParameterValues(commandParameters, parameterValues);
1276
// 调用重载方法
1277
return ExecuteXmlReader(connection, CommandType.StoredProcedure, spName, commandParameters);
1278
}
1279
else
1280
{
1281
// 没有参数值
1282
return ExecuteXmlReader(connection, CommandType.StoredProcedure, spName);
1283
}
1284
}
1285
1286
1287
/**//// <summary>
1288
/// 执行指定数据库事务的SqlCommand命令,并产生一个XmlReader对象做为结果集返回.
1289
/// </summary>
1290
/// <remarks>
1291
/// 示例:
1292
/// XmlReader r = ExecuteXmlReader(trans, CommandType.StoredProcedure, "GetOrders");
1293
/// </remarks>
1294
/// <param name="transaction">一个有效的连接事务</param>
1295
/// <param name="commandType">命令类型 (存储过程,命令文本或其它)</param>
1296
/// <param name="commandText">存储过程名称或T-SQL语句 using "FOR XML AUTO"</param>
1297
/// <returns>返回XmlReader结果集对象.</returns>
1298
public static XmlReader ExecuteXmlReader(SqlTransaction transaction, CommandType commandType, string commandText)
1299
{
1300
// 执行参数为空的方法
1301
return ExecuteXmlReader(transaction, commandType, commandText, (SqlParameter[])null);
1302
}
1303
1304
/**//// <summary>
1305
/// 执行指定数据库事务的SqlCommand命令,并产生一个XmlReader对象做为结果集返回,指定参数.
1306
/// </summary>
1307
/// <remarks>
1308
/// 示例:
1309
/// XmlReader r = ExecuteXmlReader(trans, CommandType.StoredProcedure, "GetOrders", new SqlParameter("@prodid", 24));
1310
/// </remarks>
1311
/// <param name="transaction">一个有效的连接事务</param>
1312
/// <param name="commandType">命令类型 (存储过程,命令文本或其它)</param>
1313
/// <param name="commandText">存储过程名称或T-SQL语句 using "FOR XML AUTO"</param>
1314
/// <param name="commandParameters">分配给命令的SqlParamter参数数组</param>
1315
/// <returns>返回XmlReader结果集对象.</returns>
1316
public static XmlReader ExecuteXmlReader(SqlTransaction transaction, CommandType commandType, string commandText, params SqlParameter[] commandParameters)
1317
{
1318
if (transaction == null) throw new ArgumentNullException("transaction");
1319
if (transaction != null && transaction.Connection == null) throw new ArgumentException("The transaction was rollbacked or commited, please provide an open transaction.", "transaction");
1320
1321
// 创建SqlCommand命令,并进行预处理
1322
SqlCommand cmd = new SqlCommand();
1323
bool mustCloseConnection = false;
1324
PrepareCommand(cmd, transaction.Connection, transaction, commandType, commandText, commandParameters, out mustCloseConnection);
1325
1326
// 执行命令
1327
XmlReader retval = cmd.ExecuteXmlReader();
1328
// 清除参数,以便再次使用.
1329
cmd.Parameters.Clear();
1330
return retval;
1331
}
1332
1333
/**//// <summary>
1334
/// 执行指定数据库事务的SqlCommand命令,并产生一个XmlReader对象做为结果集返回,指定参数值.
1335
/// </summary>
1336
/// <remarks>
1337
/// 此方法不提供访问存储过程输出参数和返回值参数.
1338
///
1339
/// 示例:
1340
/// XmlReader r = ExecuteXmlReader(trans, "GetOrders", 24, 36);
1341
/// </remarks>
1342
/// <param name="transaction">一个有效的连接事务</param>
1343
/// <param name="spName">存储过程名称</param>
1344
/// <param name="parameterValues">分配给存储过程输入参数的对象数组</param>
1345
/// <returns>返回一个包含结果集的DataSet.</returns>
1346
public static XmlReader ExecuteXmlReader(SqlTransaction transaction, string spName, params object[] parameterValues)
1347
{
1348
if (transaction == null) throw new ArgumentNullException("transaction");
1349
if (transaction != null && transaction.Connection == null) throw new ArgumentException("The transaction was rollbacked or commited, please provide an open transaction.", "transaction");
1350
if (spName == null || spName.Length == 0) throw new ArgumentNullException("spName");
1351
1352
// 如果有参数值
1353
if ((parameterValues != null) && (parameterValues.Length > 0))
1354
{
1355
// 从缓存中加载存储过程参数,如果缓存中不存在则从数据库中检索参数信息并加载到缓存中. ()
1356
SqlParameter[] commandParameters = SqlHelperParameterCache.GetSpParameterSet(transaction.Connection, spName);
1357
1358
// 给存储过程参数赋值
1359
AssignParameterValues(commandParameters, parameterValues);
1360
1361
// 调用重载方法
1362
return ExecuteXmlReader(transaction, CommandType.StoredProcedure, spName, commandParameters);
1363
}
1364
else
1365
{
1366
// 没有参数值
1367
return ExecuteXmlReader(transaction, CommandType.StoredProcedure, spName);
1368
}
1369
}
1370
1371
#endregion ExecuteXmlReader 阅读器结束
1372
1373
FillDataset 填充数据集#region FillDataset 填充数据集
1374
1375
/**//// <summary>
1376
/// 执行指定数据库连接字符串的命令,映射数据表并填充数据集.
1377
/// </summary>
1378
/// <remarks>
1379
/// 示例:
1380
/// FillDataset(connString, CommandType.StoredProcedure, "GetOrders", ds, new string[] {"orders"});
1381
/// </remarks>
1382
/// <param name="connectionString">一个有效的数据库连接字符串</param>
1383
/// <param name="commandType">命令类型 (存储过程,命令文本或其它)</param>
1384
/// <param name="commandText">存储过程名称或T-SQL语句</param>
1385
/// <param name="dataSet">要填充结果集的DataSet实例</param>
1386
/// <param name="tableNames">表映射的数据表数组
1387
/// 用户定义的表名 (可有是实际的表名.)</param>
1388
public static void FillDataset(string connectionString, CommandType commandType, string commandText, DataSet dataSet, string[] tableNames)
1389
{
1390
if (connectionString == null || connectionString.Length == 0) throw new ArgumentNullException("connectionString");
1391
if (dataSet == null) throw new ArgumentNullException("dataSet");
1392
1393
// 创建并打开数据库连接对象,操作完成释放对象.
1394
using (SqlConnection connection = new SqlConnection(connectionString))
1395
{
1396
connection.Open();
1397
// 调用指定数据库连接字符串重载方法.
1398
FillDataset(connection, commandType, commandText, dataSet, tableNames);
1399
}
1400
}
1401
1402
/**//// <summary>
1403
/// 执行指定数据库连接字符串的命令,映射数据表并填充数据集.指定命令参数.
1404
/// </summary>
1405
/// <remarks>
1406
/// 示例:
1407
/// FillDataset(connString, CommandType.StoredProcedure, "GetOrders", ds, new string[] {"orders"}, new SqlParameter("@prodid", 24));
1408
/// </remarks>
1409
/// <param name="connectionString">一个有效的数据库连接字符串</param>
1410
/// <param name="commandType">命令类型 (存储过程,命令文本或其它)</param>
1411
/// <param name="commandText">存储过程名称或T-SQL语句</param>
1412
/// <param name="commandParameters">分配给命令的SqlParamter参数数组</param>
1413
/// <param name="dataSet">要填充结果集的DataSet实例</param>
1414
/// <param name="tableNames">表映射的数据表数组
1415
/// 用户定义的表名 (可有是实际的表名.)
1416
/// </param>
1417
public static void FillDataset(string connectionString, CommandType commandType,
1418
string commandText, DataSet dataSet, string[] tableNames,
1419
params SqlParameter[] commandParameters)
1420
{
1421
if (connectionString == null || connectionString.Length == 0) throw new ArgumentNullException("connectionString");
1422
if (dataSet == null) throw new ArgumentNullException("dataSet");
1423
// 创建并打开数据库连接对象,操作完成释放对象.
1424
using (SqlConnection connection = new SqlConnection(connectionString))
1425
{
1426
connection.Open();
1427
// 调用指定数据库连接字符串重载方法.
1428
FillDataset(connection, commandType, commandText, dataSet, tableNames, commandParameters);
1429
}
1430
}
1431
1432
/**//// <summary>
1433
/// 执行指定数据库连接字符串的命令,映射数据表并填充数据集,指定存储过程参数值.
1434
/// </summary>
1435
/// <remarks>
1436
/// 此方法不提供访问存储过程输出参数和返回值参数.
1437
///
1438
/// 示例:
1439
/// FillDataset(connString, CommandType.StoredProcedure, "GetOrders", ds, new string[] {"orders"}, 24);
1440
/// </remarks>
1441
/// <param name="connectionString">一个有效的数据库连接字符串</param>
1442
/// <param name="spName">存储过程名称</param>
1443
/// <param name="dataSet">要填充结果集的DataSet实例</param>
1444
/// <param name="tableNames">表映射的数据表数组
1445
/// 用户定义的表名 (可有是实际的表名.)
1446
/// </param>
1447
/// <param name="parameterValues">分配给存储过程输入参数的对象数组</param>
1448
public static void FillDataset(string connectionString, string spName,
1449
DataSet dataSet, string[] tableNames,
1450
params object[] parameterValues)
1451
{
1452
if (connectionString == null || connectionString.Length == 0) throw new ArgumentNullException("connectionString");
1453
if (dataSet == null) throw new ArgumentNullException("dataSet");
1454
// 创建并打开数据库连接对象,操作完成释放对象.
1455
using (SqlConnection connection = new SqlConnection(connectionString))
1456
{
1457
connection.Open();
1458
1459
// 调用指定数据库连接字符串重载方法.
1460
FillDataset(connection, spName, dataSet, tableNames, parameterValues);
1461
}
1462
}
1463
1464
/**//// <summary>
1465
/// 执行指定数据库连接对象的命令,映射数据表并填充数据集.
1466
/// </summary>
1467
/// <remarks>
1468
/// 示例:
1469
/// FillDataset(conn, CommandType.StoredProcedure, "GetOrders", ds, new string[] {"orders"});
1470
/// </remarks>
1471
/// <param name="connection">一个有效的数据库连接对象</param>
1472
/// <param name="commandType">命令类型 (存储过程,命令文本或其它)</param>
1473
/// <param name="commandText">存储过程名称或T-SQL语句</param>
1474
/// <param name="dataSet">要填充结果集的DataSet实例</param>
1475
/// <param name="tableNames">表映射的数据表数组
1476
/// 用户定义的表名 (可有是实际的表名.)
1477
/// </param>
1478
public static void FillDataset(SqlConnection connection, CommandType commandType,
1479
string commandText, DataSet dataSet, string[] tableNames)
1480
{
1481
FillDataset(connection, commandType, commandText, dataSet, tableNames, null);
1482
}
1483
1484
1485
/**//// <summary>
1486
/// 执行指定数据库连接对象的命令,映射数据表并填充数据集,指定参数.
1487
/// </summary>
1488
/// <remarks>
1489
/// 示例:
1490
/// FillDataset(conn, CommandType.StoredProcedure, "GetOrders", ds, new string[] {"orders"}, new SqlParameter("@prodid", 24));
1491
/// </remarks>
1492
/// <param name="connection">一个有效的数据库连接对象</param>
1493
/// <param name="commandType">命令类型 (存储过程,命令文本或其它)</param>
1494
/// <param name="commandText">存储过程名称或T-SQL语句</param>
1495
/// <param name="dataSet">要填充结果集的DataSet实例</param>
1496
/// <param name="tableNames">表映射的数据表数组
1497
/// 用户定义的表名 (可有是实际的表名.)
1498
/// </param>
1499
/// <param name="commandParameters">分配给命令的SqlParamter参数数组</param>
1500
public static void FillDataset(SqlConnection connection, CommandType commandType,
1501
string commandText, DataSet dataSet, string[] tableNames,
1502
params SqlParameter[] commandParameters)
1503
{
1504
FillDataset(connection, null, commandType, commandText, dataSet, tableNames, commandParameters);
1505
}
1506
1507
/**//// <summary>
1508
/// 执行指定数据库连接对象的命令,映射数据表并填充数据集,指定存储过程参数值.
1509
/// </summary>
1510
/// <remarks>
1511
/// 此方法不提供访问存储过程输出参数和返回值参数.
1512
///
1513
/// 示例:
1514
/// FillDataset(conn, "GetOrders", ds, new string[] {"orders"}, 24, 36);
1515
/// </remarks>
1516
/// <param name="connection">一个有效的数据库连接对象</param>
1517
/// <param name="spName">存储过程名称</param>
1518
/// <param name="dataSet">要填充结果集的DataSet实例</param>
1519
/// <param name="tableNames">表映射的数据表数组
1520
/// 用户定义的表名 (可有是实际的表名.)
1521
/// </param>
1522
/// <param name="parameterValues">分配给存储过程输入参数的对象数组</param>
1523
public static void FillDataset(SqlConnection connection, string spName,
1524
DataSet dataSet, string[] tableNames,
1525
params object[] parameterValues)
1526
{
1527
if (connection == null) throw new ArgumentNullException("connection");
1528
if (dataSet == null) throw new ArgumentNullException("dataSet");
1529
if (spName == null || spName.Length == 0) throw new ArgumentNullException("spName");
1530
1531
// 如果有参数值
1532
if ((parameterValues != null) && (parameterValues.Length > 0))
1533
{
1534
// 从缓存中加载存储过程参数,如果缓存中不存在则从数据库中检索参数信息并加载到缓存中. ()
1535
SqlParameter[] commandParameters = SqlHelperParameterCache.GetSpParameterSet(connection, spName);
1536
// 给存储过程参数赋值
1537
AssignParameterValues(commandParameters, parameterValues);
1538
// 调用重载方法
1539
FillDataset(connection, CommandType.StoredProcedure, spName, dataSet, tableNames, commandParameters);
1540
}
1541
else
1542
{
1543
// 没有参数值
1544
FillDataset(connection, CommandType.StoredProcedure, spName, dataSet, tableNames);
1545
}
1546
}
1547
1548
/**//// <summary>
1549
/// 执行指定数据库事务的命令,映射数据表并填充数据集.
1550
/// </summary>
1551
/// <remarks>
1552
/// 示例:
1553
/// FillDataset(trans, CommandType.StoredProcedure, "GetOrders", ds, new string[] {"orders"});
1554
/// </remarks>
1555
/// <param name="transaction">一个有效的连接事务</param>
1556
/// <param name="commandType">命令类型 (存储过程,命令文本或其它)</param>
1557
/// <param name="commandText">存储过程名称或T-SQL语句</param>
1558
/// <param name="dataSet">要填充结果集的DataSet实例</param>
1559
/// <param name="tableNames">表映射的数据表数组
1560
/// 用户定义的表名 (可有是实际的表名.)
1561
/// </param>
1562
public static void FillDataset(SqlTransaction transaction, CommandType commandType,
1563
string commandText,
1564
DataSet dataSet, string[] tableNames)
1565
{
1566
FillDataset(transaction, commandType, commandText, dataSet, tableNames, null);
1567
}
1568
1569
/**//// <summary>
1570
/// 执行指定数据库事务的命令,映射数据表并填充数据集,指定参数.
1571
/// </summary>
1572
/// <remarks>
1573
/// 示例:
1574
/// FillDataset(trans, CommandType.StoredProcedure, "GetOrders", ds, new string[] {"orders"}, new SqlParameter("@prodid", 24));
1575
/// </remarks>
1576
/// <param name="transaction">一个有效的连接事务</param>
1577
/// <param name="commandType">命令类型 (存储过程,命令文本或其它)</param>
1578
/// <param name="commandText">存储过程名称或T-SQL语句</param>
1579
/// <param name="dataSet">要填充结果集的DataSet实例</param>
1580
/// <param name="tableNames">表映射的数据表数组
1581
/// 用户定义的表名 (可有是实际的表名.)
1582
/// </param>
1583
/// <param name="commandParameters">分配给命令的SqlParamter参数数组</param>
1584
public static void FillDataset(SqlTransaction transaction, CommandType commandType,
1585
string commandText, DataSet dataSet, string[] tableNames,
1586
params SqlParameter[] commandParameters)
1587
{
1588
FillDataset(transaction.Connection, transaction, commandType, commandText, dataSet, tableNames, commandParameters);
1589
}
1590
1591
/**//// <summary>
1592
/// 执行指定数据库事务的命令,映射数据表并填充数据集,指定存储过程参数值.
1593
/// </summary>
1594
/// <remarks>
1595
/// 此方法不提供访问存储过程输出参数和返回值参数.
1596
///
1597
/// 示例:
1598
/// FillDataset(trans, "GetOrders", ds, new string[]{"orders"}, 24, 36);
1599
/// </remarks>
1600
/// <param name="transaction">一个有效的连接事务</param>
1601
/// <param name="spName">存储过程名称</param>
1602
/// <param name="dataSet">要填充结果集的DataSet实例</param>
1603
/// <param name="tableNames">表映射的数据表数组
1604
/// 用户定义的表名 (可有是实际的表名.)
1605
/// </param>
1606
/// <param name="parameterValues">分配给存储过程输入参数的对象数组</param>
1607
public static void FillDataset(SqlTransaction transaction, string spName,
1608
DataSet dataSet, string[] tableNames,
1609
params object[] parameterValues)
1610
{
1611
if (transaction == null) throw new ArgumentNullException("transaction");
1612
if (transaction != null && transaction.Connection == null) throw new ArgumentException("The transaction was rollbacked or commited, please provide an open transaction.", "transaction");
1613
if (dataSet == null) throw new ArgumentNullException("dataSet");
1614
if (spName == null || spName.Length == 0) throw new ArgumentNullException("spName");
1615
1616
// 如果有参数值
1617
if ((parameterValues != null) && (parameterValues.Length > 0))
1618
{
1619
// 从缓存中加载存储过程参数,如果缓存中不存在则从数据库中检索参数信息并加载到缓存中. ()
1620
SqlParameter[] commandParameters = SqlHelperParameterCache.GetSpParameterSet(transaction.Connection, spName);
1621
// 给存储过程参数赋值
1622
AssignParameterValues(commandParameters, parameterValues);
1623
1624
// 调用重载方法
1625
FillDataset(transaction, CommandType.StoredProcedure, spName, dataSet, tableNames, commandParameters);
1626
}
1627
else
1628
{
1629
// 没有参数值
1630
FillDataset(transaction, CommandType.StoredProcedure, spName, dataSet, tableNames);
1631
}
1632
}
1633
1634
1635
/**//// <summary>
1636
/// [私有方法][内部调用]执行指定数据库连接对象/事务的命令,映射数据表并填充数据集,DataSet/TableNames/SqlParameters.
1637
/// </summary>
1638
/// <remarks>
1639
/// 示例:
1640
/// FillDataset(conn, trans, CommandType.StoredProcedure, "GetOrders", ds, new string[] {"orders"}, new SqlParameter("@prodid", 24));
1641
/// </remarks>
1642
/// <param name="connection">一个有效的数据库连接对象</param>
1643
/// <param name="transaction">一个有效的连接事务</param>
1644
/// <param name="commandType">命令类型 (存储过程,命令文本或其它)</param>
1645
/// <param name="commandText">存储过程名称或T-SQL语句</param>
1646
/// <param name="dataSet">要填充结果集的DataSet实例</param>
1647
/// <param name="tableNames">表映射的数据表数组
1648
/// 用户定义的表名 (可有是实际的表名.)
1649
/// </param>
1650
/// <param name="commandParameters">分配给命令的SqlParamter参数数组</param>
1651
private static void FillDataset(SqlConnection connection, SqlTransaction transaction, CommandType commandType,
1652
string commandText, DataSet dataSet, string[] tableNames,
1653
params SqlParameter[] commandParameters)
1654
{
1655
if (connection == null) throw new ArgumentNullException("connection");
1656
if (dataSet == null) throw new ArgumentNullException("dataSet");
1657
// 创建SqlCommand命令,并进行预处理
1658
SqlCommand command = new SqlCommand();
1659
bool mustCloseConnection = false;
1660
PrepareCommand(command, connection, transaction, commandType, commandText, commandParameters, out mustCloseConnection);
1661
1662
// 执行命令
1663
using (SqlDataAdapter dataAdapter = new SqlDataAdapter(command))
1664
{
1665
// 追加表映射
1666
if (tableNames != null && tableNames.Length > 0)
1667
{
1668
string tableName = "Table";
1669
for (int index = 0; index < tableNames.Length; index++)
1670
{
1671
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");
1672
dataAdapter.TableMappings.Add(tableName, tableNames[index]);
1673
tableName += (index + 1).ToString();
1674
}
1675
}
1676
1677
// 填充数据集使用默认表名称
1678
dataAdapter.Fill(dataSet);
1679
1680
// 清除参数,以便再次使用.
1681
command.Parameters.Clear();
1682
}
1683
1684
if (mustCloseConnection)
1685
connection.Close();
1686
}
1687
#endregion
1688
1689
UpdateDataset 更新数据集#region UpdateDataset 更新数据集
1690
1691
/**//// <summary>
1692
/// 执行数据集更新到数据库,指定inserted, updated, or deleted命令.
1693
/// </summary>
1694
/// <remarks>
1695
/// 示例:
1696
/// UpdateDataset(conn, insertCommand, deleteCommand, updateCommand, dataSet, "Order");
1697
/// </remarks>
1698
/// <param name="insertCommand">[追加记录]一个有效的T-SQL语句或存储过程</param>
1699
/// <param name="deleteCommand">[删除记录]一个有效的T-SQL语句或存储过程</param>
1700
/// <param name="updateCommand">[更新记录]一个有效的T-SQL语句或存储过程</param>
1701
/// <param name="dataSet">要更新到数据库的DataSet</param>
1702
/// <param name="tableName">要更新到数据库的DataTable</param>
1703
public static void UpdateDataset(SqlCommand insertCommand, SqlCommand deleteCommand, SqlCommand updateCommand, DataSet dataSet, string tableName)
1704
{
1705
if (insertCommand == null) throw new ArgumentNullException("insertCommand");
1706
if (deleteCommand == null) throw new ArgumentNullException("deleteCommand");
1707
if (updateCommand == null) throw new ArgumentNullException("updateCommand");
1708
if (tableName == null || tableName.Length == 0) throw new ArgumentNullException("tableName");
1709
1710
// 创建SqlDataAdapter,当操作完成后释放.
1711
using (SqlDataAdapter dataAdapter = new SqlDataAdapter())
1712
{
1713
// 设置数据适配器命令
1714
dataAdapter.UpdateCommand = updateCommand;
1715
dataAdapter.InsertCommand = insertCommand;
1716
dataAdapter.DeleteCommand = deleteCommand;
1717
1718
// 更新数据集改变到数据库
1719
dataAdapter.Update(dataSet, tableName);
1720
// 提交所有改变到数据集.
1721
dataSet.AcceptChanges();
1722
}