|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185 |
- using System;
- using System.Data;
- using Npgsql;
- using CnasSynchrousModel;
-
- namespace CnasSynchronusDAL
- {
- public class PostgreSqlDAL : IDAL
- {
- private string connectionString;
-
- public PostgreSqlDAL(InstrumentDataSourceInfo dataSourceInfo)
- {
- connectionString = $"Server={dataSourceInfo.Host};Port={dataSourceInfo.Port};Database={dataSourceInfo.ServerName};User Id={dataSourceInfo.UserId};Password={dataSourceInfo.UserPwd};";
- }
-
- public bool TestConnection()
- {
- try
- {
- using (NpgsqlConnection conn = new NpgsqlConnection(connectionString))
- {
- conn.Open();
- return true;
- }
- }
- catch
- {
- return false;
- }
- }
-
- public DataTable GetDataTable(string sql)
- {
- DataTable dt = new DataTable();
- try
- {
- using (NpgsqlConnection conn = new NpgsqlConnection(connectionString))
- {
- conn.Open();
- using (NpgsqlCommand cmd = new NpgsqlCommand(sql, conn))
- {
- using (NpgsqlDataAdapter adapter = new NpgsqlDataAdapter(cmd))
- {
- adapter.Fill(dt);
- }
- }
- }
- }
- catch (Exception ex)
- {
- throw new Exception($"获取数据失败:{ex.Message}");
- }
- return dt;
- }
-
- public DataSet GetDataSet(string sql)
- {
- DataSet ds = new DataSet();
- try
- {
- using (NpgsqlConnection conn = new NpgsqlConnection(connectionString))
- {
- conn.Open();
- using (NpgsqlCommand cmd = new NpgsqlCommand(sql, conn))
- {
- using (NpgsqlDataAdapter adapter = new NpgsqlDataAdapter(cmd))
- {
- adapter.Fill(ds);
- }
- }
- }
- }
- catch (Exception ex)
- {
- throw new Exception($"获取数据失败:{ex.Message}");
- }
- return ds;
- }
-
- public int ExecuteNonQuery(string sql)
- {
- try
- {
- using (NpgsqlConnection conn = new NpgsqlConnection(connectionString))
- {
- conn.Open();
- using (NpgsqlCommand cmd = new NpgsqlCommand(sql, conn))
- {
- return cmd.ExecuteNonQuery();
- }
- }
- }
- catch (Exception ex)
- {
- throw new Exception($"执行SQL失败:{ex.Message}");
- }
- }
-
- public object ExecuteScalar(string sql)
- {
- try
- {
- using (NpgsqlConnection conn = new NpgsqlConnection(connectionString))
- {
- conn.Open();
- using (NpgsqlCommand cmd = new NpgsqlCommand(sql, conn))
- {
- return cmd.ExecuteScalar();
- }
- }
- }
- catch (Exception ex)
- {
- throw new Exception($"执行SQL失败:{ex.Message}");
- }
- }
-
- public bool ExecuteTransaction(string[] sqlArray)
- {
- using (NpgsqlConnection conn = new NpgsqlConnection(connectionString))
- {
- conn.Open();
- using (NpgsqlTransaction transaction = conn.BeginTransaction())
- {
- try
- {
- foreach (string sql in sqlArray)
- {
- using (NpgsqlCommand cmd = new NpgsqlCommand(sql, conn, transaction))
- {
- cmd.ExecuteNonQuery();
- }
- }
- transaction.Commit();
- return true;
- }
- catch (Exception ex)
- {
- transaction.Rollback();
- throw new Exception($"执行事务失败:{ex.Message}");
- }
- }
- }
- }
-
- public void BulkCopy(DataTable sourceData, string targetTable)
- {
- try
- {
- using (NpgsqlConnection conn = new NpgsqlConnection(connectionString))
- {
- conn.Open();
-
- // 构建COPY命令
- using (var writer = conn.BeginBinaryImport($"COPY {targetTable} FROM STDIN (FORMAT BINARY)"))
- {
- foreach (DataRow row in sourceData.Rows)
- {
- writer.StartRow();
- for (int i = 0; i < sourceData.Columns.Count; i++)
- {
- if (row[i] == DBNull.Value)
- writer.WriteNull();
- else
- writer.Write(row[i]);
- }
- }
- writer.Complete();
- }
- }
- }
- catch (Exception ex)
- {
- throw new Exception($"批量复制数据失败:{ex.Message}");
- }
- }
-
- public void Dispose()
- {
- // 清理资源
- GC.SuppressFinalize(this);
- }
- }
- }
|