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

661 lines
27 KiB

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