CNAS取数仪器端升级
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

510 行
17KB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data;
  4. using System.Linq;
  5. using System.Text;
  6. using CnasSynchronousCommon;
  7. using Npgsql;
  8. using NpgsqlTypes;
  9. namespace CnasSynchronusDAL
  10. {
  11. public static class PostgreSQLHelper
  12. {
  13. private static String mConnStr = null;
  14. public static void InitConnectionString(string strServerIP,string strServerPort,string strServerHost,string strServerUser,string strServerPwd)
  15. {
  16. mConnStr = $"Server={strServerIP};Port={strServerPort};Database={strServerHost};User Id={strServerUser};Password={strServerPwd};";
  17. }
  18. public static void InitConnectionString(string strConnectiong)
  19. {
  20. mConnStr = strConnectiong;
  21. }
  22. /// <summary>
  23. /// 对SQLite数据库执行增删改操作,返回受影响的行数。
  24. /// </summary>
  25. /// <param name="sql">要执行的增删改的SQL语句</param>
  26. /// <returns></returns>
  27. public static int ExecuteNonQuery(String sql)
  28. {
  29. NpgsqlConnection connection = new NpgsqlConnection(mConnStr);
  30. try
  31. {
  32. using (connection)
  33. {
  34. connection.Open();
  35. NpgsqlTransaction transaction = connection.BeginTransaction();
  36. using (NpgsqlCommand cmd = new NpgsqlCommand())
  37. {
  38. try
  39. {
  40. PrepareCommand(cmd, connection, transaction, CommandType.Text, sql, null);
  41. int rows = cmd.ExecuteNonQuery();
  42. transaction.Commit();
  43. cmd.Parameters.Clear();
  44. return rows;
  45. }
  46. catch (NpgsqlException e1)
  47. {
  48. try
  49. {
  50. transaction.Rollback();
  51. }
  52. catch (Exception e2)
  53. {
  54. AppLog.Error(e2.Message);
  55. throw;
  56. }
  57. AppLog.Error(e1.Message);
  58. throw;
  59. }
  60. }
  61. }
  62. }
  63. catch (Exception e)
  64. {
  65. AppLog.Error(e.Message);
  66. throw;
  67. }
  68. }
  69. /// <summary>
  70. /// 对数据库执行增删改操作,返回受影响的行数。
  71. /// </summary>
  72. /// <param name="sql">要执行的增删改的SQL语句</param>
  73. /// <returns></returns>
  74. public static int ExecuteNonQuery(String sql, NpgsqlParameter[] cmdParams)
  75. {
  76. try
  77. {
  78. using (NpgsqlConnection connection = new NpgsqlConnection(mConnStr))
  79. {
  80. connection.Open();
  81. NpgsqlTransaction transaction = connection.BeginTransaction();
  82. using (NpgsqlCommand cmd = new NpgsqlCommand())
  83. {
  84. try
  85. {
  86. PrepareCommand(cmd, connection, transaction, CommandType.Text, sql, cmdParams);
  87. int rows = cmd.ExecuteNonQuery();
  88. transaction.Commit();
  89. cmd.Parameters.Clear();
  90. return rows;
  91. }
  92. catch (NpgsqlException e1)
  93. {
  94. try
  95. {
  96. transaction.Rollback();
  97. }
  98. catch (Exception e2)
  99. {
  100. AppLog.Error(e2.Message);
  101. throw;
  102. }
  103. AppLog.Error(e1.Message);
  104. throw;
  105. }
  106. }
  107. }
  108. }
  109. catch (Exception e)
  110. {
  111. AppLog.Error(e.Message);
  112. throw;
  113. }
  114. }
  115. /// <summary>
  116. /// 对SQLite数据库执行操作,返回 返回第一行第一列数据
  117. /// </summary>
  118. /// <param name="sql"></param>
  119. /// <returns></returns>
  120. public static int ExecuteScalar(String sql)
  121. {
  122. try
  123. {
  124. using (NpgsqlConnection connection = new NpgsqlConnection(mConnStr))
  125. {
  126. connection.Open();
  127. NpgsqlTransaction transaction = connection.BeginTransaction();
  128. using (NpgsqlCommand cmd = new NpgsqlCommand())
  129. {
  130. try
  131. {
  132. int line = 0;
  133. PrepareCommand(cmd, connection, transaction, CommandType.Text, sql, null);
  134. String str = cmd.ExecuteScalar().ToString();
  135. transaction.Commit();
  136. line = Convert.ToInt32(str);
  137. cmd.Parameters.Clear();
  138. return line;
  139. }
  140. catch (NpgsqlException e1)
  141. {
  142. try
  143. {
  144. transaction.Rollback();
  145. }
  146. catch (Exception e2)
  147. {
  148. AppLog.Error(e2.Message);
  149. throw;
  150. }
  151. AppLog.Error(e1.Message);
  152. throw;
  153. }
  154. }
  155. }
  156. }
  157. catch (Exception e)
  158. {
  159. AppLog.Error(e.Message);
  160. throw;
  161. }
  162. }
  163. /// <summary>
  164. /// 对SQLite数据库执行操作,返回 返回第一行第一列数据
  165. /// </summary>
  166. /// <param name="sql"></param>
  167. /// <returns></returns>
  168. public static int ExecuteScalar(String sql, NpgsqlParameter[] cmdParams)
  169. {
  170. try
  171. {
  172. using (NpgsqlConnection connection = new NpgsqlConnection(mConnStr))
  173. {
  174. connection.Open();
  175. NpgsqlTransaction transaction = connection.BeginTransaction();
  176. using (NpgsqlCommand cmd = new NpgsqlCommand())
  177. {
  178. try
  179. {
  180. int line = 0;
  181. PrepareCommand(cmd, connection, transaction, CommandType.Text, sql, cmdParams);
  182. String str = cmd.ExecuteScalar().ToString();
  183. transaction.Commit();
  184. line = Convert.ToInt32(str);
  185. cmd.Parameters.Clear();
  186. return line;
  187. }
  188. catch (NpgsqlException e1)
  189. {
  190. try
  191. {
  192. transaction.Rollback();
  193. }
  194. catch (Exception e2)
  195. {
  196. AppLog.Error(e2.Message);
  197. throw;
  198. }
  199. AppLog.Error(e1.Message);
  200. throw;
  201. }
  202. }
  203. }
  204. }
  205. catch (Exception e)
  206. {
  207. AppLog.Error(e.Message);
  208. throw;
  209. }
  210. }
  211. /// <summary>
  212. /// 用执行的数据库连接执行一个返回数据集的sql命令
  213. /// </summary>
  214. /// <param name="sql"></param>
  215. /// <returns></returns>
  216. public static NpgsqlDataReader ExecuteReader(String sql)
  217. {
  218. try
  219. {
  220. //创建一个NpgsqlConnection对象
  221. using (NpgsqlConnection connection = new NpgsqlConnection(mConnStr))
  222. {
  223. connection.Open();
  224. NpgsqlTransaction transaction = connection.BeginTransaction();
  225. //创建一个NpgsqlCommand对象
  226. using (NpgsqlCommand cmd = new NpgsqlCommand())
  227. {
  228. try
  229. {
  230. PrepareCommand(cmd, connection, transaction, CommandType.Text, sql, null);
  231. NpgsqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
  232. transaction.Commit();
  233. cmd.Parameters.Clear();
  234. return reader;
  235. }
  236. catch (NpgsqlException e1)
  237. {
  238. try
  239. {
  240. transaction.Rollback();
  241. }
  242. catch (Exception e2)
  243. {
  244. AppLog.Error(e2.Message);
  245. throw;
  246. }
  247. AppLog.Error(e1.Message);
  248. throw;
  249. }
  250. }
  251. }
  252. }
  253. catch (Exception e)
  254. {
  255. AppLog.Error(e.Message);
  256. throw;
  257. }
  258. }
  259. /// <summary>
  260. /// 查询返回Dtaset
  261. /// </summary>
  262. /// <param name="sql"></param>
  263. /// <returns></returns>
  264. public static DataSet ExecuteDataSet(String sql)
  265. {
  266. NpgsqlConnection connection = new NpgsqlConnection(mConnStr);
  267. try
  268. {
  269. //创建一个NpgsqlConnection对象
  270. using (connection)
  271. {
  272. connection.Open();
  273. NpgsqlTransaction transaction = connection.BeginTransaction();
  274. //创建一个NpgsqlCommand对象
  275. using (NpgsqlCommand cmd = new NpgsqlCommand())
  276. {
  277. try
  278. {
  279. AppLog.Error("===-3113-==="+ sql + mConnStr);
  280. PrepareCommand(cmd, connection, transaction, CommandType.Text, sql, null);
  281. AppLog.Error("===-3223-==="+ sql + mConnStr);
  282. NpgsqlDataAdapter adapter = new NpgsqlDataAdapter();
  283. adapter.SelectCommand = cmd;
  284. DataSet ds = new DataSet();
  285. AppLog.Error("===-3223-==="+ sql + ds);
  286. adapter.Fill(ds);
  287. AppLog.Error("===-3333-==="+ sql + ds);
  288. transaction.Commit();
  289. AppLog.Error("===-3443-==="+ sql + ds);
  290. //清除参数
  291. cmd.Parameters.Clear();
  292. return ds;
  293. AppLog.Error("===-3553-==="+ sql + ds);
  294. }
  295. catch (NpgsqlException e1)
  296. {
  297. try
  298. {
  299. transaction.Rollback();
  300. }
  301. catch (Exception e2)
  302. {
  303. AppLog.Error(e2.Message);
  304. throw;
  305. }
  306. AppLog.Error(e1.Message);
  307. throw;
  308. }
  309. }
  310. }
  311. }
  312. catch (Exception e)
  313. {
  314. AppLog.Error(e.Message);
  315. throw;
  316. }finally
  317. {
  318. if (connection != null)
  319. {
  320. //关闭数据库连接
  321. connection.Close();
  322. }
  323. }
  324. }
  325. /// <summary>
  326. /// 执行sql 返回一个DataTable
  327. /// </summary>
  328. /// <param name="sqlText"></param>
  329. /// <param name="parameters"></param>
  330. /// <returns></returns>
  331. public static DataTable ExecuteDataTable(string sqlText, params NpgsqlParameter[] parameters)
  332. {
  333. DataTable dt = null;
  334. try
  335. {
  336. using (NpgsqlDataAdapter adapter = new NpgsqlDataAdapter(sqlText, mConnStr))
  337. {
  338. dt = new DataTable();
  339. adapter.SelectCommand.Parameters.AddRange(parameters);
  340. adapter.Fill(dt);
  341. return dt;
  342. }
  343. }
  344. catch (Exception ex)
  345. {
  346. AppLog.Error(ex.Message);
  347. }
  348. return null;
  349. }
  350. /// <summary>
  351. /// 查询返回Dtaset
  352. /// </summary>
  353. /// <param name="sql"></param>
  354. /// <returns></returns>
  355. public static DataSet ExecuteDataSet(String sql, NpgsqlParameter[] cmdParams)
  356. {
  357. NpgsqlConnection connection = new NpgsqlConnection(mConnStr);
  358. try
  359. {
  360. //创建一个NpgsqlConnection对象
  361. using (connection)
  362. {
  363. connection.Open();
  364. NpgsqlTransaction transaction = connection.BeginTransaction();
  365. //创建一个NpgsqlCommand对象
  366. using (NpgsqlCommand cmd = new NpgsqlCommand())
  367. {
  368. try
  369. {
  370. PrepareCommand(cmd, connection, transaction, CommandType.Text, sql, cmdParams);
  371. NpgsqlDataAdapter adapter = new NpgsqlDataAdapter();
  372. adapter.SelectCommand = cmd;
  373. DataSet ds = new DataSet();
  374. adapter.Fill(ds);
  375. transaction.Commit();
  376. //清除参数
  377. cmd.Parameters.Clear();
  378. return ds;
  379. }
  380. catch (NpgsqlException e1)
  381. {
  382. try
  383. {
  384. transaction.Rollback();
  385. }
  386. catch (Exception e2)
  387. {
  388. AppLog.Error(e2.Message);
  389. throw;
  390. }
  391. AppLog.Error(e1.Message);
  392. throw;
  393. }
  394. }
  395. }
  396. }
  397. catch (Exception e)
  398. {
  399. AppLog.Error(e.Message);
  400. throw;
  401. }finally
  402. {
  403. if (connection != null)
  404. {
  405. //关闭数据库连接
  406. connection.Close();
  407. }
  408. }
  409. }
  410. /// <summary>
  411. /// 准备执行一个命令
  412. /// </summary>
  413. /// <param name="cmd">sql命令</param>
  414. /// <param name="conn">OleDb连接</param>
  415. /// <param name="trans">OleDb事务</param>
  416. /// <param name="cmdType">命令类型例如 存储过程或者文本</param>
  417. /// <param name="cmdText">命令文本,例如:Select * from Products</param>
  418. /// <param name="cmdParms">执行命令的参数</param>
  419. private static void PrepareCommand(NpgsqlCommand cmd, NpgsqlConnection conn, NpgsqlTransaction trans, CommandType cmdType, string cmdText, NpgsqlParameter[] cmdParms)
  420. {
  421. if (conn.State != ConnectionState.Open)
  422. conn.Open();
  423. cmd.Connection = conn;
  424. cmd.CommandText = cmdText;
  425. if (trans != null)
  426. cmd.Transaction = trans;
  427. cmd.CommandType = cmdType;
  428. if (cmdParms != null)
  429. {
  430. foreach (NpgsqlParameter parm in cmdParms)
  431. cmd.Parameters.Add(parm);
  432. }
  433. }
  434. public static bool TestConnectPostgreSQL()
  435. {
  436. bool bIfSuccess = false;
  437. try
  438. {
  439. using (NpgsqlConnection connection = new NpgsqlConnection(mConnStr))
  440. {
  441. connection.Open();
  442. if (connection.State == System.Data.ConnectionState.Open)
  443. bIfSuccess = true;
  444. }
  445. }
  446. catch (Exception ex)
  447. {
  448. AppLog.Error(ex.Message);
  449. bIfSuccess = false;
  450. }
  451. return bIfSuccess;
  452. }
  453. }
  454. }