c# 访问postgressql,使用pghelper访问pgsql
由于工作需要,数据库是postgressql的,本来以为很简单的,结果弄了一晚上,为了总结经验,现将C#连接PGSQL(postgres sql)的资料整理如下。
一、总体思路
1、通过第三方Npgsql的的dll实现数据库的连接。
2、连接文件写在web.config中。
3、连接类写成pghelper。
这样,在程序中,可以像sqlhelper一样调用了。
二、下载资料
1、Npgsql的下载地址http://pgfoundry.org/projects/npgsql/,
2、当然,你需要安装postgres sql,去官网下载安装,过程略。
三、配置与代码
1、web.config 中,配置<connectionStrings>
放于<configuration></configuration>内任何位置均可。
样本如下:
<configuration>
<connectionStrings>
<add name="postgre" connectionString="PORT=5433;DATABASE=test;HOST=localhost;PASSWORD=123;USER ID=postgres"/>
</connectionStrings>
</configuration>
解释connectionString里存的就是连接PG数据库的配置信息了,具体略。
2、写一个pghelper类。
在类中,插入如何下的方法:
public static string ConnectionString = ConfigurationManager.ConnectionStrings["postgre"].ToString();
/// <summary>
/// 执行SQL语句
/// </summary>
/// <param name="sql">SQL</param>
/// <returns>成功返回大于0的数字</returns>
public static int ExecuteSQL(string sql)
{
int num2 = -;
using (NpgsqlConnection connection = new NpgsqlConnection(ConnectionString))
{
using (NpgsqlCommand command = new NpgsqlCommand(sql, connection))
{
try
{
connection.Open();
num2 = command.ExecuteNonQuery();
}
catch (NpgsqlException exception)
{
throw new Exception(exception.Message);
}
finally
{
connection.Close();
}
}
}
return num2;
}
//带参数的执行查询,不返回结果,返回影响行数
//执行SQL语句并返回受影响的行数
public static int ExecuteNonQuery(string sql, params NpgsqlParameter[] parameters)
{
using (NpgsqlConnection conn = new NpgsqlConnection(ConnectionString))
{
conn.Open();
using (NpgsqlCommand cmd = conn.CreateCommand())
{
cmd.CommandText = sql;
//foreach (SqlParameter param in parameters)
//{
// cmd.Parameters.Add(param);
//}
cmd.Parameters.AddRange(parameters);
return cmd.ExecuteNonQuery();
}
}
}
//执行查询,并返回查询所返回的结果集中第一行的第一列。忽略额外的列或行。
public static object ExecuteScalar(string sql, params NpgsqlParameter[] parameters)
{
using (NpgsqlConnection conn = new NpgsqlConnection(ConnectionString))
{
conn.Open();
using (NpgsqlCommand cmd = conn.CreateCommand())
{
cmd.CommandText = sql;
cmd.Parameters.AddRange(parameters);
return cmd.ExecuteScalar();
}
}
}
//查询并返回结果集DataTable,一般只用来执行查询结果比较少的sql。
public static DataTable ExecuteDataTable(string sql, params NpgsqlParameter[] parameters)
{
using (NpgsqlConnection conn = new NpgsqlConnection(ConnectionString))
{
conn.Open();
using (NpgsqlCommand cmd = conn.CreateCommand())
{
cmd.CommandText = sql;
cmd.Parameters.AddRange(parameters);
NpgsqlDataAdapter adapter = new NpgsqlDataAdapter(cmd);
DataSet dataset = new DataSet();
adapter.Fill(dataset);
return dataset.Tables[];
}
}
//查询较大的数据用 DateRead(),但应尽可能用分页数据,仍然用datatable更好。
}
四、使用。
使用时,就可以像SQLHELPER一样用了。
int i;
i = pghelper.ExecuteSQL("update student set classid=113 where id=4");
context.Response.Write("这是无参数的ExecuteSQL:" + i + "<br/>"); i = pghelper.ExecuteNonQuery("update student set classid=113 where id=@id", new NpgsqlParameter("@id", ));
context.Response.Write("这是有参数的调用ExecuteNonQuery:" + i + "<br/>"); string s = pghelper.ExecuteScalar("select count(name) from student").ToString();
context.Response.Write("这是exeScalar调用:" + s+"<br/><hr/>"); DataTable dt = new DataTable();
dt = pghelper.ExecuteDataTable("select id,name from student");
context.Response.Write("这是ExecuteDataTable:<br/>"); for (int j=;j<dt.Rows.Count;j++)
{
DataRow dr = dt.Rows[j];
context.Response.Write(dr["id"].ToString()+dr["name"].ToString());
}
其实,ExecuteNonQuery完全可以取代ExecuteSQL。使用习惯看个人吧。
好了,就写到这里了,不当之处还请各位看官体谅
c# 访问postgressql,使用pghelper访问pgsql的更多相关文章
- Nginx禁止ip访问或IP网段访问方法
Nginx禁止ip访问可以防止指定IP访问我们的网站,本例子可以实现是防止单IP访问或IP网段访问了,非常的有用我们一起来看看吧. 常用的linux做法 iptables参考规则 代码如下 复制代码 ...
- Java知多少(19)访问修饰符(访问控制符)
Java 通过修饰符来控制类.属性和方法的访问权限和其他功能,通常放在语句的最前端.例如: 1 public class className { 2 // body of class 3 } 4 pr ...
- php获得访问来源(手机wap访问、电脑web访问)
为了更好的用户体验,本主题默认支持wap和web两种访问样式,wap访问将不包含任何脚本.样式也是重新撰写,不支持更多页面,只支持首页.详情页, 评论框.登录等内容将陆续更新到wap内容里.如果想测试 ...
- dedecms由子目录访问修改为根目录访问
现在我需要将原来位于xampp/htdocs/zm下的网站修改到D:/lyh/webhome目录下访问,原来的访问路径为http://localhost/zm,现在为http://www.yihui. ...
- 重新想象 Windows 8 Store Apps (23) - 文件系统: 文本的读写, 二进制的读写, 流的读写, 最近访问列表和未来访问列表
原文:重新想象 Windows 8 Store Apps (23) - 文件系统: 文本的读写, 二进制的读写, 流的读写, 最近访问列表和未来访问列表 [源码下载] 重新想象 Windows 8 S ...
- 易语言关于使用CURL,网页_访问,网页_访问S,网页_访问_对象,鱼刺(winHttpW)发送Get性能测试
易语言关于使用 CURL,网页_访问,网页_访问S,网页_访问_对象,鱼刺(winHttpW)发送Get性能测试 测试模块情况: |-精易模块5.8 |-鱼刺类Http |-libCURL +++ ...
- 解决mysql中只能通过localhost访问不能通过ip访问的问题
解决mysql中只能通过localhost访问不能通过ip访问的问题 原因是没开权限 SELECT * FROM USER WHERE USER='root'; grant all privilege ...
- Python第五天 文件访问 for循环访问文件 while循环访问文件 字符串的startswith函数和split函数 linecache模块
Python第五天 文件访问 for循环访问文件 while循环访问文件 字符串的startswith函数和split函数 linecache模块 目录 Pycharm使用技巧( ...
- 大数据实操2 - hadoop集群访问——Hadoop客户端访问、Java API访问
上一篇中介绍了hadoop集群搭建方式,本文介绍集群的访问.集群的访问方式有两种:hadoop客户端访问,Java API访问. 一.集群客户端访问 Hadoop采用C/S架构,可以通过客户端对集群进 ...
- Chapter6_访问权限控制_访问权限修饰词
Java中有四种访问权限,public,private,protected和包访问权限,它们是置于类中每一个成员之前的定义,无论是一个域还是一个方法,下面一一介绍. 一.包访问权限 如果不提供任何访问 ...
随机推荐
- Ubuntu 安装python
1. wget http://mirrors.sohu.com/python/3.6.0/Python-3.6.0.tar.xz wget https://www.python.org/ftp/pyt ...
- The directory '/home/stone/.cache/pip/http' or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If execu
使用sudo pip install ......的时候出现下面一段黄色的代码: The directory '/home/stone/.cache/pip/http' or its parent d ...
- Oracle EBS AR 更新客户账户层
declare x_return_status ); x_msg_count NUMBER; x_msg_data ); x_profile_id NUMBER; l_location_id NUMB ...
- 解决 There are no resources that can be added or removed from the server
网上下载了一个项目,在eclipse中部署时,加载项目到tomcat中项目名称无法显示,报出There are no resources that can be added or removed fr ...
- 安装Window Server 2008的些配置
上次安装window server2008,由于server2008需要设置很多东西,不然用起来很不爽,就说IE吧,每次随便打开一个网页都要弹出n多窗口出来叫你添加到信任域里面!太烦人了![下面有解决 ...
- Flask 的馈赠
我们在之前用过装饰器 但是在装饰很多函数的时候 那么这些函数的名字都是装饰器内部函数的名字了怎么办呢? django中有functools进行保留你的函数名字保存 flask也可以使用functo ...
- 可以简易设置文字内边距的EdgeInsetsLabel
可以简易设置文字内边距的EdgeInsetsLabel 最终效果: 源码: EdgeInsetsLabel.h 与 EdgeInsetsLabel.m // // EdgeInsetsLabel.h ...
- mac 程序 Access-JSON-Data
mac 程序 Access-JSON-Data 说明 直接将JSON数据生成文件,便于调试. 效果图 源码 https://github.com/YouXianMing/Create-JSON-Mod ...
- 【原创】Qt 使用ODBC driver 连接SQL Server
最近在做数据库的课程设计.第一个需要解决的问题是使用什么工具来实现这个系统.经过一番资料查找,决定使用SQL Server Express 2012作为服务器,使用Qt作为编写客户端程序语言.问题是c ...
- opengl redbook Fatal Error in XXXXXX pixel format with necessary capabilities not found
随便运行书中的一个程序aaindex.c时出现了下面的错误:pixel format with necessary capabilities not found 解决方法: http://www.ed ...