c#的pg数据库操作(增删改查)
2021/7/30 19:37:32
本文主要是介绍c#的pg数据库操作(增删改查),对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
1.webconfig增加
<connectionStrings>
<add name="postgres" connectionString="PORT=5433;DATABASE=gis;HOST=127.0.0.1;PASSWORD=123456;USER ID=postgres" />
</connectionStrings>
<system.webServer>
<directoryBrowse enabled="true" />
</system.webServer>
</configuration>
2.操作
using System; using System.Collections.Generic; using System.Configuration; using System.Data; using System.Linq; using System.Web; using Npgsql; namespace PipeLineEditor { public class PgsqlHelper { public static string connStr = ConfigurationManager.ConnectionStrings["postgres"].ToString(); /// <summary> /// 增 删 改(无参数) /// </summary> /// <param name="sql"></param> /// <returns></returns> public static int ExecuteSQL(string sql) { int n = -1; using (NpgsqlConnection conn = new NpgsqlConnection(connStr)) { using (NpgsqlCommand cmd = new NpgsqlCommand(sql, conn)) { try { conn.Open(); n = cmd.ExecuteNonQuery(); } catch (NpgsqlException exception) { throw new Exception(exception.Message); } finally { conn.Close(); } } } return n; } /// <summary> /// 增 删 改(有参数) /// </summary> /// <param name="sql"></param> /// <param name="parameters"></param> /// <returns></returns> public static int ExecuteNonQuery(string sql, params NpgsqlParameter[] parameters) { int n = -1; using (NpgsqlConnection conn = new NpgsqlConnection(connStr)) { conn.Open(); using (NpgsqlCommand cmd = conn.CreateCommand()) { cmd.CommandText = sql; cmd.Parameters.AddRange(parameters); n = cmd.ExecuteNonQuery(); } } return n; } /// <summary> /// 查询并返回结果集DataTable(带参数) /// </summary> /// <param name="sql"></param> /// <param name="parameters"></param> /// <returns></returns> public static DataTable ExecuteDataTable(string sql, params NpgsqlParameter[] parameters) { using (NpgsqlConnection conn = new NpgsqlConnection(connStr)) { conn.Open(); using (NpgsqlCommand cmd = conn.CreateCommand()) { cmd.CommandText = sql; if (parameters != null) { cmd.Parameters.AddRange(parameters); } NpgsqlDataAdapter adapter = new NpgsqlDataAdapter(cmd); DataSet dataset = new DataSet(); adapter.Fill(dataset); return dataset.Tables[0]; } } } /// <summary> /// 查询并返回结果集DataTable(无参数) /// </summary> /// <param name="sql"></param> /// <returns></returns> public static DataTable ExecuteQuery(string sql) { NpgsqlConnection sqlConn = new NpgsqlConnection(connStr); DataTable ds = new DataTable(); try { using (NpgsqlDataAdapter sqldap = new NpgsqlDataAdapter(sql, sqlConn)) { sqldap.Fill(ds); } return ds; } catch (System.Exception ex) { throw ex; } } } }
这篇关于c#的pg数据库操作(增删改查)的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2022-03-01沐雪多租宝商城源码从.NetCore3.1升级到.Net6的步骤
- 2024-11-15C#中怎么从PEM格式的证书中提取公钥?-icode9专业技术文章分享
- 2024-11-14云架构设计——如何用diagrams.net绘制专业的AWS架构图?
- 2024-05-08首个适配Visual Studio平台的国产智能编程助手CodeGeeX正式上线!C#程序员必备效率神器!
- 2024-03-30C#设计模式之十六迭代器模式(Iterator Pattern)【行为型】
- 2024-03-29c# datetime tryparse
- 2024-02-21list find index c#
- 2024-01-24convert toint32 c#
- 2024-01-24Advanced .Net Debugging 1:你必须知道的调试工具
- 2024-01-24.NET集成IdGenerator生成分布式全局唯一ID