纽威
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.

630 lines
26 KiB

1 year ago
  1. using NFine.Code;
  2. using System;
  3. using System.Configuration;
  4. using System.Data;
  5. using System.Data.Common;
  6. using System.Data.SqlClient;
  7. namespace NFine.Data.Extensions
  8. {
  9. public class DbHelper
  10. {
  11. /// <summary>
  12. /// 调试日志
  13. /// </summary>
  14. //public static LogHelper log = LogFactory.GetLogger(typeof(DbHelper));
  15. /// <summary>
  16. /// 连接字符串
  17. /// </summary>
  18. public static string ConnectionString { get; set; }
  19. /// <summary>
  20. /// 数据库类型
  21. /// </summary>
  22. public static DatabaseType DbType { get; set; }
  23. /// <summary>
  24. /// 数据库命名参数符号
  25. /// </summary>
  26. public static string DbParmChar { get; set; }
  27. public DbHelper(string connstring)
  28. {
  29. string ConStringDESEncrypt = ConfigurationManager.ConnectionStrings["WMSconnstr"].ConnectionString;
  30. ConnectionString = ConfigurationManager.ConnectionStrings["WMSconnstr"].ConnectionString;
  31. if (ConStringDESEncrypt == "true")
  32. {
  33. ConnectionString = DESEncrypt.Decrypt(ConnectionString);
  34. }
  35. this.DatabaseTypeEnumParse(ConfigurationManager.ConnectionStrings["WMSconnstr"].ProviderName);
  36. DbParmChar = DbFactory.CreateDbParmCharacter();
  37. }
  38. /// <summary>
  39. /// 执行 SQL 语句,并返回受影响的行数。
  40. /// </summary>
  41. /// <param name="cmdType">执行命令的类型(存储过程或T-SQL,等等)</param>
  42. /// <param name="cmdText">存储过程名称或者T-SQL命令行</param>
  43. /// <param name="parameters">执行命令所需的sql语句对应参数</param>
  44. /// <returns></returns>
  45. public static int ExecuteNonQuery(CommandType cmdType, string cmdText, params DbParameter[] parameters)
  46. {
  47. int num = 0;
  48. try
  49. {
  50. DbCommand cmd = DbFactory.CreateDbCommand();
  51. using (DbConnection conn = DbFactory.CreateDbConnection(ConnectionString))
  52. {
  53. PrepareCommand(cmd, conn, null, cmdType, cmdText, parameters);
  54. num = cmd.ExecuteNonQuery();
  55. cmd.Parameters.Clear();
  56. }
  57. }
  58. catch (Exception ex)
  59. {
  60. num = -1;
  61. //log.Error(ex.Message);
  62. }
  63. return num;
  64. }
  65. /// <summary>
  66. /// 执行 SQL 语句,并返回受影响的行数。
  67. /// </summary>
  68. /// <param name="cmdType">执行命令的类型(存储过程或T-SQL,等等)</param>
  69. /// <param name="cmdText">存储过程名称或者T-SQL命令行</param>
  70. /// <returns></returns>
  71. public static int ExecuteNonQuery(CommandType cmdType, string cmdText)
  72. {
  73. int num = 0;
  74. try
  75. {
  76. DbCommand cmd = DbFactory.CreateDbCommand();
  77. using (DbConnection conn = DbFactory.CreateDbConnection(ConnectionString))
  78. {
  79. PrepareCommand(cmd, conn, null, cmdType, cmdText, null);
  80. num = cmd.ExecuteNonQuery();
  81. cmd.Parameters.Clear();
  82. }
  83. }
  84. catch (Exception ex)
  85. {
  86. num = -1;
  87. //log.Error(ex.Message);
  88. }
  89. return num;
  90. }
  91. /// <summary>
  92. /// 执行 SQL 语句,并返回受影响的行数。
  93. /// </summary>
  94. /// <param name="conn">数据库连接对象</param>
  95. /// <param name="cmdType">执行命令的类型(存储过程或T-SQL,等等)</param>
  96. /// <param name="cmdText">存储过程名称或者T-SQL命令行</param>
  97. /// <param name="parameters">执行命令所需的sql语句对应参数</param>
  98. /// <returns></returns>
  99. public static int ExecuteNonQuery(DbConnection connection, CommandType cmdType, string cmdText, params DbParameter[] parameters)
  100. {
  101. int num = 0;
  102. try
  103. {
  104. DbCommand cmd = DbFactory.CreateDbCommand();
  105. PrepareCommand(cmd, connection, null, cmdType, cmdText, parameters);
  106. num = cmd.ExecuteNonQuery();
  107. cmd.Parameters.Clear();
  108. }
  109. catch (Exception ex)
  110. {
  111. num = -1;
  112. //log.Error(ex.Message);
  113. }
  114. return num;
  115. }
  116. /// <summary>
  117. /// 执行 SQL 语句,并返回受影响的行数。
  118. /// </summary>
  119. /// <param name="conn">数据库连接对象</param>
  120. /// <param name="cmdType">执行命令的类型(存储过程或T-SQL,等等)</param>
  121. /// <param name="cmdText">存储过程名称或者T-SQL命令行</param>
  122. /// <returns></returns>
  123. public static int ExecuteNonQuery(DbConnection connection, CommandType cmdType, string cmdText)
  124. {
  125. int num = 0;
  126. try
  127. {
  128. DbCommand cmd = DbFactory.CreateDbCommand();
  129. PrepareCommand(cmd, connection, null, cmdType, cmdText, null);
  130. num = cmd.ExecuteNonQuery();
  131. cmd.Parameters.Clear();
  132. }
  133. catch (Exception ex)
  134. {
  135. num = -1;
  136. //log.Error(ex.Message);
  137. }
  138. return num;
  139. }
  140. /// <summary>
  141. /// 执行 SQL 语句,并返回受影响的行数。
  142. /// </summary>
  143. /// <param name="isOpenTrans">事务对象</param>
  144. /// <param name="cmdType">执行命令的类型(存储过程或T-SQL,等等)</param>
  145. /// <param name="cmdText">存储过程名称或者T-SQL命令行</param>
  146. /// <param name="parameters">执行命令所需的sql语句对应参数</param>
  147. /// <returns></returns>
  148. public static int ExecuteNonQuery(DbTransaction isOpenTrans, CommandType cmdType, string cmdText, params DbParameter[] parameters)
  149. {
  150. int num = 0;
  151. try
  152. {
  153. DbCommand cmd = DbFactory.CreateDbCommand();
  154. if (isOpenTrans == null || isOpenTrans.Connection == null)
  155. {
  156. using (DbConnection conn = DbFactory.CreateDbConnection(ConnectionString))
  157. {
  158. PrepareCommand(cmd, conn, isOpenTrans, cmdType, cmdText, parameters);
  159. num = cmd.ExecuteNonQuery();
  160. }
  161. }
  162. else
  163. {
  164. PrepareCommand(cmd, isOpenTrans.Connection, isOpenTrans, cmdType, cmdText, parameters);
  165. num = cmd.ExecuteNonQuery();
  166. }
  167. cmd.Parameters.Clear();
  168. }
  169. catch (Exception ex)
  170. {
  171. num = -1;
  172. //log.Error(ex.Message);
  173. }
  174. return num;
  175. }
  176. /// <summary>
  177. /// 执行 SQL 语句,并返回受影响的行数。
  178. /// </summary>
  179. /// <param name="isOpenTrans">事务对象</param>
  180. /// <param name="cmdType">执行命令的类型(存储过程或T-SQL,等等)</param>
  181. /// <param name="cmdText">存储过程名称或者T-SQL命令行</param>
  182. /// <returns></returns>
  183. public static int ExecuteNonQuery(DbTransaction isOpenTrans, CommandType cmdType, string cmdText)
  184. {
  185. int num = 0;
  186. try
  187. {
  188. DbCommand cmd = DbFactory.CreateDbCommand();
  189. PrepareCommand(cmd, isOpenTrans.Connection, isOpenTrans, cmdType, cmdText, null);
  190. num = cmd.ExecuteNonQuery();
  191. cmd.Parameters.Clear();
  192. }
  193. catch (Exception ex)
  194. {
  195. num = -1;
  196. //log.Error(ex.Message);
  197. }
  198. return num;
  199. }
  200. /// <summary>
  201. /// 使用提供的参数,执行有结果集返回的数据库操作命令、并返回SqlDataReader对象
  202. /// </summary>
  203. /// <param name="isOpenTrans">事务对象</param>
  204. /// <param name="commandType">执行命令的类型(存储过程或T-SQL,等等)</param>
  205. /// <param name="commandText">存储过程名称或者T-SQL命令行<</param>
  206. /// <param name="parameters">执行命令所需的sql语句对应参数</param>
  207. /// <returns>返回SqlDataReader对象</returns>
  208. public static IDataReader ExecuteReader(DbTransaction isOpenTrans, CommandType cmdType, string cmdText, params DbParameter[] parameters)
  209. {
  210. DbCommand cmd = DbFactory.CreateDbCommand();
  211. DbConnection conn = DbFactory.CreateDbConnection(ConnectionString);
  212. try
  213. {
  214. PrepareCommand(cmd, conn, isOpenTrans, cmdType, cmdText, parameters);
  215. IDataReader rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
  216. cmd.Parameters.Clear();
  217. return rdr;
  218. }
  219. catch (Exception ex)
  220. {
  221. conn.Close();
  222. cmd.Dispose();
  223. //log.Error(ex.Message);
  224. throw;
  225. }
  226. }
  227. public static SqlDataReader ExecuteReader(string connectionString, CommandType cmdType, string cmdText, params SqlParameter[] commandParameters)
  228. {
  229. SqlCommand cmd = new SqlCommand();
  230. SqlConnection conn = new SqlConnection(connectionString);
  231. // we use a try/catch here because if the method throws an exception we want to
  232. // close the connection throw code, because no datareader will exist, hence the
  233. // commandBehaviour.CloseConnection will not work
  234. try
  235. {
  236. PrepareCommand(cmd, conn, null, cmdType, cmdText, commandParameters);
  237. SqlDataReader rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
  238. cmd.Parameters.Clear();
  239. return rdr;
  240. }
  241. catch
  242. {
  243. conn.Close();
  244. throw;
  245. }
  246. }
  247. /// <summary>
  248. /// 返回一个数据表
  249. /// </summary>
  250. /// <param name="connectionString">数据库连接字符串</param>
  251. /// <param name="cmdType">命令类型</param>
  252. /// <param name="cmdText">SQL语句</param>
  253. /// <param name="commandParameters">参数</param>
  254. /// <param name="strTableName">数据表名</param>
  255. /// <param name="bIsLoadStru">是否加载数据表结构</param>
  256. /// <returns></returns>
  257. public static DataTable ExecuteTable(string connectionString, CommandType cmdType, string cmdText, bool bIsLoadStru, params SqlParameter[] commandParameters)
  258. {
  259. using (SqlConnection connection = new SqlConnection(connectionString))
  260. {
  261. DataTable dt = ExecuteTable(connection, cmdType, cmdText, bIsLoadStru, commandParameters);
  262. connection.Close();
  263. return dt;
  264. }
  265. }
  266. /// <summary>
  267. /// 返回一个数据表
  268. /// </summary>
  269. /// <param name="connection">数据库连接</param>
  270. /// <param name="cmdType">命令类型</param>
  271. /// <param name="cmdText">SQL语句</param>
  272. /// <param name="commandParameters">参数</param>
  273. /// <param name="strTableName">数据表名</param>
  274. /// <param name="bIsLoadStru">是否加载数据表结构</param>
  275. /// <returns></returns>
  276. public static DataTable ExecuteTable(SqlConnection connection, CommandType cmdType, string cmdText, bool bIsLoadStru, params SqlParameter[] commandParameters)
  277. {
  278. DataSet ds = new DataSet();
  279. SqlCommand cmd = new SqlCommand();
  280. PrepareCommand(cmd, connection, null, cmdType, cmdText, commandParameters);
  281. SqlDataAdapter da = new SqlDataAdapter(cmd);
  282. if (bIsLoadStru)
  283. {
  284. da.FillSchema(ds, SchemaType.Source);
  285. }
  286. da.Fill(ds);
  287. cmd.Parameters.Clear();
  288. return ds.Tables[0];
  289. }
  290. public static int ExecuteNonQuery(string connectionString, CommandType cmdType, string cmdText, params SqlParameter[] commandParameters)
  291. {
  292. SqlCommand cmd = new SqlCommand();
  293. using (SqlConnection conn = new SqlConnection(connectionString))
  294. {
  295. PrepareCommand(cmd, conn, null, cmdType, cmdText, commandParameters);
  296. int val = cmd.ExecuteNonQuery();
  297. cmd.Parameters.Clear();
  298. return val;
  299. }
  300. }
  301. /// <summary>
  302. /// 使用提供的参数,执行有结果集返回的数据库操作命令、并返回SqlDataReader对象
  303. /// </summary>
  304. /// <param name="commandType">执行命令的类型(存储过程或T-SQL,等等)</param>
  305. /// <param name="commandText">存储过程名称或者T-SQL命令行<</param>
  306. /// <param name="parameters">执行命令所需的sql语句对应参数</param>
  307. /// <returns>返回SqlDataReader对象</returns>
  308. public static IDataReader ExecuteReader(CommandType cmdType, string cmdText, params DbParameter[] parameters)
  309. {
  310. DbCommand cmd = DbFactory.CreateDbCommand();
  311. DbConnection conn = DbFactory.CreateDbConnection(ConnectionString);
  312. try
  313. {
  314. PrepareCommand(cmd, conn, null, cmdType, cmdText, parameters);
  315. IDataReader rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
  316. cmd.Parameters.Clear();
  317. return rdr;
  318. }
  319. catch (Exception ex)
  320. {
  321. conn.Close();
  322. cmd.Dispose();
  323. //log.Error(ex.Message);
  324. throw;
  325. }
  326. }
  327. /// <summary>
  328. ///使用提供的参数,执行有结果集返回的数据库操作命令、并返回SqlDataReader对象
  329. /// </summary>
  330. /// <param name="commandType">执行命令的类型(存储过程或T-SQL,等等)</param>
  331. /// <param name="commandText">存储过程名称或者T-SQL命令行<</param>
  332. /// <returns>返回SqlDataReader对象</returns>
  333. public static IDataReader ExecuteReader(CommandType cmdType, string cmdText)
  334. {
  335. DbCommand cmd = DbFactory.CreateDbCommand();
  336. DbConnection conn = DbFactory.CreateDbConnection(ConnectionString);
  337. try
  338. {
  339. PrepareCommand(cmd, conn, null, cmdType, cmdText, null);
  340. IDataReader rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
  341. cmd.Parameters.Clear();
  342. return rdr;
  343. }
  344. catch (Exception ex)
  345. {
  346. conn.Close();
  347. cmd.Dispose();
  348. //log.Error(ex.Message);
  349. throw;
  350. }
  351. }
  352. /// <summary>
  353. /// 查询数据填充到数据集DataSet中
  354. /// </summary>
  355. /// <param name="cmdType">执行命令的类型(存储过程或T-SQL,等等)</param>
  356. /// <param name="cmdText">命令文本</param>
  357. /// <param name="parameters">sql语句对应参数</param>
  358. /// <returns>数据集DataSet对象</returns>
  359. public static DataSet GetDataSet(CommandType cmdType, string cmdText, params DbParameter[] parameters)
  360. {
  361. DataSet ds = new DataSet();
  362. DbCommand cmd = DbFactory.CreateDbCommand();
  363. DbConnection conn = DbFactory.CreateDbConnection(ConnectionString);
  364. try
  365. {
  366. PrepareCommand(cmd, conn, null, cmdType, cmdText, parameters);
  367. IDbDataAdapter sda = DbFactory.CreateDataAdapter(cmd);
  368. sda.Fill(ds);
  369. return ds;
  370. }
  371. catch (Exception ex)
  372. {
  373. conn.Close();
  374. cmd.Dispose();
  375. //log.Error(ex.Message);
  376. throw;
  377. }
  378. }
  379. /// <summary>
  380. /// 查询数据填充到数据集DataSet中
  381. /// </summary>
  382. /// <param name="cmdType">执行命令的类型(存储过程或T-SQL,等等)</param>
  383. /// <param name="cmdText">命令文本</param>
  384. /// <returns>数据集DataSet对象</returns>
  385. public static DataSet GetDataSet(CommandType cmdType, string cmdText)
  386. {
  387. DataSet ds = new DataSet();
  388. DbCommand cmd = DbFactory.CreateDbCommand();
  389. DbConnection conn = DbFactory.CreateDbConnection(ConnectionString);
  390. try
  391. {
  392. PrepareCommand(cmd, conn, null, cmdType, cmdText, null);
  393. IDbDataAdapter sda = DbFactory.CreateDataAdapter(cmd);
  394. sda.Fill(ds);
  395. return ds;
  396. }
  397. catch (Exception ex)
  398. {
  399. conn.Close();
  400. cmd.Dispose();
  401. //log.Error(ex.Message);
  402. throw;
  403. }
  404. }
  405. /// <summary>
  406. /// 依靠数据库连接字符串connectionString,
  407. /// 使用所提供参数,执行返回首行首列命令
  408. /// </summary>
  409. /// <param name="commandType">执行命令的类型(存储过程或T-SQL,等等)</param>
  410. /// <param name="commandText">存储过程名称或者T-SQL命令行</param>
  411. /// <param name="parameters">执行命令所需的sql语句对应参数</param>
  412. /// <returns>返回一个对象,使用Convert.To{Type}将该对象转换成想要的数据类型。</returns>
  413. public static object ExecuteScalar(CommandType cmdType, string cmdText, params DbParameter[] parameters)
  414. {
  415. try
  416. {
  417. DbCommand cmd = DbFactory.CreateDbCommand();
  418. using (DbConnection connection = DbFactory.CreateDbConnection(ConnectionString))
  419. {
  420. PrepareCommand(cmd, connection, null, cmdType, cmdText, parameters);
  421. object val = cmd.ExecuteScalar();
  422. cmd.Parameters.Clear();
  423. return val;
  424. }
  425. }
  426. catch (Exception ex)
  427. {
  428. //log.Error(ex.Message);
  429. throw;
  430. }
  431. }
  432. /// <summary>
  433. /// 依靠数据库连接字符串connectionString,
  434. /// 使用所提供参数,执行返回首行首列命令
  435. /// </summary>
  436. /// <param name="commandType">执行命令的类型(存储过程或T-SQL,等等)</param>
  437. /// <param name="commandText">存储过程名称或者T-SQL命令行</param>
  438. /// <returns>返回一个对象,使用Convert.To{Type}将该对象转换成想要的数据类型。</returns>
  439. public static object ExecuteScalar(CommandType cmdType, string cmdText)
  440. {
  441. try
  442. {
  443. DbCommand cmd = DbFactory.CreateDbCommand();
  444. using (DbConnection connection = DbFactory.CreateDbConnection(ConnectionString))
  445. {
  446. PrepareCommand(cmd, connection, null, cmdType, cmdText, null);
  447. object val = cmd.ExecuteScalar();
  448. cmd.Parameters.Clear();
  449. return val;
  450. }
  451. }
  452. catch (Exception ex)
  453. {
  454. //log.Error(ex.Message);
  455. throw;
  456. }
  457. }
  458. /// <summary>
  459. ///依靠数据库连接字符串connectionString,
  460. /// 使用所提供参数,执行返回首行首列命令
  461. /// </summary>
  462. /// <param name="connection">数据库连接对象</param>
  463. /// <param name="commandType">执行命令的类型(存储过程或T-SQL,等等)</param>
  464. /// <param name="commandText">存储过程名称或者T-SQL命令行</param>
  465. /// <param name="parameters">执行命令所需的sql语句对应参数</param>
  466. /// <returns>返回一个对象,使用Convert.To{Type}将该对象转换成想要的数据类型。</returns>
  467. public static object ExecuteScalar(DbConnection connection, CommandType cmdType, string cmdText, params DbParameter[] parameters)
  468. {
  469. try
  470. {
  471. DbCommand cmd = DbFactory.CreateDbCommand();
  472. PrepareCommand(cmd, connection, null, cmdType, cmdText, parameters);
  473. object val = cmd.ExecuteScalar();
  474. cmd.Parameters.Clear();
  475. return val;
  476. }
  477. catch (Exception ex)
  478. {
  479. //log.Error(ex.Message);
  480. throw;
  481. }
  482. }
  483. /// <summary>
  484. ///依靠数据库连接字符串connectionString,
  485. /// 使用所提供参数,执行返回首行首列命令
  486. /// </summary>
  487. /// <param name="connection">数据库连接对象</param>
  488. /// <param name="commandType">执行命令的类型(存储过程或T-SQL,等等)</param>
  489. /// <param name="commandText">存储过程名称或者T-SQL命令行</param>
  490. /// <param name="parameters">执行命令所需的sql语句对应参数</param>
  491. /// <returns>返回一个对象,使用Convert.To{Type}将该对象转换成想要的数据类型。</returns>
  492. public static object ExecuteScalar(DbConnection connection, CommandType cmdType, string cmdText)
  493. {
  494. try
  495. {
  496. DbCommand cmd = DbFactory.CreateDbCommand();
  497. PrepareCommand(cmd, connection, null, cmdType, cmdText, null);
  498. object val = cmd.ExecuteScalar();
  499. cmd.Parameters.Clear();
  500. return val;
  501. }
  502. catch (Exception ex)
  503. {
  504. //log.Error(ex.Message);
  505. throw;
  506. }
  507. }
  508. /// <summary>
  509. ///依靠数据库连接字符串connectionString,
  510. /// 使用所提供参数,执行返回首行首列命令
  511. /// </summary>
  512. /// <param name="connection">数据库连接对象</param>
  513. /// <param name="commandType">执行命令的类型(存储过程或T-SQL,等等)</param>
  514. /// <param name="commandText">存储过程名称或者T-SQL命令行</param>
  515. /// <param name="parameters">执行命令所需的sql语句对应参数</param>
  516. /// <returns>返回一个对象,使用Convert.To{Type}将该对象转换成想要的数据类型。</returns>
  517. public static object ExecuteScalar(DbConnection conn, DbTransaction isOpenTrans, CommandType cmdType, string cmdText)
  518. {
  519. try
  520. {
  521. DbCommand cmd = DbFactory.CreateDbCommand();
  522. PrepareCommand(cmd, conn, isOpenTrans, cmdType, cmdText, null);
  523. object val = cmd.ExecuteScalar();
  524. cmd.Parameters.Clear();
  525. return val;
  526. }
  527. catch (Exception ex)
  528. {
  529. //log.Error(ex.Message);
  530. throw;
  531. }
  532. }
  533. /// <summary>
  534. ///依靠数据库连接字符串connectionString,
  535. /// 使用所提供参数,执行返回首行首列命令
  536. /// </summary>
  537. /// <param name="isOpenTrans">事务</param>
  538. /// <param name="commandType">执行命令的类型(存储过程或T-SQL,等等)</param>
  539. /// <param name="commandText">存储过程名称或者T-SQL命令行</param>
  540. /// <param name="parameters">执行命令所需的sql语句对应参数</param>
  541. /// <returns>返回一个对象,使用Convert.To{Type}将该对象转换成想要的数据类型。</returns>
  542. public static object ExecuteScalar(DbTransaction isOpenTrans, CommandType cmdType, string cmdText, params DbParameter[] parameters)
  543. {
  544. try
  545. {
  546. DbCommand cmd = DbFactory.CreateDbCommand();
  547. PrepareCommand(cmd, isOpenTrans.Connection, isOpenTrans, cmdType, cmdText, parameters);
  548. object val = cmd.ExecuteScalar();
  549. cmd.Parameters.Clear();
  550. return val;
  551. }
  552. catch (Exception ex)
  553. {
  554. //log.Error(ex.Message);
  555. throw;
  556. }
  557. }
  558. /// <summary>
  559. /// 为即将执行准备一个命令
  560. /// </summary>
  561. /// <param name="cmd">SqlCommand对象</param>
  562. /// <param name="conn">SqlConnection对象</param>
  563. /// <param name="isOpenTrans">DbTransaction对象</param>
  564. /// <param name="cmdType">执行命令的类型(存储过程或T-SQL,等等)</param>
  565. /// <param name="cmdText">存储过程名称或者T-SQL命令行, e.g. Select * from Products</param>
  566. /// <param name="cmdParms">SqlParameters to use in the command</param>
  567. private static void PrepareCommand(DbCommand cmd, DbConnection conn, DbTransaction isOpenTrans, CommandType cmdType, string cmdText, DbParameter[] cmdParms)
  568. {
  569. if (conn.State != ConnectionState.Open)
  570. conn.Open();
  571. cmd.Connection = conn;
  572. cmd.CommandText = cmdText;
  573. if (isOpenTrans != null)
  574. cmd.Transaction = isOpenTrans;
  575. cmd.CommandType = cmdType;
  576. if (cmdParms != null)
  577. {
  578. cmd.Parameters.AddRange(cmdParms);
  579. }
  580. }
  581. /// <summary>
  582. /// 用于数据库类型的字符串枚举转换
  583. /// </summary>
  584. /// <typeparam name="T"></typeparam>
  585. /// <param name="value"></param>
  586. /// <returns></returns>
  587. public void DatabaseTypeEnumParse(string value)
  588. {
  589. try
  590. {
  591. switch (value)
  592. {
  593. case "System.Data.SqlClient":
  594. DbType = DatabaseType.SqlServer;
  595. break;
  596. case "System.Data.OracleClient":
  597. DbType = DatabaseType.Oracle;
  598. break;
  599. case "MySql.Data.MySqlClient":
  600. DbType = DatabaseType.MySql;
  601. break;
  602. case "System.Data.OleDb":
  603. DbType = DatabaseType.Access;
  604. break;
  605. case "System.Data.SQLite":
  606. DbType = DatabaseType.SQLite;
  607. break;
  608. default:
  609. break;
  610. }
  611. }
  612. catch
  613. {
  614. throw new Exception("数据库类型\"" + value + "\"错误,请检查!");
  615. }
  616. }
  617. }
  618. }