No Description

OracleHelper.cs 30KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Configuration;
  6. using System.Collections;
  7. using System.Data.OracleClient;
  8. using System.Data;
  9. namespace XYFDRQ.DBUtility
  10. {
  11. /// <summary>
  12. /// A helper class used to execute queries against an Oracle database
  13. /// </summary>
  14. public abstract class OracleHelper
  15. {
  16. // Read the connection strings from the configuration file
  17. public static readonly string ConnectionStringLocalTransaction = ConfigurationManager.ConnectionStrings["OraConnString1"].ConnectionString;
  18. public static readonly string ConnectionStringInventoryDistributedTransaction = ConfigurationManager.ConnectionStrings["OraConnString2"].ConnectionString;
  19. public static readonly string ConnectionStringOrderDistributedTransaction = ConfigurationManager.ConnectionStrings["OraConnString3"].ConnectionString;
  20. public static readonly string ConnectionStringProfile = ConfigurationManager.ConnectionStrings["OraProfileConnString"].ConnectionString;
  21. public static readonly string ConnectionStringMembership = ConfigurationManager.ConnectionStrings["OraMembershipConnString"].ConnectionString;
  22. //Create a hashtable for the parameter cached
  23. private static Hashtable parmCache = Hashtable.Synchronized(new Hashtable());
  24. /// <summary>
  25. /// Execute a database query which does not include a select
  26. /// </summary>
  27. /// <param name="connString">Connection string to database</param>
  28. /// <param name="cmdType">Command type either stored procedure or SQL</param>
  29. /// <param name="cmdText">Acutall SQL Command</param>
  30. /// <param name="commandParameters">Parameters to bind to the command</param>
  31. /// <returns></returns>
  32. public static int ExecuteNonQuery(string connectionString, CommandType cmdType, string cmdText, params OracleParameter[] commandParameters)
  33. {
  34. // Create a new Oracle command
  35. OracleCommand cmd = new OracleCommand();
  36. //Create a connection
  37. using (OracleConnection connection = new OracleConnection(connectionString))
  38. {
  39. //Prepare the command
  40. PrepareCommand(cmd, connection, null, cmdType, cmdText, commandParameters);
  41. //Execute the command
  42. int val = cmd.ExecuteNonQuery();
  43. cmd.Parameters.Clear();
  44. return val;
  45. }
  46. }
  47. /// <summary>
  48. /// Execute an OracleCommand (that returns no resultset) against an existing database transaction
  49. /// using the provided parameters.
  50. /// </summary>
  51. /// <remarks>
  52. /// e.g.:
  53. /// int result = ExecuteNonQuery(trans, CommandType.StoredProcedure, "PublishOrders", new OracleParameter(":prodid", 24));
  54. /// </remarks>
  55. /// <param name="trans">an existing database transaction</param>
  56. /// <param name="commandType">the CommandType (stored procedure, text, etc.)</param>
  57. /// <param name="commandText">the stored procedure name or PL/SQL command</param>
  58. /// <param name="commandParameters">an array of OracleParamters used to execute the command</param>
  59. /// <returns>an int representing the number of rows affected by the command</returns>
  60. public static int ExecuteNonQuery(OracleTransaction trans, CommandType cmdType, string cmdText, params OracleParameter[] commandParameters)
  61. {
  62. OracleCommand cmd = new OracleCommand();
  63. PrepareCommand(cmd, trans.Connection, trans, cmdType, cmdText, commandParameters);
  64. int val = cmd.ExecuteNonQuery();
  65. cmd.Parameters.Clear();
  66. return val;
  67. }
  68. /// <summary>
  69. /// Execute an OracleCommand (that returns no resultset) against an existing database connection
  70. /// using the provided parameters.
  71. /// </summary>
  72. /// <remarks>
  73. /// e.g.:
  74. /// int result = ExecuteNonQuery(connString, CommandType.StoredProcedure, "PublishOrders", new OracleParameter(":prodid", 24));
  75. /// </remarks>
  76. /// <param name="conn">an existing database connection</param>
  77. /// <param name="commandType">the CommandType (stored procedure, text, etc.)</param>
  78. /// <param name="commandText">the stored procedure name or PL/SQL command</param>
  79. /// <param name="commandParameters">an array of OracleParamters used to execute the command</param>
  80. /// <returns>an int representing the number of rows affected by the command</returns>
  81. public static int ExecuteNonQuery(OracleConnection connection, CommandType cmdType, string cmdText, params OracleParameter[] commandParameters)
  82. {
  83. OracleCommand cmd = new OracleCommand();
  84. PrepareCommand(cmd, connection, null, cmdType, cmdText, commandParameters);
  85. int val = cmd.ExecuteNonQuery();
  86. cmd.Parameters.Clear();
  87. return val;
  88. }
  89. /// <summary>
  90. /// Execute a select query that will return a result set
  91. /// </summary>
  92. /// <param name="connString">Connection string</param>
  93. //// <param name="commandType">the CommandType (stored procedure, text, etc.)</param>
  94. /// <param name="commandText">the stored procedure name or PL/SQL command</param>
  95. /// <param name="commandParameters">an array of OracleParamters used to execute the command</param>
  96. /// <returns></returns>
  97. public static OracleDataReader ExecuteReader(string connectionString, CommandType cmdType, string cmdText, params OracleParameter[] commandParameters)
  98. {
  99. //Create the command and connection
  100. OracleCommand cmd = new OracleCommand();
  101. OracleConnection conn = new OracleConnection(connectionString);
  102. try
  103. {
  104. //Prepare the command to execute
  105. PrepareCommand(cmd, conn, null, cmdType, cmdText, commandParameters);
  106. //Execute the query, stating that the connection should close when the resulting datareader has been read
  107. OracleDataReader rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
  108. cmd.Parameters.Clear();
  109. return rdr;
  110. }
  111. catch
  112. {
  113. //If an error occurs close the connection as the reader will not be used and we expect it to close the connection
  114. conn.Close();
  115. throw;
  116. }
  117. }
  118. /// <summary>
  119. /// Execute an OracleCommand that returns the first column of the first record against the database specified in the connection string
  120. /// using the provided parameters.
  121. /// </summary>
  122. /// <remarks>
  123. /// e.g.:
  124. /// Object obj = ExecuteScalar(connString, CommandType.StoredProcedure, "PublishOrders", new OracleParameter(":prodid", 24));
  125. /// </remarks>
  126. /// <param name="connectionString">a valid connection string for a OracleConnection</param>
  127. /// <param name="commandType">the CommandType (stored procedure, text, etc.)</param>
  128. /// <param name="commandText">the stored procedure name or PL/SQL command</param>
  129. /// <param name="commandParameters">an array of OracleParamters used to execute the command</param>
  130. /// <returns>An object that should be converted to the expected type using Convert.To{Type}</returns>
  131. public static object ExecuteScalar(string connectionString, CommandType cmdType, string cmdText, params OracleParameter[] commandParameters)
  132. {
  133. OracleCommand cmd = new OracleCommand();
  134. using (OracleConnection conn = new OracleConnection(connectionString))
  135. {
  136. PrepareCommand(cmd, conn, null, cmdType, cmdText, commandParameters);
  137. object val = cmd.ExecuteScalar();
  138. cmd.Parameters.Clear();
  139. return val;
  140. }
  141. }
  142. /// <summary>
  143. /// Execute a OracleCommand (that returns a 1x1 resultset) against the specified OracleTransaction
  144. /// using the provided parameters.
  145. /// </summary>
  146. /// <param name="transaction">A valid OracleTransaction</param>
  147. /// <param name="commandType">The CommandType (stored procedure, text, etc.)</param>
  148. /// <param name="commandText">The stored procedure name or PL/SQL command</param>
  149. /// <param name="commandParameters">An array of OracleParamters used to execute the command</param>
  150. /// <returns>An object containing the value in the 1x1 resultset generated by the command</returns>
  151. public static object ExecuteScalar(OracleTransaction transaction, CommandType commandType, string commandText, params OracleParameter[] commandParameters)
  152. {
  153. if (transaction == null)
  154. throw new ArgumentNullException("transaction");
  155. if (transaction != null && transaction.Connection == null)
  156. throw new ArgumentException("The transaction was rollbacked or commited, please provide an open transaction.", "transaction");
  157. // Create a command and prepare it for execution
  158. OracleCommand cmd = new OracleCommand();
  159. PrepareCommand(cmd, transaction.Connection, transaction, commandType, commandText, commandParameters);
  160. // Execute the command & return the results
  161. object retval = cmd.ExecuteScalar();
  162. // Detach the OracleParameters from the command object, so they can be used again
  163. cmd.Parameters.Clear();
  164. return retval;
  165. }
  166. /// <summary>
  167. /// Execute an OracleCommand that returns the first column of the first record against an existing database connection
  168. /// using the provided parameters.
  169. /// </summary>
  170. /// <remarks>
  171. /// e.g.:
  172. /// Object obj = ExecuteScalar(conn, CommandType.StoredProcedure, "PublishOrders", new OracleParameter(":prodid", 24));
  173. /// </remarks>
  174. /// <param name="conn">an existing database connection</param>
  175. /// <param name="commandType">the CommandType (stored procedure, text, etc.)</param>
  176. /// <param name="commandText">the stored procedure name or PL/SQL command</param>
  177. /// <param name="commandParameters">an array of OracleParamters used to execute the command</param>
  178. /// <returns>An object that should be converted to the expected type using Convert.To{Type}</returns>
  179. public static object ExecuteScalar(OracleConnection connectionString, CommandType cmdType, string cmdText, params OracleParameter[] commandParameters)
  180. {
  181. OracleCommand cmd = new OracleCommand();
  182. PrepareCommand(cmd, connectionString, null, cmdType, cmdText, commandParameters);
  183. object val = cmd.ExecuteScalar();
  184. cmd.Parameters.Clear();
  185. return val;
  186. }
  187. /// <summary>
  188. /// Add a set of parameters to the cached
  189. /// </summary>
  190. /// <param name="cacheKey">Key value to look up the parameters</param>
  191. /// <param name="commandParameters">Actual parameters to cached</param>
  192. public static void CacheParameters(string cacheKey, params OracleParameter[] commandParameters)
  193. {
  194. parmCache[cacheKey] = commandParameters;
  195. }
  196. /// <summary>
  197. /// Fetch parameters from the cache
  198. /// </summary>
  199. /// <param name="cacheKey">Key to look up the parameters</param>
  200. /// <returns></returns>
  201. public static OracleParameter[] GetCachedParameters(string cacheKey)
  202. {
  203. OracleParameter[] cachedParms = (OracleParameter[])parmCache[cacheKey];
  204. if (cachedParms == null)
  205. return null;
  206. // If the parameters are in the cache
  207. OracleParameter[] clonedParms = new OracleParameter[cachedParms.Length];
  208. // return a copy of the parameters
  209. for (int i = 0, j = cachedParms.Length; i < j; i++)
  210. clonedParms[i] = (OracleParameter)((ICloneable)cachedParms[i]).Clone();
  211. return clonedParms;
  212. }
  213. /// <summary>
  214. /// Internal function to prepare a command for execution by the database
  215. /// </summary>
  216. /// <param name="cmd">Existing command object</param>
  217. /// <param name="conn">Database connection object</param>
  218. /// <param name="trans">Optional transaction object</param>
  219. /// <param name="cmdType">Command type, e.g. stored procedure</param>
  220. /// <param name="cmdText">Command test</param>
  221. /// <param name="commandParameters">Parameters for the command</param>
  222. private static void PrepareCommand(OracleCommand cmd, OracleConnection conn, OracleTransaction trans, CommandType cmdType, string cmdText, OracleParameter[] commandParameters)
  223. {
  224. //Open the connection if required
  225. if (conn.State != ConnectionState.Open)
  226. conn.Open();
  227. //Set up the command
  228. cmd.Connection = conn;
  229. cmd.CommandText = cmdText;
  230. cmd.CommandType = cmdType;
  231. //Bind it to the transaction if it exists
  232. if (trans != null)
  233. cmd.Transaction = trans;
  234. // Bind the parameters passed in
  235. if (commandParameters != null)
  236. {
  237. foreach (OracleParameter parm in commandParameters)
  238. cmd.Parameters.Add(parm);
  239. }
  240. }
  241. /// <summary>
  242. /// Converter to use boolean data type with Oracle
  243. /// </summary>
  244. /// <param name="value">Value to convert</param>
  245. /// <returns></returns>
  246. public static string OraBit(bool value)
  247. {
  248. if (value)
  249. return "Y";
  250. else
  251. return "N";
  252. }
  253. /// <summary>
  254. /// Converter to use boolean data type with Oracle
  255. /// </summary>
  256. /// <param name="value">Value to convert</param>
  257. /// <returns></returns>
  258. public static bool OraBool(string value)
  259. {
  260. if (value.Equals("Y"))
  261. return true;
  262. else
  263. return false;
  264. }
  265. #region ExecuteDataSet
  266. /// <summary>
  267. /// Execute a OracleCommand (that returns a resultset and takes no parameters) against the database specified in
  268. /// the connection string.
  269. /// </summary>
  270. /// <remarks>
  271. /// e.g.:
  272. /// DataSet ds = ExecuteDataset(connString, CommandType.StoredProcedure, "GetOrders");
  273. /// </remarks>
  274. /// <param name="connectionString">a valid connection string for a OracleConnection</param>
  275. /// <param name="commandType">the CommandType (stored procedure, text, etc.)</param>
  276. /// <param name="commandText">the stored procedure name or T-SQL command</param>
  277. /// <returns>a dataset containing the resultset generated by the command</returns>
  278. public static DataSet ExecuteDataset(string connectionString, CommandType commandType, string commandText)
  279. {
  280. //pass through the call providing null for the set of OracleParameters
  281. return ExecuteDataset(connectionString, commandType, commandText, (OracleParameter[])null);
  282. }
  283. /// <summary>
  284. /// Execute a OracleCommand (that returns a resultset) against the database specified in the connection string
  285. /// using the provided parameters.
  286. /// </summary>
  287. /// <remarks>
  288. /// e.g.:
  289. /// DataSet ds = ExecuteDataset(connString, CommandType.StoredProcedure, "GetOrders", new OracleParameter("@prodid", 24));
  290. /// </remarks>
  291. /// <param name="connectionString">a valid connection string for a OracleConnection</param>
  292. /// <param name="commandType">the CommandType (stored procedure, text, etc.)</param>
  293. /// <param name="commandText">the stored procedure name or T-SQL command</param>
  294. /// <param name="commandParameters">an array of SqlParamters used to execute the command</param>
  295. /// <returns>a dataset containing the resultset generated by the command</returns>
  296. public static DataSet ExecuteDataset(string connectionString, CommandType commandType, string commandText, params OracleParameter[] commandParameters)
  297. {
  298. //create & open a OracleConnection, and dispose of it after we are done.
  299. using (OracleConnection cn = new OracleConnection(connectionString))
  300. {
  301. cn.Open();
  302. //call the overload that takes a connection in place of the connection string
  303. DataSet ds = ExecuteDataset(cn, commandType, commandText, commandParameters);
  304. cn.Close();
  305. cn.Dispose();
  306. return ds;
  307. }
  308. }
  309. /// <summary>
  310. /// Execute a OracleCommand (that returns a resultset and takes no parameters) against the provided OracleConnection.
  311. /// </summary>
  312. /// <remarks>
  313. /// e.g.:
  314. /// DataSet ds = ExecuteDataset(conn, CommandType.StoredProcedure, "GetOrders");
  315. /// </remarks>
  316. /// <param name="connection">a valid OracleConnection</param>
  317. /// <param name="commandType">the CommandType (stored procedure, text, etc.)</param>
  318. /// <param name="commandText">the stored procedure name or T-SQL command</param>
  319. /// <returns>a dataset containing the resultset generated by the command</returns>
  320. public static DataSet ExecuteDataset(OracleConnection connection, CommandType commandType, string commandText)
  321. {
  322. //pass through the call providing null for the set of OracleParameters
  323. return ExecuteDataset(connection, commandType, commandText, (OracleParameter[])null);
  324. }
  325. /// <summary>
  326. /// Execute a OracleCommand (that returns a resultset) against the specified OracleConnection
  327. /// using the provided parameters.
  328. /// </summary>
  329. /// <remarks>
  330. /// e.g.:
  331. /// DataSet ds = ExecuteDataset(conn, CommandType.StoredProcedure, "GetOrders", new OracleParameter("@prodid", 24));
  332. /// </remarks>
  333. /// <param name="connection">a valid OracleConnection</param>
  334. /// <param name="commandType">the CommandType (stored procedure, text, etc.)</param>
  335. /// <param name="commandText">the stored procedure name or T-SQL command</param>
  336. /// <param name="commandParameters">an array of SqlParamters used to execute the command</param>
  337. /// <returns>a dataset containing the resultset generated by the command</returns>
  338. public static DataSet ExecuteDataset(OracleConnection connection, CommandType commandType, string commandText, params OracleParameter[] commandParameters)
  339. {
  340. //create a command and prepare it for execution
  341. OracleCommand cmd = new OracleCommand();
  342. PrepareCommand(cmd, connection, (OracleTransaction)null, commandType, commandText, commandParameters);
  343. //create the DataAdapter & DataSet
  344. OracleDataAdapter da = new OracleDataAdapter(cmd);
  345. DataSet ds = new DataSet();
  346. //fill the DataSet using default values for DataTable names, etc.
  347. da.Fill(ds);
  348. // detach the OracleParameters from the command object, so they can be used again.
  349. cmd.Parameters.Clear();
  350. connection.Close();
  351. connection.Dispose();
  352. //return the dataset
  353. return ds;
  354. }
  355. /// <summary>
  356. /// Execute a OracleCommand (that returns a resultset and takes no parameters) against the provided OracleTransaction.
  357. /// </summary>
  358. /// <remarks>
  359. /// e.g.:
  360. /// DataSet ds = ExecuteDataset(trans, CommandType.StoredProcedure, "GetOrders");
  361. /// </remarks>
  362. /// <param name="transaction">a valid OracleTransaction</param>
  363. /// <param name="commandType">the CommandType (stored procedure, text, etc.)</param>
  364. /// <param name="commandText">the stored procedure name or T-SQL command</param>
  365. /// <returns>a dataset containing the resultset generated by the command</returns>
  366. public static DataSet ExecuteDataset(OracleTransaction transaction, CommandType commandType, string commandText)
  367. {
  368. //pass through the call providing null for the set of OracleParameters
  369. return ExecuteDataset(transaction, commandType, commandText, (OracleParameter[])null);
  370. }
  371. /// <summary>
  372. /// Execute a OracleCommand (that returns a resultset) against the specified OracleTransaction
  373. /// using the provided parameters.
  374. /// </summary>
  375. /// <remarks>
  376. /// e.g.:
  377. /// DataSet ds = ExecuteDataset(trans, CommandType.StoredProcedure, "GetOrders", new OracleParameter("@prodid", 24));
  378. /// </remarks>
  379. /// <param name="transaction">a valid OracleTransaction</param>
  380. /// <param name="commandType">the CommandType (stored procedure, text, etc.)</param>
  381. /// <param name="commandText">the stored procedure name or T-SQL command</param>
  382. /// <param name="commandParameters">an array of SqlParamters used to execute the command</param>
  383. /// <returns>a dataset containing the resultset generated by the command</returns>
  384. public static DataSet ExecuteDataset(OracleTransaction transaction, CommandType commandType, string commandText, params OracleParameter[] commandParameters)
  385. {
  386. //create a command and prepare it for execution
  387. OracleCommand cmd = new OracleCommand();
  388. PrepareCommand(cmd, transaction.Connection, transaction, commandType, commandText, commandParameters);
  389. //create the DataAdapter & DataSet
  390. OracleDataAdapter da = new OracleDataAdapter(cmd);
  391. DataSet ds = new DataSet();
  392. //fill the DataSet using default values for DataTable names, etc.
  393. da.Fill(ds);
  394. // detach the OracleParameters from the command object, so they can be used again.
  395. cmd.Parameters.Clear();
  396. //return the dataset
  397. return ds;
  398. }
  399. #endregion ExecuteDataSet
  400. #region ExecuteDataTable
  401. /// <summary>
  402. /// Execute a OracleCommand (that returns a resultset and takes no parameters) against the database specified in
  403. /// the connection string.
  404. /// </summary>
  405. /// <remarks>
  406. /// e.g.:
  407. /// DataTable dt = ExecuteDataTable(connString, CommandType.StoredProcedure, "GetOrders");
  408. /// </remarks>
  409. /// <param name="connectionString">a valid connection string for a OracleConnection</param>
  410. /// <param name="commandType">the CommandType (stored procedure, text, etc.)</param>
  411. /// <param name="commandText">the stored procedure name or T-SQL command</param>
  412. /// <returns>a DataTable containing the resultset generated by the command</returns>
  413. public static DataTable ExecuteDataTable(string connectionString, CommandType commandType, string commandText)
  414. {
  415. //pass through the call providing null for the set of OracleParameters
  416. return ExecuteDataTable(connectionString, commandType, commandText, (OracleParameter[])null);
  417. }
  418. /// <summary>
  419. /// Execute a OracleCommand (that returns a resultset) against the database specified in the connection string
  420. /// using the provided parameters.
  421. /// </summary>
  422. /// <remarks>
  423. /// e.g.:
  424. /// DataTable dt = ExecuteDataTable(connString, CommandType.StoredProcedure, "GetOrders", new OracleParameter("@prodid", 24));
  425. /// </remarks>
  426. /// <param name="connectionString">a valid connection string for a OracleConnection</param>
  427. /// <param name="commandType">the CommandType (stored procedure, text, etc.)</param>
  428. /// <param name="commandText">the stored procedure name or T-SQL command</param>
  429. /// <param name="commandParameters">an array of SqlParamters used to execute the command</param>
  430. /// <returns>a DataTable containing the resultset generated by the command</returns>
  431. public static DataTable ExecuteDataTable(string connectionString, CommandType commandType, string commandText, params OracleParameter[] commandParameters)
  432. {
  433. //create & open a OracleConnection, and dispose of it after we are done.
  434. using (OracleConnection cn = new OracleConnection(connectionString))
  435. {
  436. cn.Open();
  437. //call the overload that takes a connection in place of the connection string
  438. DataTable dt = ExecuteDataTable(cn, commandType, commandText, commandParameters);
  439. cn.Close();
  440. cn.Dispose();
  441. return dt;
  442. }
  443. }
  444. /// <summary>
  445. /// Execute a OracleCommand (that returns a resultset and takes no parameters) against the provided OracleConnection.
  446. /// </summary>
  447. /// <remarks>
  448. /// e.g.:
  449. /// DataTable dt = ExecuteDataTable(conn, CommandType.StoredProcedure, "GetOrders");
  450. /// </remarks>
  451. /// <param name="connection">a valid OracleConnection</param>
  452. /// <param name="commandType">the CommandType (stored procedure, text, etc.)</param>
  453. /// <param name="commandText">the stored procedure name or T-SQL command</param>
  454. /// <returns>a DataTable containing the resultset generated by the command</returns>
  455. public static DataTable ExecuteDataTable(OracleConnection connection, CommandType commandType, string commandText)
  456. {
  457. //pass through the call providing null for the set of OracleParameters
  458. return ExecuteDataTable(connection, commandType, commandText, (OracleParameter[])null);
  459. }
  460. /// <summary>
  461. /// Execute a OracleCommand (that returns a resultset) against the specified OracleConnection
  462. /// using the provided parameters.
  463. /// </summary>
  464. /// <remarks>
  465. /// e.g.:
  466. /// DataTable dt = ExecuteDataTable(conn, CommandType.StoredProcedure, "GetOrders", new OracleParameter("@prodid", 24));
  467. /// </remarks>
  468. /// <param name="connection">a valid OracleConnection</param>
  469. /// <param name="commandType">the CommandType (stored procedure, text, etc.)</param>
  470. /// <param name="commandText">the stored procedure name or T-SQL command</param>
  471. /// <param name="commandParameters">an array of SqlParamters used to execute the command</param>
  472. /// <returns>a DataTable containing the resultset generated by the command</returns>
  473. public static DataTable ExecuteDataTable(OracleConnection connection, CommandType commandType, string commandText, params OracleParameter[] commandParameters)
  474. {
  475. //create a command and prepare it for execution
  476. OracleCommand cmd = new OracleCommand();
  477. PrepareCommand(cmd, connection, (OracleTransaction)null, commandType, commandText, commandParameters);
  478. //create the DataAdapter & DataTable
  479. OracleDataAdapter da = new OracleDataAdapter(cmd);
  480. DataTable dt = new DataTable();
  481. //fill the DataTable using default values for DataTable names, etc.
  482. da.Fill(dt);
  483. // detach the OracleParameters from the command object, so they can be used again.
  484. cmd.Parameters.Clear();
  485. connection.Close();
  486. connection.Dispose();
  487. //return the DataTable
  488. return dt;
  489. }
  490. /// <summary>
  491. /// Execute a OracleCommand (that returns a resultset and takes no parameters) against the provided OracleTransaction.
  492. /// </summary>
  493. /// <remarks>
  494. /// e.g.:
  495. /// DataTable dt = ExecuteDataTable(trans, CommandType.StoredProcedure, "GetOrders");
  496. /// </remarks>
  497. /// <param name="transaction">a valid OracleTransaction</param>
  498. /// <param name="commandType">the CommandType (stored procedure, text, etc.)</param>
  499. /// <param name="commandText">the stored procedure name or T-SQL command</param>
  500. /// <returns>a DataTable containing the resultset generated by the command</returns>
  501. public static DataTable ExecuteDataTable(OracleTransaction transaction, CommandType commandType, string commandText)
  502. {
  503. //pass through the call providing null for the set of OracleParameters
  504. return ExecuteDataTable(transaction, commandType, commandText, (OracleParameter[])null);
  505. }
  506. /// <summary>
  507. /// Execute a OracleCommand (that returns a resultset) against the specified OracleTransaction
  508. /// using the provided parameters.
  509. /// </summary>
  510. /// <remarks>
  511. /// e.g.:
  512. /// DataTable dt = ExecuteDataTable(trans, CommandType.StoredProcedure, "GetOrders", new OracleParameter("@prodid", 24));
  513. /// </remarks>
  514. /// <param name="transaction">a valid OracleTransaction</param>
  515. /// <param name="commandType">the CommandType (stored procedure, text, etc.)</param>
  516. /// <param name="commandText">the stored procedure name or T-SQL command</param>
  517. /// <param name="commandParameters">an array of SqlParamters used to execute the command</param>
  518. /// <returns>a DataTable containing the resultset generated by the command</returns>
  519. public static DataTable ExecuteDataTable(OracleTransaction transaction, CommandType commandType, string commandText, params OracleParameter[] commandParameters)
  520. {
  521. //create a command and prepare it for execution
  522. OracleCommand cmd = new OracleCommand();
  523. PrepareCommand(cmd, transaction.Connection, transaction, commandType, commandText, commandParameters);
  524. //create the DataAdapter & DataTable
  525. OracleDataAdapter da = new OracleDataAdapter(cmd);
  526. DataTable dt = new DataTable();
  527. //fill the DataTable using default values for DataTable names, etc.
  528. da.Fill(dt);
  529. // detach the OracleParameters from the command object, so they can be used again.
  530. cmd.Parameters.Clear();
  531. //return the DataTable
  532. return dt;
  533. }
  534. #endregion ExecuteDataTable
  535. }
  536. }