C# 操作PostgreSQL 資料庫
轉載自:https://blog.csdn.net/mygisforum/article/details/7931749
一、第三方類庫
Npgsql提供 .NET 操作postgreSQL資料庫的相關類。支援postgreSQL7.x 及以上版本。
二、自定義介面及操作類
2.1介面類
public interface IDBHelper
{
/// <summary>
/// 執行 Transact-SQL 語句並返回受影響的行數。
/// </summary>
int ExecuteNonQuery(string connectionString, System.Data.CommandType cmdType, string cmdText, params System.Data.Common.DbParameter[] cmdParms);
/// <summary>
/// 在事務中執行 Transact-SQL 語句並返回受影響的行數。
/// </summary>
int ExecuteNonQuery(System.Data.Common.DbTransaction trans, System.Data.CommandType cmdType, string cmdText, params System.Data.Common.DbParameter[] cmdParms);
/// <summary>
/// 在事務中執行查詢,返回DataSet
/// </summary>
DataSet ExecuteQuery(System.Data.Common.DbTransaction trans, System.Data.CommandType cmdType, string cmdText, params System.Data.Common.DbParameter[] cmdParms);
/// <summary>
/// 執行查詢,返回DataSet
/// </summary>
DataSet ExecuteQuery(string connectionString, System.Data.CommandType cmdType, string cmdText, params System.Data.Common.DbParameter[] cmdParms);
/// <summary>
/// 在事務中執行查詢,返回DataReader
/// </summary>
DbDataReader ExecuteReader(System.Data.Common.DbTransaction trans, System.Data.CommandType cmdType, string cmdText, params System.Data.Common.DbParameter[] cmdParms);
/// <summary>
/// 執行查詢,返回DataReader
/// </summary>
DbDataReader ExecuteReader(string connectionString, System.Data.CommandType cmdType, string cmdText, params System.Data.Common.DbParameter[] cmdParms);
/// <summary>
/// 在事務中執行查詢,並返回查詢所返回的結果集中第一行的第一列。忽略其他列或行。
/// </summary>
object ExecuteScalar(System.Data.Common.DbTransaction trans, System.Data.CommandType cmdType, string cmdText, params System.Data.Common.DbParameter[] cmdParms);
/// <summary>
/// 執行查詢,並返回查詢所返回的結果集中第一行的第一列。忽略其他列或行。
/// </summary>
object ExecuteScalar(string connectionString, System.Data.CommandType cmdType, string cmdText, params System.Data.Common.DbParameter[] cmdParms);
/// <summary>
/// 得到資料條數
/// </summary>
/// <param name="tblName">表名</param>
/// <param name="condition">條件(不需要where)</param>
/// <returns>資料條數</returns>
int GetCount(string connectionString, string tblName, string condition);
}
2.2 操作類
/// <summary>
/// 資料庫操作基類(for PostgreSQL)
/// </summary>
public class PostgreHelper : IDBHelper
{
/// <summary>
/// 得到資料條數
/// </summary>
public int GetCount(string connectionString, string tblName, string condition)
{
StringBuilder sql = new StringBuilder("select count(*) from " + tblName);
if (!string.IsNullOrEmpty(condition))
sql.Append(" where " + condition);
object count = ExecuteScalar(connectionString, CommandType.Text, sql.ToString(), null);
return int.Parse(count.ToString());
}
/// <summary>
/// 執行查詢,返回DataSet
/// </summary>
public DataSet ExecuteQuery(string connectionString, CommandType cmdType, string cmdText,
params DbParameter[] cmdParms)
{
using (NpgsqlConnection conn = new NpgsqlConnection(connectionString))
{
using (NpgsqlCommand cmd = new NpgsqlCommand())
{
PrepareCommand(cmd, conn, null, cmdType, cmdText, cmdParms);
using (NpgsqlDataAdapter da = new NpgsqlDataAdapter(cmd))
{
DataSet ds = new DataSet();
da.Fill(ds, "ds");
cmd.Parameters.Clear();
return ds;
}
}
}
}
/// <summary>
/// 在事務中執行查詢,返回DataSet
/// </summary>
public DataSet ExecuteQuery(DbTransaction trans, CommandType cmdType, string cmdText,
params DbParameter[] cmdParms)
{
NpgsqlCommand cmd = new NpgsqlCommand();
PrepareCommand(cmd, trans.Connection, trans, cmdType, cmdText, cmdParms);
NpgsqlDataAdapter da = new NpgsqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds, "ds");
cmd.Parameters.Clear();
return ds;
}
/// <summary>
/// 執行 Transact-SQL 語句並返回受影響的行數。
/// </summary>
public int ExecuteNonQuery(string connectionString, CommandType cmdType, string cmdText,
params DbParameter[] cmdParms)
{
NpgsqlCommand cmd = new NpgsqlCommand();
using (NpgsqlConnection conn = new NpgsqlConnection(connectionString))
{
PrepareCommand(cmd, conn, null, cmdType, cmdText, cmdParms);
int val = cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
return val;
}
}
/// <summary>
/// 在事務中執行 Transact-SQL 語句並返回受影響的行數。
/// </summary>
public int ExecuteNonQuery(DbTransaction trans, CommandType cmdType, string cmdText,
params DbParameter[] cmdParms)
{
NpgsqlCommand cmd = new NpgsqlCommand();
PrepareCommand(cmd, trans.Connection, trans, cmdType, cmdText, cmdParms);
int val = cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
return val;
}
/// <summary>
/// 執行查詢,返回DataReader
/// </summary>
public DbDataReader ExecuteReader(string connectionString, CommandType cmdType, string cmdText,
params DbParameter[] cmdParms)
{
NpgsqlCommand cmd = new NpgsqlCommand();
NpgsqlConnection conn = new NpgsqlConnection(connectionString);
try
{
PrepareCommand(cmd, conn, null, cmdType, cmdText, cmdParms);
NpgsqlDataReader rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
cmd.Parameters.Clear();
return rdr;
}
catch
{
conn.Close();
throw;
}
}
/// <summary>
/// 在事務中執行查詢,返回DataReader
/// </summary>
public DbDataReader ExecuteReader(DbTransaction trans, CommandType cmdType, string cmdText,
params DbParameter[] cmdParms)
{
NpgsqlCommand cmd = new NpgsqlCommand();
PrepareCommand(cmd, trans.Connection, trans, cmdType, cmdText, cmdParms);
NpgsqlDataReader rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
cmd.Parameters.Clear();
return rdr;
}
/// <summary>
/// 執行查詢,並返回查詢所返回的結果集中第一行的第一列。忽略其他列或行。
/// </summary>
public object ExecuteScalar(string connectionString, CommandType cmdType, string cmdText,
params DbParameter[] cmdParms)
{
NpgsqlCommand cmd = new NpgsqlCommand();
using (NpgsqlConnection connection = new NpgsqlConnection(connectionString))
{
PrepareCommand(cmd, connection, null, cmdType, cmdText, cmdParms);
object val = cmd.ExecuteScalar();
cmd.Parameters.Clear();
return val;
}
}
/// <summary>
/// 在事務中執行查詢,並返回查詢所返回的結果集中第一行的第一列。忽略其他列或行。
/// </summary>
public object ExecuteScalar(DbTransaction trans, CommandType cmdType, string cmdText,
params DbParameter[] cmdParms)
{
NpgsqlCommand cmd = new NpgsqlCommand();
PrepareCommand(cmd, trans.Connection, trans, cmdType, cmdText, cmdParms);
object val = cmd.ExecuteScalar();
cmd.Parameters.Clear();
return val;
}
/// <summary>
/// 生成要執行的命令
/// </summary>
/// <remarks>引數的格式:冒號+引數名</remarks>
private static void PrepareCommand(DbCommand cmd, DbConnection conn, DbTransaction trans, CommandType cmdType,
string cmdText, DbParameter[] cmdParms)
{
if (conn.State != ConnectionState.Open)
conn.Open();
cmd.Connection = conn;
cmd.CommandText = cmdText.Replace("@", ":").Replace("?", ":").Replace("[", "\"").Replace("]", "\"");
if (trans != null)
cmd.Transaction = trans;
cmd.CommandType = cmdType;
if (cmdParms != null)
{
foreach (NpgsqlParameter parm in cmdParms)
{
parm.ParameterName = parm.ParameterName.Replace("@", ":").Replace("?",":");
cmd.Parameters.Add(parm);
}
}
}
}
三、資料庫操作例項
寫入例項:
IDBHelper dbHelper = new PostgreHelper();
string connectionString = "User ID=postgres;Password=admin;Server=192.168.0.226;Port=5432;Database=MonitorDB;";
string sql = "insert into [RawLog]([ProjectID],[File],[Note]) values('專案編號',@file,'備註')";
string file = this.textBox1.Text;
if (!File.Exists(file)) return;
//獲取檔案二進位制流
System.IO.FileStream fs = new System.IO.FileStream(file, System.IO.FileMode.Open);
BinaryReader br = new BinaryReader(fs);
byte[] b = br.ReadBytes((int)fs.Length);
br.Close();fs.Close();
int r = dbHelper.ExecuteNonQuery(connectionString, CommandType.Text,sql,new Npgsql.NpgsqlParameter("@file",raw_log.File));
注意:
1.查詢語句中表名、欄位名需用中括號[ ] 封起來,在操作類中會統一替換為雙引號。
2.命令語句中的引數名格式為:@或?+ 引數名,在操作類中為統一替換為冒號。
3.對於二進位制流、日期等特殊型別需採用2中的引數形式
4.經多次測試postgreSQL資料庫只直接支援(冒號+引數名)的引數方式
---------------------
作者:mygisforum
來源:CSDN
原文:https://blog.csdn.net/mygisforum/article/details/7931749
版權宣告:本文為博主原創文章,轉載請附上博文連結!