纽威
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

681 lines
28 KiB

1 year ago
  1. using System;
  2. using System.Configuration;
  3. using System.Data;
  4. using System.Collections;
  5. using System.Collections.Generic;
  6. using System.Text;
  7. using System.Data.Common;
  8. using Oracle.ManagedDataAccess.Client;
  9. namespace NFine.Data
  10. {
  11. /// <summary>
  12. /// A helper class used to execute queries against an Oracle database
  13. /// </summary>
  14. public abstract class OracleHelper
  15. {
  16. public static string ConnectionString = ConfigurationManager.ConnectionStrings["Oracleconnstr"].ConnectionString;
  17. private static void SetConnByString(string conn)
  18. {
  19. ConnectionString = ConfigurationManager.ConnectionStrings[conn].ConnectionString;
  20. }
  21. // Read the connection strings from the configuration file
  22. //public static readonly string ConnectionStringLocalTransaction = ConfigurationManager.AppSettings["OraConnString1"];
  23. //public static readonly string ConnectionStringInventoryDistributedTransaction = ConfigurationManager.AppSettings["OraConnString2"];
  24. //public static readonly string ConnectionStringOrderDistributedTransaction = ConfigurationManager.AppSettings["OraConnString3"];
  25. //public static readonly string ConnectionStringProfile = ConfigurationManager.AppSettings["OraProfileConnString"];
  26. //public static readonly string ConnectionStringMembership = ConfigurationManager.AppSettings["OraMembershipConnString"];
  27. //Create a hashtable for the parameter cached
  28. private static Hashtable parmCache = Hashtable.Synchronized(new Hashtable());
  29. /// <summary>
  30. /// Execute a database query which does not include a select
  31. /// </summary>
  32. /// <param name="connString">Connection string to database</param>
  33. /// <param name="cmdType">Command type either stored procedure or SQL</param>
  34. /// <param name="cmdText">Acutall SQL Command</param>
  35. /// <param name="commandParameters">Parameters to bind to the command</param>
  36. /// <returns></returns>
  37. public static int ExecuteNonQuery(string connectionString, CommandType cmdType, string cmdText, params OracleParameter[] commandParameters)
  38. {
  39. // Create a new Oracle command
  40. OracleCommand cmd = new OracleCommand();
  41. //Create a connection
  42. using (OracleConnection connection = new OracleConnection(connectionString))
  43. {
  44. //Prepare the command
  45. PrepareCommand(cmd, connection, null, cmdType, cmdText, commandParameters);
  46. //Execute the command
  47. int val = cmd.ExecuteNonQuery();
  48. connection.Close();
  49. cmd.Parameters.Clear();
  50. return val;
  51. }
  52. }
  53. /// <summary>
  54. /// 执行查询语句,返回DataSet
  55. /// </summary>
  56. /// <param name="SQLString">查询语句</param>
  57. /// <returns>DataSet</returns>
  58. public static DataSet Queryold(string connectionString, string SQLString)
  59. {
  60. using (OracleConnection connection = new OracleConnection(connectionString))
  61. {
  62. DataSet ds = new DataSet();
  63. try
  64. {
  65. connection.Open();
  66. OracleDataAdapter command = new OracleDataAdapter(SQLString, connection);
  67. command.Fill(ds, "ds");
  68. }
  69. catch (OracleException ex)
  70. {
  71. throw new Exception(ex.Message);
  72. }
  73. finally
  74. {
  75. if (connection.State != ConnectionState.Closed)
  76. {
  77. connection.Close();
  78. }
  79. }
  80. return ds;
  81. }
  82. }
  83. public static DataSet Query(string connectionString, string SQLString)
  84. {
  85. DataSet ds = new DataSet();
  86. try
  87. {
  88. string connstr = connectionString;
  89. OracleConnection conn = new OracleConnection(connectionString);
  90. if (conn.State != ConnectionState.Open)
  91. {
  92. conn.Open();
  93. }
  94. OracleDataAdapter adp = new OracleDataAdapter(SQLString, conn);
  95. adp.Fill(ds);
  96. conn.Close();
  97. }
  98. catch (OracleException ex)
  99. {
  100. throw new Exception(ex.ToString());
  101. }
  102. return ds;
  103. }
  104. public static DataTable GetPageTable_EXISTSTemp(string sql, string sqlTempName, string Droptable, string connectstr, DbParameter[] param, string orderField, string orderType, int pageIndex, int pageSize, ref int count)
  105. {
  106. SetConnByString(connectstr);
  107. //StringBuilder strSql = new StringBuilder();
  108. if (pageIndex == 0)
  109. {
  110. pageIndex = 1;
  111. }
  112. int num = (pageIndex - 1) * pageSize;
  113. int num1 = (pageIndex) * pageSize;
  114. string OrderBy = "";
  115. if (!string.IsNullOrEmpty(orderField))
  116. OrderBy = "Order By " + orderField + " " + orderType + "";
  117. DataSet ds = new DataSet();
  118. string SQLCOUNT = " SELECT COUNT(1) FROM " + sqlTempName + " ";
  119. string WhereStr = " 1=1 AND ROWNUM>="+num+ " AND ROWNUM>=" + num1 + " ";
  120. sql.Replace("1=1", WhereStr);
  121. sql += OrderBy;
  122. sql= sql + SQLCOUNT + Droptable;
  123. ds = Query(ConnectionString, sql);
  124. count = Convert.ToInt32(ds.Tables[1].Rows[0][0].ToString());
  125. return ds.Tables[0];
  126. }
  127. public static DataTable GetTable(string sql, string connectstr, DbParameter[] param )
  128. {
  129. SetConnByString(connectstr);
  130. StringBuilder strSql = new StringBuilder();
  131. DataSet ds = new DataSet();
  132. ds = Query(ConnectionString, sql);
  133. return ds.Tables[0];
  134. }
  135. public static DataSet QueryByConStr(string connectstr, string SQLString, params OracleParameter[] cmdParms)
  136. {
  137. SetConnByString(connectstr);
  138. using (OracleConnection connection = new OracleConnection(ConnectionString))
  139. {
  140. OracleCommand cmd = new OracleCommand();
  141. PrepareCommand(cmd, connection, null, SQLString, cmdParms);
  142. using (OracleDataAdapter da = new OracleDataAdapter(cmd))
  143. {
  144. DataSet ds = new DataSet();
  145. try
  146. {
  147. da.Fill(ds, "ds");
  148. cmd.Parameters.Clear();
  149. }
  150. catch (OracleException ex)
  151. {
  152. throw new Exception(ex.Message);
  153. }
  154. finally
  155. {
  156. if (connection.State != ConnectionState.Closed)
  157. {
  158. connection.Close();
  159. }
  160. }
  161. return ds;
  162. }
  163. }
  164. }
  165. public static DataSet Query(string connectionString, string SQLString, params OracleParameter[] cmdParms)
  166. {
  167. using (OracleConnection connection = new OracleConnection(connectionString))
  168. {
  169. OracleCommand cmd = new OracleCommand();
  170. PrepareCommand(cmd, connection, null, SQLString, cmdParms);
  171. using (OracleDataAdapter da = new OracleDataAdapter(cmd))
  172. {
  173. DataSet ds = new DataSet();
  174. try
  175. {
  176. da.Fill(ds, "ds");
  177. cmd.Parameters.Clear();
  178. }
  179. catch (OracleException ex)
  180. {
  181. throw new Exception(ex.Message);
  182. }
  183. finally
  184. {
  185. if (connection.State != ConnectionState.Closed)
  186. {
  187. connection.Close();
  188. }
  189. }
  190. return ds;
  191. }
  192. }
  193. }
  194. private static void PrepareCommand(OracleCommand cmd, OracleConnection conn, OracleTransaction trans, string cmdText, OracleParameter[] cmdParms)
  195. {
  196. if (conn.State != ConnectionState.Open)
  197. conn.Open();
  198. cmd.Connection = conn;
  199. cmd.CommandText = cmdText;
  200. if (trans != null)
  201. cmd.Transaction = trans;
  202. cmd.CommandType = CommandType.Text;//cmdType;
  203. if (cmdParms != null)
  204. {
  205. foreach (OracleParameter parameter in cmdParms)
  206. {
  207. if ((parameter.Direction == ParameterDirection.InputOutput || parameter.Direction == ParameterDirection.Input) &&
  208. (parameter.Value == null))
  209. {
  210. parameter.Value = DBNull.Value;
  211. }
  212. cmd.Parameters.Add(parameter);
  213. }
  214. }
  215. }
  216. /// <summary>
  217. /// 执行一条计算查询结果语句,返回查询结果(object)。
  218. /// </summary>
  219. /// <param name="SQLString">计算查询结果语句</param>
  220. /// <returns>查询结果(object)</returns>
  221. public static object GetSingle(string connectionString, string SQLString)
  222. {
  223. using (OracleConnection connection = new OracleConnection(connectionString))
  224. {
  225. using (OracleCommand cmd = new OracleCommand(SQLString, connection))
  226. {
  227. try
  228. {
  229. connection.Open();
  230. object obj = cmd.ExecuteScalar();
  231. if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
  232. {
  233. return null;
  234. }
  235. else
  236. {
  237. return obj;
  238. }
  239. }
  240. catch (OracleException ex)
  241. {
  242. throw new Exception(ex.Message);
  243. }
  244. finally
  245. {
  246. if (connection.State != ConnectionState.Closed)
  247. {
  248. connection.Close();
  249. }
  250. }
  251. }
  252. }
  253. }
  254. public static bool Exists(string connectionString, string strOracle)
  255. {
  256. object obj = OracleHelper.GetSingle(connectionString, strOracle);
  257. int cmdresult;
  258. if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
  259. {
  260. cmdresult = 0;
  261. }
  262. else
  263. {
  264. cmdresult = int.Parse(obj.ToString());
  265. }
  266. if (cmdresult == 0)
  267. {
  268. return false;
  269. }
  270. else
  271. {
  272. return true;
  273. }
  274. }
  275. /// <summary>
  276. /// Execute an OracleCommand (that returns no resultset) against an existing database transaction
  277. /// using the provided parameters.
  278. /// </summary>
  279. /// <remarks>
  280. /// e.g.:
  281. /// int result = ExecuteNonQuery(trans, CommandType.StoredProcedure, "PublishOrders", new OracleParameter(":prodid", 24));
  282. /// </remarks>
  283. /// <param name="trans">an existing database transaction</param>
  284. /// <param name="commandType">the CommandType (stored procedure, text, etc.)</param>
  285. /// <param name="commandText">the stored procedure name or PL/SQL command</param>
  286. /// <param name="commandParameters">an array of OracleParamters used to execute the command</param>
  287. /// <returns>an int representing the number of rows affected by the command</returns>
  288. public static int ExecuteNonQuery(OracleTransaction trans, CommandType cmdType, string cmdText, params OracleParameter[] commandParameters)
  289. {
  290. OracleCommand cmd = new OracleCommand();
  291. PrepareCommand(cmd, trans.Connection, trans, cmdType, cmdText, commandParameters);
  292. int val = cmd.ExecuteNonQuery();
  293. cmd.Parameters.Clear();
  294. return val;
  295. }
  296. /// <summary>
  297. /// Execute an OracleCommand (that returns no resultset) against an existing database connection
  298. /// using the provided parameters.
  299. /// </summary>
  300. /// <remarks>
  301. /// e.g.:
  302. /// int result = ExecuteNonQuery(connString, CommandType.StoredProcedure, "PublishOrders", new OracleParameter(":prodid", 24));
  303. /// </remarks>
  304. /// <param name="conn">an existing database connection</param>
  305. /// <param name="commandType">the CommandType (stored procedure, text, etc.)</param>
  306. /// <param name="commandText">the stored procedure name or PL/SQL command</param>
  307. /// <param name="commandParameters">an array of OracleParamters used to execute the command</param>
  308. /// <returns>an int representing the number of rows affected by the command</returns>
  309. public static int ExecuteNonQuery(OracleConnection connection, CommandType cmdType, string cmdText, params OracleParameter[] commandParameters)
  310. {
  311. OracleCommand cmd = new OracleCommand();
  312. PrepareCommand(cmd, connection, null, cmdType, cmdText, commandParameters);
  313. int val = cmd.ExecuteNonQuery();
  314. cmd.Parameters.Clear();
  315. return val;
  316. }
  317. /// <summary>
  318. /// Execute an OracleCommand (that returns no resultset) against an existing database connection
  319. /// using the provided parameters.
  320. /// </summary>
  321. /// <remarks>
  322. /// e.g.:
  323. /// int result = ExecuteNonQuery(connString, CommandType.StoredProcedure, "PublishOrders", new OracleParameter(":prodid", 24));
  324. /// </remarks>
  325. /// <param name="conn">an existing database connection</param>
  326. /// <param name="commandText">the stored procedure name or PL/SQL command</param>
  327. /// <returns>an int representing the number of rows affected by the command</returns>
  328. public static int ExecuteNonQuery(string connectionString, string cmdText)
  329. {
  330. OracleCommand cmd = new OracleCommand();
  331. OracleConnection connection = new OracleConnection(connectionString);
  332. PrepareCommand(cmd, connection, null, CommandType.Text, cmdText, null);
  333. int val = cmd.ExecuteNonQuery();
  334. cmd.Parameters.Clear();
  335. return val;
  336. }
  337. /// <summary>
  338. /// Execute a select query that will return a result set
  339. /// </summary>
  340. /// <param name="connString">Connection string</param>
  341. //// <param name="commandType">the CommandType (stored procedure, text, etc.)</param>
  342. /// <param name="commandText">the stored procedure name or PL/SQL command</param>
  343. /// <param name="commandParameters">an array of OracleParamters used to execute the command</param>
  344. /// <returns></returns>
  345. public static OracleDataReader ExecuteReader(string connectionString, CommandType cmdType, string cmdText, params OracleParameter[] commandParameters)
  346. {
  347. OracleCommand cmd = new OracleCommand();
  348. OracleConnection conn = new OracleConnection(connectionString);
  349. try
  350. {
  351. //Prepare the command to execute
  352. PrepareCommand(cmd, conn, null, cmdType, cmdText, commandParameters);
  353. OracleDataReader rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
  354. cmd.Parameters.Clear();
  355. return rdr;
  356. }
  357. catch
  358. {
  359. conn.Close();
  360. throw;
  361. }
  362. }
  363. /// <summary>
  364. /// Execute an OracleCommand that returns the first column of the first record against the database specified in the connection string
  365. /// using the provided parameters.
  366. /// </summary>
  367. /// <remarks>
  368. /// e.g.:
  369. /// Object obj = ExecuteScalar(connString, CommandType.StoredProcedure, "PublishOrders", new OracleParameter(":prodid", 24));
  370. /// </remarks>
  371. /// <param name="connectionString">a valid connection string for a SqlConnection</param>
  372. /// <param name="commandType">the CommandType (stored procedure, text, etc.)</param>
  373. /// <param name="commandText">the stored procedure name or PL/SQL command</param>
  374. /// <param name="commandParameters">an array of OracleParamters used to execute the command</param>
  375. /// <returns>An object that should be converted to the expected type using Convert.To{Type}</returns>
  376. public static object ExecuteScalar(string connectionString, CommandType cmdType, string cmdText, params OracleParameter[] commandParameters)
  377. {
  378. OracleCommand cmd = new OracleCommand();
  379. using (OracleConnection conn = new OracleConnection(connectionString))
  380. {
  381. PrepareCommand(cmd, conn, null, cmdType, cmdText, commandParameters);
  382. object val = cmd.ExecuteScalar();
  383. cmd.Parameters.Clear();
  384. return val;
  385. }
  386. }
  387. /// <summary>
  388. /// Execute a OracleCommand (that returns a 1x1 resultset) against the specified SqlTransaction
  389. /// using the provided parameters.
  390. /// </summary>
  391. /// <param name="transaction">A valid SqlTransaction</param>
  392. /// <param name="commandType">The CommandType (stored procedure, text, etc.)</param>
  393. /// <param name="commandText">The stored procedure name or PL/SQL command</param>
  394. /// <param name="commandParameters">An array of OracleParamters used to execute the command</param>
  395. /// <returns>An object containing the value in the 1x1 resultset generated by the command</returns>
  396. public static object ExecuteScalar(OracleTransaction transaction, CommandType commandType, string commandText, params OracleParameter[] commandParameters)
  397. {
  398. if (transaction == null)
  399. throw new ArgumentNullException("transaction");
  400. if (transaction != null && transaction.Connection == null)
  401. throw new ArgumentException("The transaction was rollbacked or commited, please provide an open transaction.", "transaction");
  402. // Create a command and prepare it for execution
  403. OracleCommand cmd = new OracleCommand();
  404. PrepareCommand(cmd, transaction.Connection, transaction, commandType, commandText, commandParameters);
  405. // Execute the command & return the results
  406. object retval = cmd.ExecuteScalar();
  407. // Detach the SqlParameters from the command object, so they can be used again
  408. cmd.Parameters.Clear();
  409. return retval;
  410. }
  411. /// <summary>
  412. /// Execute an OracleCommand that returns the first column of the first record against an existing database connection
  413. /// using the provided parameters.
  414. /// </summary>
  415. /// <remarks>
  416. /// e.g.:
  417. /// Object obj = ExecuteScalar(conn, CommandType.StoredProcedure, "PublishOrders", new OracleParameter(":prodid", 24));
  418. /// </remarks>
  419. /// <param name="conn">an existing database connection</param>
  420. /// <param name="commandType">the CommandType (stored procedure, text, etc.)</param>
  421. /// <param name="commandText">the stored procedure name or PL/SQL command</param>
  422. /// <param name="commandParameters">an array of OracleParamters used to execute the command</param>
  423. /// <returns>An object that should be converted to the expected type using Convert.To{Type}</returns>
  424. public static object ExecuteScalar(OracleConnection connectionString, CommandType cmdType, string cmdText, params OracleParameter[] commandParameters)
  425. {
  426. OracleCommand cmd = new OracleCommand();
  427. PrepareCommand(cmd, connectionString, null, cmdType, cmdText, commandParameters);
  428. object val = cmd.ExecuteScalar();
  429. cmd.Parameters.Clear();
  430. return val;
  431. }
  432. /// <summary>
  433. /// Add a set of parameters to the cached
  434. /// </summary>
  435. /// <param name="cacheKey">Key value to look up the parameters</param>
  436. /// <param name="commandParameters">Actual parameters to cached</param>
  437. public static void CacheParameters(string cacheKey, params OracleParameter[] commandParameters)
  438. {
  439. parmCache[cacheKey] = commandParameters;
  440. }
  441. /// <summary>
  442. /// Fetch parameters from the cache
  443. /// </summary>
  444. /// <param name="cacheKey">Key to look up the parameters</param>
  445. /// <returns></returns>
  446. public static OracleParameter[] GetCachedParameters(string cacheKey)
  447. {
  448. OracleParameter[] cachedParms = (OracleParameter[])parmCache[cacheKey];
  449. if (cachedParms == null)
  450. return null;
  451. // If the parameters are in the cache
  452. OracleParameter[] clonedParms = new OracleParameter[cachedParms.Length];
  453. // return a copy of the parameters
  454. for (int i = 0, j = cachedParms.Length; i < j; i++)
  455. clonedParms[i] = (OracleParameter)((ICloneable)cachedParms[i]).Clone();
  456. return clonedParms;
  457. }
  458. /// <summary>
  459. /// Internal function to prepare a command for execution by the database
  460. /// </summary>
  461. /// <param name="cmd">Existing command object</param>
  462. /// <param name="conn">Database connection object</param>
  463. /// <param name="trans">Optional transaction object</param>
  464. /// <param name="cmdType">Command type, e.g. stored procedure</param>
  465. /// <param name="cmdText">Command test</param>
  466. /// <param name="commandParameters">Parameters for the command</param>
  467. private static void PrepareCommand(OracleCommand cmd, OracleConnection conn, OracleTransaction trans, CommandType cmdType, string cmdText, OracleParameter[] commandParameters)
  468. {
  469. //Open the connection if required
  470. if (conn.State != ConnectionState.Open)
  471. conn.Open();
  472. //Set up the command
  473. cmd.Connection = conn;
  474. cmd.CommandText = cmdText;
  475. cmd.CommandType = cmdType;
  476. //Bind it to the transaction if it exists
  477. if (trans != null)
  478. cmd.Transaction = trans;
  479. // Bind the parameters passed in
  480. if (commandParameters != null)
  481. {
  482. foreach (OracleParameter parm in commandParameters)
  483. cmd.Parameters.Add(parm);
  484. }
  485. }
  486. /// <summary>
  487. /// Converter to use boolean data type with Oracle
  488. /// </summary>
  489. /// <param name="value">Value to convert</param>
  490. /// <returns></returns>
  491. public static string OraBit(bool value)
  492. {
  493. if (value)
  494. return "Y";
  495. else
  496. return "N";
  497. }
  498. /// <summary>
  499. /// Converter to use boolean data type with Oracle
  500. /// </summary>
  501. /// <param name="value">Value to convert</param>
  502. /// <returns></returns>
  503. public static bool OraBool(string value)
  504. {
  505. if (value.Equals("Y"))
  506. return true;
  507. else
  508. return false;
  509. }
  510. /// <summary>
  511. /// 执行多条SQL语句,实现数据库事务。
  512. /// </summary>
  513. /// <param name="SQLStringList">多条SQL语句</param>
  514. public static bool ExecuteSqlTran(string conStr, List<CommandInfo> cmdList)
  515. {
  516. using (OracleConnection conn = new OracleConnection(conStr))
  517. {
  518. conn.Open();
  519. OracleCommand cmd = new OracleCommand();
  520. cmd.Connection = conn;
  521. OracleTransaction tx = conn.BeginTransaction();
  522. cmd.Transaction = tx;
  523. try
  524. {
  525. foreach (CommandInfo c in cmdList)
  526. {
  527. if (!String.IsNullOrEmpty(c.CommandText))
  528. {
  529. PrepareCommand(cmd, conn, tx, CommandType.Text, c.CommandText, (OracleParameter[])c.Parameters);
  530. if (c.EffentNextType == EffentNextType.WhenHaveContine || c.EffentNextType == EffentNextType.WhenNoHaveContine)
  531. {
  532. if (c.CommandText.ToLower().IndexOf("count(") == -1)
  533. {
  534. tx.Rollback();
  535. throw new Exception("Oracle:违背要求" + c.CommandText + "必须符合select count(..的格式");
  536. //return false;
  537. }
  538. object obj = cmd.ExecuteScalar();
  539. bool isHave = false;
  540. if (obj == null && obj == DBNull.Value)
  541. {
  542. isHave = false;
  543. }
  544. isHave = Convert.ToInt32(obj) > 0;
  545. if (c.EffentNextType == EffentNextType.WhenHaveContine && !isHave)
  546. {
  547. tx.Rollback();
  548. throw new Exception("Oracle:违背要求" + c.CommandText + "返回值必须大于0");
  549. //return false;
  550. }
  551. if (c.EffentNextType == EffentNextType.WhenNoHaveContine && isHave)
  552. {
  553. tx.Rollback();
  554. throw new Exception("Oracle:违背要求" + c.CommandText + "返回值必须等于0");
  555. //eturn false;
  556. }
  557. continue;
  558. }
  559. int res = cmd.ExecuteNonQuery();
  560. if (c.EffentNextType == EffentNextType.ExcuteEffectRows && res == 0)
  561. {
  562. tx.Rollback();
  563. throw new Exception("Oracle:违背要求" + c.CommandText + "必须有影像行");
  564. // return false;
  565. }
  566. }
  567. }
  568. tx.Commit();
  569. return true;
  570. }
  571. catch (OracleException E)
  572. {
  573. tx.Rollback();
  574. throw E;
  575. }
  576. finally
  577. {
  578. if (conn.State != ConnectionState.Closed)
  579. {
  580. conn.Close();
  581. }
  582. }
  583. }
  584. }
  585. /// <summary>
  586. /// 执行多条SQL语句,实现数据库事务。
  587. /// </summary>
  588. /// <param name="SQLStringList">多条SQL语句</param>
  589. public static void ExecuteSqlTran(string conStr, List<String> SQLStringList)
  590. {
  591. using (OracleConnection conn = new OracleConnection(conStr))
  592. {
  593. conn.Open();
  594. OracleCommand cmd = new OracleCommand();
  595. cmd.Connection = conn;
  596. OracleTransaction tx = conn.BeginTransaction();
  597. cmd.Transaction = tx;
  598. try
  599. {
  600. foreach (string sql in SQLStringList)
  601. {
  602. if (!String.IsNullOrEmpty(sql))
  603. {
  604. cmd.CommandText = sql;
  605. cmd.ExecuteNonQuery();
  606. }
  607. }
  608. tx.Commit();
  609. }
  610. catch (OracleException E)
  611. {
  612. tx.Rollback();
  613. throw new Exception(E.Message);
  614. }
  615. finally
  616. {
  617. if (conn.State != ConnectionState.Closed)
  618. {
  619. conn.Close();
  620. }
  621. }
  622. }
  623. }
  624. }
  625. }