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