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

736 lines
30 KiB

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