|
- 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);
- }
- }
- }
|