PageRenderTime 60ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 1ms

/MDXParameter/MDXParameter/SqlHelper.cs

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