CNAS取数仪器端升级
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.

PostgreSqlDAL.cs 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. using System;
  2. using System.Data;
  3. using Npgsql;
  4. using CnasSynchrousModel;
  5. namespace CnasSynchronusDAL
  6. {
  7. public class PostgreSqlDAL : IDAL
  8. {
  9. private string connectionString;
  10. public PostgreSqlDAL(InstrumentDataSourceInfo dataSourceInfo)
  11. {
  12. connectionString = $"Server={dataSourceInfo.Host};Port={dataSourceInfo.Port};Database={dataSourceInfo.ServerName};User Id={dataSourceInfo.UserId};Password={dataSourceInfo.UserPwd};";
  13. }
  14. public bool TestConnection()
  15. {
  16. try
  17. {
  18. using (NpgsqlConnection conn = new NpgsqlConnection(connectionString))
  19. {
  20. conn.Open();
  21. return true;
  22. }
  23. }
  24. catch
  25. {
  26. return false;
  27. }
  28. }
  29. public DataTable GetDataTable(string sql)
  30. {
  31. DataTable dt = new DataTable();
  32. try
  33. {
  34. using (NpgsqlConnection conn = new NpgsqlConnection(connectionString))
  35. {
  36. conn.Open();
  37. using (NpgsqlCommand cmd = new NpgsqlCommand(sql, conn))
  38. {
  39. using (NpgsqlDataAdapter adapter = new NpgsqlDataAdapter(cmd))
  40. {
  41. adapter.Fill(dt);
  42. }
  43. }
  44. }
  45. }
  46. catch (Exception ex)
  47. {
  48. throw new Exception($"获取数据失败:{ex.Message}");
  49. }
  50. return dt;
  51. }
  52. public DataSet GetDataSet(string sql)
  53. {
  54. DataSet ds = new DataSet();
  55. try
  56. {
  57. using (NpgsqlConnection conn = new NpgsqlConnection(connectionString))
  58. {
  59. conn.Open();
  60. using (NpgsqlCommand cmd = new NpgsqlCommand(sql, conn))
  61. {
  62. using (NpgsqlDataAdapter adapter = new NpgsqlDataAdapter(cmd))
  63. {
  64. adapter.Fill(ds);
  65. }
  66. }
  67. }
  68. }
  69. catch (Exception ex)
  70. {
  71. throw new Exception($"获取数据失败:{ex.Message}");
  72. }
  73. return ds;
  74. }
  75. public int ExecuteNonQuery(string sql)
  76. {
  77. try
  78. {
  79. using (NpgsqlConnection conn = new NpgsqlConnection(connectionString))
  80. {
  81. conn.Open();
  82. using (NpgsqlCommand cmd = new NpgsqlCommand(sql, conn))
  83. {
  84. return cmd.ExecuteNonQuery();
  85. }
  86. }
  87. }
  88. catch (Exception ex)
  89. {
  90. throw new Exception($"执行SQL失败:{ex.Message}");
  91. }
  92. }
  93. public object ExecuteScalar(string sql)
  94. {
  95. try
  96. {
  97. using (NpgsqlConnection conn = new NpgsqlConnection(connectionString))
  98. {
  99. conn.Open();
  100. using (NpgsqlCommand cmd = new NpgsqlCommand(sql, conn))
  101. {
  102. return cmd.ExecuteScalar();
  103. }
  104. }
  105. }
  106. catch (Exception ex)
  107. {
  108. throw new Exception($"执行SQL失败:{ex.Message}");
  109. }
  110. }
  111. public bool ExecuteTransaction(string[] sqlArray)
  112. {
  113. using (NpgsqlConnection conn = new NpgsqlConnection(connectionString))
  114. {
  115. conn.Open();
  116. using (NpgsqlTransaction transaction = conn.BeginTransaction())
  117. {
  118. try
  119. {
  120. foreach (string sql in sqlArray)
  121. {
  122. using (NpgsqlCommand cmd = new NpgsqlCommand(sql, conn, transaction))
  123. {
  124. cmd.ExecuteNonQuery();
  125. }
  126. }
  127. transaction.Commit();
  128. return true;
  129. }
  130. catch (Exception ex)
  131. {
  132. transaction.Rollback();
  133. throw new Exception($"执行事务失败:{ex.Message}");
  134. }
  135. }
  136. }
  137. }
  138. public void BulkCopy(DataTable sourceData, string targetTable)
  139. {
  140. try
  141. {
  142. using (NpgsqlConnection conn = new NpgsqlConnection(connectionString))
  143. {
  144. conn.Open();
  145. // 构建COPY命令
  146. using (var writer = conn.BeginBinaryImport($"COPY {targetTable} FROM STDIN (FORMAT BINARY)"))
  147. {
  148. foreach (DataRow row in sourceData.Rows)
  149. {
  150. writer.StartRow();
  151. for (int i = 0; i < sourceData.Columns.Count; i++)
  152. {
  153. if (row[i] == DBNull.Value)
  154. writer.WriteNull();
  155. else
  156. writer.Write(row[i]);
  157. }
  158. }
  159. writer.Complete();
  160. }
  161. }
  162. }
  163. catch (Exception ex)
  164. {
  165. throw new Exception($"批量复制数据失败:{ex.Message}");
  166. }
  167. }
  168. public void Dispose()
  169. {
  170. // 清理资源
  171. GC.SuppressFinalize(this);
  172. }
  173. }
  174. }