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.

753 lines
35 KiB

2 years ago
  1. using EasyNet.Common;
  2. using System;
  3. using System.Collections;
  4. using System.Data;
  5. using System.Text;
  6. namespace EasyNet.DBUtility
  7. {
  8. public class AdoHelper
  9. {
  10. //獲取資料庫類型
  11. private static string strDbType = CommonUtils.GetConfigValueByKey("dbType").ToUpper();
  12. //將資料庫類型轉換成枚舉類型
  13. public static DatabaseType DbType = DatabaseTypeEnumParse<DatabaseType>(strDbType);
  14. public static string DbHost = CommonUtils.GetConfigValueByKey("DbHost");
  15. public static string DbPort = CommonUtils.GetConfigValueByKey("DbPort");
  16. public static string DbName = CommonUtils.GetConfigValueByKey("DbName");
  17. public static string DbUser = CommonUtils.GetConfigValueByKey("DbUser");
  18. public static string DbPassword = CommonUtils.GetConfigValueByKey("DbPassword");
  19. public static string DbMinPoolSize = CommonUtils.GetConfigValueByKey("DbMinPoolSize");
  20. public static string DbMaxPoolSize = CommonUtils.GetConfigValueByKey("DbMaxPoolSize");
  21. public static string DbCharset = CommonUtils.GetConfigValueByKey("DbCharset");
  22. //獲取資料庫連接字串
  23. public static string ConnectionString = GetConnectionString("ConnectionString");
  24. //獲取資料庫具名引數符號,比如@(SQLSERVER)、:(ORACLE)
  25. public static string DbParmChar = DbFactory.CreateDbParmCharacter();
  26. private static Hashtable parmCache = Hashtable.Synchronized(new Hashtable());
  27. /// <summary>
  28. ///通過提供的參數,執行無結果集的資料庫操作命令
  29. /// 並返回執行資料庫操作所影響的行數。
  30. /// </summary>
  31. /// <param name="connectionString">資料庫連接字串</param>
  32. /// <param name="commandParameters">執行命令所需的參數陣列</param>
  33. /// <param name="cmdType">todo: describe cmdType parameter on ExecuteNonQuery</param>
  34. /// <param name="cmdText">todo: describe cmdText parameter on ExecuteNonQuery</param>
  35. /// <returns>返回通過執行命令所影響的行數</returns>
  36. public static int ExecuteNonQuery(string connectionString, CommandType cmdType, string cmdText, params IDbDataParameter[] commandParameters)
  37. {
  38. var cmd = DbFactory.CreateDbCommand();
  39. using (IDbConnection conn = DbFactory.CreateDbConnection(connectionString))
  40. {
  41. PrepareCommand(cmd, conn, null, cmdType, cmdText, commandParameters);
  42. var val = cmd.ExecuteNonQuery();
  43. cmd.Parameters.Clear();
  44. return val;
  45. }
  46. }
  47. /// <summary>
  48. ///通過提供的參數,執行無結果集的資料庫操作命令
  49. /// 並返回執行資料庫操作所影響的行數。
  50. /// </summary>
  51. /// <param name="connectionString">資料庫連接字串</param>
  52. /// <param name="cmdType">todo: describe cmdType parameter on ExecuteNonQuery</param>
  53. /// <param name="cmdText">todo: describe cmdText parameter on ExecuteNonQuery</param>
  54. /// <returns>返回通過執行命令所影響的行數</returns>
  55. public static int ExecuteNonQuery(string connectionString, CommandType cmdType, string cmdText)
  56. {
  57. var cmd = DbFactory.CreateDbCommand();
  58. using (IDbConnection conn = DbFactory.CreateDbConnection(connectionString))
  59. {
  60. PrepareCommand(cmd, conn, null, cmdType, cmdText, null);
  61. var val = cmd.ExecuteNonQuery();
  62. cmd.Parameters.Clear();
  63. return val;
  64. }
  65. }
  66. /// <summary>
  67. ///通過提供的參數,執行無結果集返回的資料庫操作命令
  68. ///並返回執行資料庫操作所影響的行數。
  69. /// </summary>
  70. /// <param name="commandParameters">執行命令所需的參數陣列</param>
  71. /// <param name="connection">todo: describe connection parameter on ExecuteNonQuery</param>
  72. /// <param name="cmdType">todo: describe cmdType parameter on ExecuteNonQuery</param>
  73. /// <param name="cmdText">todo: describe cmdText parameter on ExecuteNonQuery</param>
  74. /// <remarks>
  75. /// e.g.:
  76. /// int result = ExecuteNonQuery(connString, CommandType.StoredProcedure, "PublishOrders", new SqlParameter("@prodid", 24));
  77. /// </remarks>
  78. /// <returns>返回通過執行命令所影響的行數</returns>
  79. public static int ExecuteNonQuery(IDbConnection connection, CommandType cmdType, string cmdText, params IDbDataParameter[] commandParameters)
  80. {
  81. var cmd = DbFactory.CreateDbCommand();
  82. PrepareCommand(cmd, connection, null, cmdType, cmdText, commandParameters);
  83. var val = cmd.ExecuteNonQuery();
  84. cmd.Parameters.Clear();
  85. return val;
  86. }
  87. /// <summary>
  88. ///通過提供的參數,執行無結果集返回的資料庫操作命令
  89. ///並返回執行資料庫操作所影響的行數。
  90. /// </summary>
  91. /// <param name="connection">todo: describe connection parameter on ExecuteNonQuery</param>
  92. /// <param name="cmdType">todo: describe cmdType parameter on ExecuteNonQuery</param>
  93. /// <param name="cmdText">todo: describe cmdText parameter on ExecuteNonQuery</param>
  94. /// <remarks>
  95. /// e.g.:
  96. /// int result = ExecuteNonQuery(connString, CommandType.StoredProcedure, "PublishOrders", new SqlParameter("@prodid", 24));
  97. /// </remarks>
  98. /// <returns>返回通過執行命令所影響的行數</returns>
  99. public static int ExecuteNonQuery(IDbConnection connection, CommandType cmdType, string cmdText)
  100. {
  101. var cmd = DbFactory.CreateDbCommand();
  102. PrepareCommand(cmd, connection, null, cmdType, cmdText, null);
  103. var val = cmd.ExecuteNonQuery();
  104. cmd.Parameters.Clear();
  105. return val;
  106. }
  107. /// <summary>
  108. ///通過提供的參數,執行無結果集返回的資料庫操作命令
  109. ///並返回執行資料庫操作所影響的行數。
  110. /// </summary>
  111. /// <param name="trans">sql事務物件</param>
  112. /// <param name="commandParameters">執行命令所需的參數陣列</param>
  113. /// <param name="cmdType">todo: describe cmdType parameter on ExecuteNonQuery</param>
  114. /// <param name="cmdText">todo: describe cmdText parameter on ExecuteNonQuery</param>
  115. /// <remarks>
  116. /// e.g.:
  117. /// int result = ExecuteNonQuery(connString, CommandType.StoredProcedure, "PublishOrders", new SqlParameter("@prodid", 24));
  118. /// </remarks>
  119. /// <returns>返回通過執行命令所影響的行數</returns>
  120. public static int ExecuteNonQuery(IDbTransaction trans, CommandType cmdType, string cmdText, params IDbDataParameter[] commandParameters)
  121. {
  122. var val = 0;
  123. var cmd = DbFactory.CreateDbCommand();
  124. if (trans == null || trans.Connection == null)
  125. {
  126. using (IDbConnection conn = DbFactory.CreateDbConnection(AdoHelper.ConnectionString))
  127. {
  128. PrepareCommand(cmd, conn, trans, cmdType, cmdText, commandParameters);
  129. val = cmd.ExecuteNonQuery();
  130. }
  131. }
  132. else
  133. {
  134. PrepareCommand(cmd, trans.Connection, trans, cmdType, cmdText, commandParameters);
  135. val = cmd.ExecuteNonQuery();
  136. }
  137. cmd.Parameters.Clear();
  138. return val;
  139. }
  140. /// <summary>
  141. ///通過提供的參數,執行無結果集返回的資料庫操作命令
  142. ///並返回執行資料庫操作所影響的行數。
  143. /// </summary>
  144. /// <param name="trans">sql事務物件</param>
  145. /// <param name="cmdType">todo: describe cmdType parameter on ExecuteNonQuery</param>
  146. /// <param name="cmdText">todo: describe cmdText parameter on ExecuteNonQuery</param>
  147. /// <remarks>
  148. /// e.g.:
  149. /// int result = ExecuteNonQuery(connString, CommandType.StoredProcedure, "PublishOrders", new SqlParameter("@prodid", 24));
  150. /// </remarks>
  151. /// <returns>返回通過執行命令所影響的行數</returns>
  152. public static int ExecuteNonQuery(IDbTransaction trans, CommandType cmdType, string cmdText)
  153. {
  154. var cmd = DbFactory.CreateDbCommand();
  155. PrepareCommand(cmd, trans.Connection, trans, cmdType, cmdText, null);
  156. var val = cmd.ExecuteNonQuery();
  157. cmd.Parameters.Clear();
  158. return val;
  159. }
  160. /// <summary>
  161. /// 使用提供的參數,執行有結果集返回的資料庫操作命令 並返回SqlDataReader對象
  162. /// </summary>
  163. /// <param name="connectionString">資料庫連接字串</param>
  164. /// <param name="commandParameters">執行命令所需的參數陣列</param>
  165. /// <param name="cmdType">todo: describe cmdType parameter on ExecuteReader</param>
  166. /// <param name="cmdText">todo: describe cmdText parameter on ExecuteReader</param>
  167. /// <remarks>
  168. /// e.g.: SqlDataReader r = ExecuteReader(connString, CommandType.StoredProcedure,
  169. /// "PublishOrders", new SqlParameter("@prodid", 24));
  170. /// </remarks>
  171. /// <returns>返回SqlDataReader對象</returns>
  172. public static IDataReader ExecuteReader(string connectionString, CommandType cmdType, string cmdText, params IDbDataParameter[] commandParameters)
  173. {
  174. var cmd = DbFactory.CreateDbCommand();
  175. var conn = DbFactory.CreateDbConnection(connectionString);
  176. //我們在這裡使用一個 try/catch,因為如果PrepareCommand方法拋出一個異常,我們想在捕獲代碼裡面關閉
  177. //connection連線物件,因為異常發生datareader將不會存在,所以commandBehaviour.CloseConnection
  178. //將不會執行。
  179. try
  180. {
  181. PrepareCommand(cmd, conn, null, cmdType, cmdText, commandParameters);
  182. var rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
  183. cmd.Parameters.Clear();
  184. return rdr;
  185. }
  186. catch
  187. {
  188. conn.Close();
  189. cmd.Dispose();
  190. throw;
  191. }
  192. }
  193. /// <summary>
  194. /// 使用提供的參數,執行有結果集返回的資料庫操作命令 並返回SqlDataReader對象
  195. /// </summary>
  196. /// <param name="commandParameters">執行命令所需的參數陣列</param>
  197. /// <param name="trans">todo: describe trans parameter on ExecuteReader</param>
  198. /// <param name="cmdType">todo: describe cmdType parameter on ExecuteReader</param>
  199. /// <param name="cmdText">todo: describe cmdText parameter on ExecuteReader</param>
  200. /// <remarks>
  201. /// e.g.: SqlDataReader r = ExecuteReader(connString, CommandType.StoredProcedure,
  202. /// "PublishOrders", new SqlParameter("@prodid", 24));
  203. /// </remarks>
  204. /// <returns>返回SqlDataReader對象</returns>
  205. public static IDataReader ExecuteReader(IDbTransaction trans, CommandType cmdType, string cmdText, params IDbDataParameter[] commandParameters)
  206. {
  207. var cmd = DbFactory.CreateDbCommand();
  208. var conn = trans.Connection;
  209. //我們在這裡使用一個 try/catch,因為如果PrepareCommand方法拋出一個異常,我們想在捕獲代碼裡面關閉
  210. //connection連線物件,因為異常發生datareader將不會存在,所以commandBehaviour.CloseConnection
  211. //將不會執行。
  212. try
  213. {
  214. PrepareCommand(cmd, conn, null, cmdType, cmdText, commandParameters);
  215. var rdr = cmd.ExecuteReader();
  216. cmd.Parameters.Clear();
  217. return rdr;
  218. }
  219. catch
  220. {
  221. conn.Close();
  222. cmd.Dispose();
  223. throw;
  224. }
  225. }
  226. /// <summary>
  227. /// 使用提供的參數,執行有結果集返回的資料庫操作命令 並返回SqlDataReader對象
  228. /// </summary>
  229. /// <param name="commandParameters">執行命令所需的參數陣列</param>
  230. /// <param name="closeConnection">todo: describe closeConnection parameter on ExecuteReader</param>
  231. /// <param name="connection">todo: describe connection parameter on ExecuteReader</param>
  232. /// <param name="cmdType">todo: describe cmdType parameter on ExecuteReader</param>
  233. /// <param name="cmdText">todo: describe cmdText parameter on ExecuteReader</param>
  234. /// <remarks>
  235. /// e.g.: SqlDataReader r = ExecuteReader(connString, CommandType.StoredProcedure,
  236. /// "PublishOrders", new SqlParameter("@prodid", 24));
  237. /// </remarks>
  238. /// <returns>返回SqlDataReader對象</returns>
  239. public static IDataReader ExecuteReader(bool closeConnection, IDbConnection connection, CommandType cmdType, string cmdText, params IDbDataParameter[] commandParameters)
  240. {
  241. var cmd = DbFactory.CreateDbCommand();
  242. var conn = connection;
  243. //我們在這裡使用一個 try/catch,因為如果PrepareCommand方法拋出一個異常,我們想在捕獲代碼裡面關閉
  244. //connection連線物件,因為異常發生datareader將不會存在,所以commandBehaviour.CloseConnection
  245. //將不會執行。
  246. try
  247. {
  248. PrepareCommand(cmd, conn, null, cmdType, cmdText, commandParameters);
  249. var rdr = closeConnection ? cmd.ExecuteReader(CommandBehavior.CloseConnection) : cmd.ExecuteReader();
  250. cmd.Parameters.Clear();
  251. return rdr;
  252. }
  253. catch
  254. {
  255. conn.Close();
  256. cmd.Dispose();
  257. throw;
  258. }
  259. }
  260. /// <summary>
  261. ///使用提供的參數,執行有結果集返回的資料庫操作命令
  262. /// 並返回SqlDataReader對象
  263. /// </summary>
  264. /// <param name="connectionString">資料庫連接字串</param>
  265. /// <param name="cmdType">todo: describe cmdType parameter on ExecuteReader</param>
  266. /// <param name="cmdText">todo: describe cmdText parameter on ExecuteReader</param>
  267. /// <returns>返回SqlDataReader對象</returns>
  268. public static IDataReader ExecuteReader(string connectionString, CommandType cmdType, string cmdText)
  269. {
  270. var cmd = DbFactory.CreateDbCommand();
  271. var conn = DbFactory.CreateDbConnection(connectionString);
  272. //我們在這裡使用一個 try/catch,因為如果PrepareCommand方法拋出一個異常,我們想在捕獲代碼裡面關閉
  273. //connection連線物件,因為異常發生datareader將不會存在,所以commandBehaviour.CloseConnection
  274. //將不會執行。
  275. try
  276. {
  277. PrepareCommand(cmd, conn, null, cmdType, cmdText, null);
  278. var rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
  279. cmd.Parameters.Clear();
  280. return rdr;
  281. }
  282. catch (Exception ex)
  283. {
  284. conn.Close();
  285. cmd.Dispose();
  286. throw new Exception("some reason to rethrow", ex);
  287. }
  288. }
  289. /// <summary>
  290. ///使用提供的參數,執行有結果集返回的資料庫操作命令
  291. /// 並返回SqlDataReader對象
  292. /// </summary>
  293. /// <param name="trans">todo: describe trans parameter on ExecuteReader</param>
  294. /// <param name="cmdType">todo: describe cmdType parameter on ExecuteReader</param>
  295. /// <param name="cmdText">todo: describe cmdText parameter on ExecuteReader</param>
  296. /// <returns>返回SqlDataReader對象</returns>
  297. public static IDataReader ExecuteReader(IDbTransaction trans, CommandType cmdType, string cmdText)
  298. {
  299. var cmd = DbFactory.CreateDbCommand();
  300. var conn = trans.Connection;
  301. //我們在這裡使用一個 try/catch,因為如果PrepareCommand方法拋出一個異常,我們想在捕獲代碼裡面關閉
  302. //connection連線物件,因為異常發生datareader將不會存在,所以commandBehaviour.CloseConnection
  303. //將不會執行。
  304. try
  305. {
  306. PrepareCommand(cmd, conn, null, cmdType, cmdText, null);
  307. var rdr = cmd.ExecuteReader();
  308. cmd.Parameters.Clear();
  309. return rdr;
  310. }
  311. catch (Exception ex)
  312. {
  313. conn.Close();
  314. cmd.Dispose();
  315. throw new Exception("some reason to rethrow", ex);
  316. }
  317. }
  318. /// <summary>
  319. ///使用提供的參數,執行有結果集返回的資料庫操作命令
  320. /// 並返回SqlDataReader對象
  321. /// </summary>
  322. /// <param name="closeConnection">todo: describe closeConnection parameter on ExecuteReader</param>
  323. /// <param name="connection">todo: describe connection parameter on ExecuteReader</param>
  324. /// <param name="cmdType">todo: describe cmdType parameter on ExecuteReader</param>
  325. /// <param name="cmdText">todo: describe cmdText parameter on ExecuteReader</param>
  326. /// <returns>返回SqlDataReader對象</returns>
  327. public static IDataReader ExecuteReader(bool closeConnection, IDbConnection connection, CommandType cmdType, string cmdText)
  328. {
  329. var cmd = DbFactory.CreateDbCommand();
  330. var conn = connection;
  331. //我們在這裡使用一個 try/catch,因為如果PrepareCommand方法拋出一個異常,我們想在捕獲代碼裡面關閉
  332. //connection連線物件,因為異常發生datareader將不會存在,所以commandBehaviour.CloseConnection
  333. //將不會執行。
  334. try
  335. {
  336. PrepareCommand(cmd, conn, null, cmdType, cmdText, null);
  337. var rdr = closeConnection ? cmd.ExecuteReader(CommandBehavior.CloseConnection) : cmd.ExecuteReader();
  338. cmd.Parameters.Clear();
  339. return rdr;
  340. }
  341. catch (Exception ex)
  342. {
  343. conn.Close();
  344. cmd.Dispose();
  345. throw new Exception("some reason to rethrow", ex);
  346. }
  347. }
  348. /// <summary>
  349. /// 查詢資料填充到資料集DataSet中
  350. /// </summary>
  351. /// <param name="connectionString">資料庫連接字串</param>
  352. /// <param name="cmdType">執行命令的類型(存儲過程或T-SQL,等等)</param>
  353. /// <param name="cmdText">命令文本</param>
  354. /// <param name="commandParameters">參數陣列</param>
  355. /// <returns>資料集DataSet對象</returns>
  356. public static DataSet DataSet(string connectionString, CommandType cmdType, string cmdText, params IDbDataParameter[] commandParameters)
  357. {
  358. var ds = new DataSet();
  359. var cmd = DbFactory.CreateDbCommand();
  360. var conn = DbFactory.CreateDbConnection(connectionString);
  361. try
  362. {
  363. PrepareCommand(cmd, conn, null, cmdType, cmdText, commandParameters);
  364. var sda = DbFactory.CreateDataAdapter(cmd);
  365. sda.Fill(ds);
  366. return ds;
  367. }
  368. catch
  369. {
  370. conn.Close();
  371. cmd.Dispose();
  372. throw;
  373. }
  374. finally
  375. {
  376. conn.Close();
  377. cmd.Dispose();
  378. }
  379. }
  380. /// <summary>
  381. /// 查詢資料填充到資料集DataSet中
  382. /// </summary>
  383. /// <param name="connectionString">資料庫連接字串</param>
  384. /// <param name="cmdType">執行命令的類型(存儲過程或T-SQL,等等)</param>
  385. /// <param name="cmdText">命令文本</param>
  386. /// <returns>資料集DataSet對象</returns>
  387. public static DataSet DataSet(string connectionString, CommandType cmdType, string cmdText)
  388. {
  389. var ds = new DataSet();
  390. var cmd = DbFactory.CreateDbCommand();
  391. var conn = DbFactory.CreateDbConnection(connectionString);
  392. try
  393. {
  394. PrepareCommand(cmd, conn, null, cmdType, cmdText, null);
  395. var sda = DbFactory.CreateDataAdapter(cmd);
  396. sda.Fill(ds);
  397. return ds;
  398. }
  399. catch
  400. {
  401. conn.Close();
  402. cmd.Dispose();
  403. throw;
  404. }
  405. finally
  406. {
  407. conn.Close();
  408. cmd.Dispose();
  409. }
  410. }
  411. /// <summary>
  412. /// 依靠資料庫連接字串connectionString, 使用所提供參數,執行返回首行首列命令
  413. /// </summary>
  414. /// <param name="connectionString">資料庫連接字串</param>
  415. /// <param name="commandParameters">執行命令所需的參數陣列</param>
  416. /// <param name="cmdType">todo: describe cmdType parameter on ExecuteScalar</param>
  417. /// <param name="cmdText">todo: describe cmdText parameter on ExecuteScalar</param>
  418. /// <remarks>
  419. /// e.g.: Object obj = ExecuteScalar(connString, CommandType.StoredProcedure,
  420. /// "PublishOrders", new SqlParameter("@prodid", 24));
  421. /// </remarks>
  422. /// <returns>返回一個物件,使用Convert.To{Type}將該對象轉換成想要的資料類型。</returns>
  423. public static object ExecuteScalar(string connectionString, CommandType cmdType, string cmdText, params IDbDataParameter[] commandParameters)
  424. {
  425. var cmd = DbFactory.CreateDbCommand();
  426. using (IDbConnection connection = DbFactory.CreateDbConnection(connectionString))
  427. {
  428. PrepareCommand(cmd, connection, null, cmdType, cmdText, commandParameters);
  429. var val = cmd.ExecuteScalar();
  430. cmd.Parameters.Clear();
  431. return val;
  432. }
  433. }
  434. /// <summary>
  435. /// 依靠資料庫連接字串connectionString, 使用所提供參數,執行返回首行首列命令
  436. /// </summary>
  437. /// <param name="connectionString">資料庫連接字串</param>
  438. /// <param name="cmdType">todo: describe cmdType parameter on ExecuteScalar</param>
  439. /// <param name="cmdText">todo: describe cmdText parameter on ExecuteScalar</param>
  440. /// <remarks>
  441. /// e.g.: Object obj = ExecuteScalar(connString, CommandType.StoredProcedure,
  442. /// "PublishOrders", new SqlParameter("@prodid", 24));
  443. /// </remarks>
  444. /// <returns>返回一個物件,使用Convert.To{Type}將該對象轉換成想要的資料類型。</returns>
  445. public static object ExecuteScalar(string connectionString, CommandType cmdType, string cmdText)
  446. {
  447. var cmd = DbFactory.CreateDbCommand();
  448. using (IDbConnection connection = DbFactory.CreateDbConnection(connectionString))
  449. {
  450. PrepareCommand(cmd, connection, null, cmdType, cmdText, null);
  451. var val = cmd.ExecuteScalar();
  452. cmd.Parameters.Clear();
  453. return val;
  454. }
  455. }
  456. /// <summary>
  457. ///依靠資料庫連接字串connectionString,
  458. /// 使用所提供參數,執行返回首行首列命令
  459. /// </summary>
  460. /// <param name="commandParameters">執行命令所需的參數陣列</param>
  461. /// <param name="connection">todo: describe connection parameter on ExecuteScalar</param>
  462. /// <param name="cmdType">todo: describe cmdType parameter on ExecuteScalar</param>
  463. /// <param name="cmdText">todo: describe cmdText parameter on ExecuteScalar</param>
  464. /// <remarks>
  465. /// e.g.:
  466. /// Object obj = ExecuteScalar(connString, CommandType.StoredProcedure, "PublishOrders", new SqlParameter("@prodid", 24));
  467. /// </remarks>
  468. /// <returns>返回一個物件,使用Convert.To{Type}將該對象轉換成想要的資料類型。</returns>
  469. public static object ExecuteScalar(IDbConnection connection, CommandType cmdType, string cmdText, params IDbDataParameter[] commandParameters)
  470. {
  471. var cmd = DbFactory.CreateDbCommand();
  472. PrepareCommand(cmd, connection, null, cmdType, cmdText, commandParameters);
  473. var val = cmd.ExecuteScalar();
  474. cmd.Parameters.Clear();
  475. return val;
  476. }
  477. /// <summary>
  478. ///依靠資料庫連接字串connectionString,
  479. /// 使用所提供參數,執行返回首行首列命令
  480. /// </summary>
  481. /// <param name="connection">todo: describe connection parameter on ExecuteScalar</param>
  482. /// <param name="cmdType">todo: describe cmdType parameter on ExecuteScalar</param>
  483. /// <param name="cmdText">todo: describe cmdText parameter on ExecuteScalar</param>
  484. /// <remarks>
  485. /// e.g.:
  486. /// Object obj = ExecuteScalar(connString, CommandType.StoredProcedure, "PublishOrders", new SqlParameter("@prodid", 24));
  487. /// </remarks>
  488. /// <returns>返回一個物件,使用Convert.To{Type}將該對象轉換成想要的資料類型。</returns>
  489. public static object ExecuteScalar(IDbConnection connection, CommandType cmdType, string cmdText)
  490. {
  491. var cmd = DbFactory.CreateDbCommand();
  492. PrepareCommand(cmd, connection, null, cmdType, cmdText, null);
  493. var val = cmd.ExecuteScalar();
  494. cmd.Parameters.Clear();
  495. return val;
  496. }
  497. /// <summary>
  498. ///依靠資料庫連接字串connectionString,
  499. /// 使用所提供參數,執行返回首行首列命令
  500. /// </summary>
  501. /// <param name="conn">資料庫連線物件</param>
  502. /// <param name="trans">todo: describe trans parameter on ExecuteScalar</param>
  503. /// <param name="cmdType">todo: describe cmdType parameter on ExecuteScalar</param>
  504. /// <param name="cmdText">todo: describe cmdText parameter on ExecuteScalar</param>
  505. /// <remarks>
  506. /// e.g.:
  507. /// Object obj = ExecuteScalar(connString, CommandType.StoredProcedure, "PublishOrders", new SqlParameter("@prodid", 24));
  508. /// </remarks>
  509. /// <returns>返回一個物件,使用Convert.To{Type}將該對象轉換成想要的資料類型。</returns>
  510. public static object ExecuteScalar(IDbConnection conn, IDbTransaction trans, CommandType cmdType, string cmdText)
  511. {
  512. var cmd = DbFactory.CreateDbCommand();
  513. PrepareCommand(cmd, conn, trans, cmdType, cmdText, null);
  514. var val = cmd.ExecuteScalar();
  515. cmd.Parameters.Clear();
  516. return val;
  517. }
  518. /// <summary>
  519. ///依靠資料庫連接字串connectionString,
  520. /// 使用所提供參數,執行返回首行首列命令
  521. /// </summary>
  522. /// <param name="commandParameters">執行命令所需的參數陣列</param>
  523. /// <param name="trans">todo: describe trans parameter on ExecuteScalar</param>
  524. /// <param name="cmdType">todo: describe cmdType parameter on ExecuteScalar</param>
  525. /// <param name="cmdText">todo: describe cmdText parameter on ExecuteScalar</param>
  526. /// <remarks>
  527. /// e.g.:
  528. /// Object obj = ExecuteScalar(connString, CommandType.StoredProcedure, "PublishOrders", new SqlParameter("@prodid", 24));
  529. /// </remarks>
  530. /// <returns>返回一個物件,使用Convert.To{Type}將該對象轉換成想要的資料類型。</returns>
  531. public static object ExecuteScalar(IDbTransaction trans, CommandType cmdType, string cmdText, params IDbDataParameter[] commandParameters)
  532. {
  533. var cmd = DbFactory.CreateDbCommand();
  534. PrepareCommand(cmd, trans.Connection, trans, cmdType, cmdText, commandParameters);
  535. var val = cmd.ExecuteScalar();
  536. cmd.Parameters.Clear();
  537. return val;
  538. }
  539. /// <summary>
  540. ///通過提供的參數,執行無結果集返回的資料庫操作命令
  541. ///並返回執行資料庫操作所影響的行數。
  542. /// </summary>
  543. /// <param name="trans">sql事務物件</param>
  544. /// <param name="commandParameters">執行命令所需的參數陣列</param>
  545. /// <param name="cmdType">todo: describe cmdType parameter on ExecuteQuery</param>
  546. /// <param name="cmdText">todo: describe cmdText parameter on ExecuteQuery</param>
  547. /// <remarks>
  548. /// e.g.:
  549. /// int result = ExecuteNonQuery(connString, CommandType.StoredProcedure, "PublishOrders", new SqlParameter("@prodid", 24));
  550. /// </remarks>
  551. /// <returns>返回通過執行命令所影響的行數</returns>
  552. public static object ExecuteQuery(IDbTransaction trans, CommandType cmdType, string cmdText, params IDbDataParameter[] commandParameters)
  553. {
  554. object val = null;
  555. var cmd = DbFactory.CreateDbCommand();
  556. if (trans == null || trans.Connection == null)
  557. {
  558. using (IDbConnection conn = DbFactory.CreateDbConnection(AdoHelper.ConnectionString))
  559. {
  560. PrepareCommand(cmd, conn, trans, cmdType, cmdText, commandParameters);
  561. val = cmd.ExecuteScalar();
  562. }
  563. }
  564. else
  565. {
  566. PrepareCommand(cmd, trans.Connection, trans, cmdType, cmdText, commandParameters);
  567. val = cmd.ExecuteScalar();
  568. }
  569. cmd.Parameters.Clear();
  570. return val;
  571. }
  572. /// <summary>
  573. /// add parameter array to the cache
  574. /// </summary>
  575. /// <param name="cacheKey">Key to the parameter cache</param>
  576. /// <param name="commandParameters">todo: describe commandParameters parameter on CacheParameters</param>
  577. public static void CacheParameters(string cacheKey, params IDbDataParameter[] commandParameters)
  578. {
  579. parmCache[cacheKey] = commandParameters;
  580. }
  581. /// <summary>
  582. /// 查詢緩存參數
  583. /// </summary>
  584. /// <param name="cacheKey">使用緩存名稱查找值</param>
  585. /// <returns>緩存參數陣列</returns>
  586. public static IDbDataParameter[] GetCachedParameters(string cacheKey)
  587. {
  588. var cachedParms = (IDbDataParameter[])parmCache[cacheKey];
  589. if (cachedParms == null)
  590. return null;
  591. var clonedParms = new IDbDataParameter[cachedParms.Length];
  592. for (int i = 0, j = cachedParms.Length; i < j; i++)
  593. clonedParms[i] = (IDbDataParameter)((ICloneable)cachedParms[i]).Clone();
  594. return clonedParms;
  595. }
  596. /// <summary>
  597. /// 為即將執行準備一個命令
  598. /// </summary>
  599. /// <param name="cmd">SqlCommand對象</param>
  600. /// <param name="conn">SqlConnection對象</param>
  601. /// <param name="trans">IDbTransaction對象</param>
  602. /// <param name="cmdType">執行命令的類型(存儲過程或T-SQL,等等)</param>
  603. /// <param name="cmdText">存儲過程名稱或者T-SQL命令列, e.g. Select * from Products</param>
  604. /// <param name="cmdParms">SqlParameters to use in the command</param>
  605. private static void PrepareCommand(IDbCommand cmd, IDbConnection conn, IDbTransaction trans, CommandType cmdType, string cmdText, IDbDataParameter[] cmdParms)
  606. {
  607. if (conn.State != ConnectionState.Open)
  608. conn.Open();
  609. cmd.Connection = conn;
  610. cmd.CommandText = cmdText;
  611. if (trans != null)
  612. cmd.Transaction = trans;
  613. cmd.CommandType = cmdType;
  614. if (cmdParms != null)
  615. {
  616. foreach (IDbDataParameter parm in cmdParms)
  617. cmd.Parameters.Add(parm);
  618. }
  619. }
  620. /// <summary>
  621. /// 根據傳入的Key獲取設定檔中 相應Key的資料庫連接字串
  622. /// </summary>
  623. /// <param name="Key"></param>
  624. /// <returns></returns>
  625. public static string GetConnectionString(string Key)
  626. {
  627. try
  628. {
  629. var connectionString = CommonUtils.GetConfigValueByKey(Key);
  630. if (!String.IsNullOrEmpty(connectionString)) return connectionString;
  631. var sDbHost = CommonUtils.GetConfigValueByKey(nameof(DbHost));
  632. var sDbPort = CommonUtils.GetConfigValueByKey(nameof(DbPort));
  633. var sDbName = CommonUtils.GetConfigValueByKey(nameof(DbName));
  634. var sDbUser = CommonUtils.GetConfigValueByKey(nameof(DbUser));
  635. var sDbPassword = CommonUtils.GetConfigValueByKey(nameof(DbPassword));
  636. var sDbMinPoolSize = CommonUtils.GetConfigValueByKey(nameof(DbMinPoolSize));
  637. var sDbMaxPoolSize = CommonUtils.GetConfigValueByKey(nameof(DbMaxPoolSize));
  638. var sDbCharset = CommonUtils.GetConfigValueByKey(nameof(DbCharset));
  639. var sb = new StringBuilder();
  640. sb.Append("Data Source=").Append(sDbHost).Append(";");
  641. if (!String.IsNullOrEmpty(sDbPort))
  642. {
  643. sb.Append("port=").Append(sDbPort).Append(";");
  644. }
  645. sb.Append("User ID=").Append(sDbUser).Append(";");
  646. sb.Append("Password=").Append(sDbPassword).Append(";");
  647. sb.Append("DataBase=").Append(sDbName).Append(";");
  648. if (!String.IsNullOrEmpty(sDbMinPoolSize))
  649. {
  650. sb.Append("Min Pool Size=").Append(sDbMinPoolSize).Append(";");
  651. }
  652. if (!String.IsNullOrEmpty(sDbMinPoolSize))
  653. {
  654. sb.Append("Max Pool Size=").Append(sDbMaxPoolSize).Append(";");
  655. }
  656. if (!String.IsNullOrEmpty(sDbCharset))
  657. {
  658. sb.Append("charset=").Append(sDbCharset).Append(";");
  659. }
  660. return sb.ToString();
  661. }
  662. catch
  663. {
  664. throw new Exception("web.config檔appSettings中資料庫連接字串未配置或配置錯誤,必須為Key=\"connectionString\"");
  665. }
  666. }
  667. /// <summary>
  668. /// 用於資料庫類型的字串枚舉轉換
  669. /// </summary>
  670. /// <typeparam name="T"></typeparam>
  671. /// <param name="value"></param>
  672. /// <returns></returns>
  673. public static T DatabaseTypeEnumParse<T>(string value)
  674. {
  675. try
  676. {
  677. return CommonUtils.EnumParse<T>(value);
  678. }
  679. catch
  680. {
  681. throw new Exception("資料庫類型\"" + value + "\"錯誤,請檢查!");
  682. }
  683. }
  684. }
  685. }