.net调用POSTGRE数据库

samool 发布暂无评论9150 阅读

AI摘要

本文介绍了如何在.NET环境下调用PostgreSQL数据库。首先,需要从PostgreSQL官网下载dll文件,并在Visual Studio中新建工程并导入Npgsql.dll文件。文章提供了两个代码示例,第一个示例展示了如何使用存储过程,包括建立数据库连接、开始事务、设置存储过程名称和调用存储过程。第二个示例是一个Web服务,展示了如何连接数据库、执行查询并返回结果。这两个示例为.NET开发者提供了调用PostgreSQL数据库的基本方法。

PostgreSQL For Windows下载:http://www.skycn.com/soft/42297.html
 
PostgreSQL是一个历史悠久的对象关系数据库管理系统,由美国Great Bridge公司开发的,支持几乎所有SQL功能,包括subselects、transactions以及用户自定义类和功能。
 
1,从postgre网站上下载dll文件
   ●http://pgfoundry.org/frs/?group_id=1000140
    从该网站上下载Npgsql2.0.6-bin-ms.net.zip压缩包,解压到本地。
   ●解压目录下..Npgsql2.0.6-bin-ms.netin目录下取出Npgsql.dll文件保存到 某个文件夹下
2,在Visual Studio2005下新建工程
3,将刚才保存好的Npgsql.dll文件导入到新建立的工程的参照设定中
4,代码编写

例子代码1 使用存储过程:
*******************************************************************
//取得数据库连接
NpgsqlConnection conn = new NpgsqlConnection("Server=**IP**;Port=5432;Userid=****;password=****;Database=****");
conn.Open();

//开始一个事物(POSTGRE中存储过程只能在一个事物中完成,不可以像ORACLE中在中途进行commit)
NpgsqlTransaction t = conn.BeginTransaction();
//设定所要调用的存储过程的名称
NpgsqlCommand command = new NpgsqlCommand("csm030202_get_trd_list", conn);
//设定调用对象为存储过程
command.CommandType = CommandType.StoredPro

例子代码2:

using System;
using System.Data;
using System.Web;
using System.Collections;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.ComponentModel;
using Npgsql;

namespace Myorc
{
    ///


    /// Service1 的摘要说明
    ///
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [ToolboxItem(false)]
    public class Service1 : System.Web.Services.WebService
    {

        [WebMethod]
        public DataSet HelloWorld()
        {

            NpgsqlConnection conn = new NpgsqlConnection("Server=127.0.0.1;Port=5432;Userid=postgres;password=root;Database=postgres");
            conn.Open();
            DataSet  table = new DataSet();
            NpgsqlDataAdapter AD = new NpgsqlDataAdapter("select * from pg_cast", conn);
            AD.Fill(table);
            return table;

        }
    }
}

  1. 01SaMDeditor V2.4 更新内容2026-12-02
  2. 02恭喜《轻武爱学习》小程序备案成功!2026-11-24
  3. 03SaView 图阅 1.1.1 更新日志2026-11-18
  4. 04Book2EPUB 1.0.4 更新日志2026-11-18
  5. 05SaView 图阅 一款轻便纯粹的看图工具2026-11-16
  6. 06免费工具Book2EPUB:轻松将电子书转换为 EPUB2026-11-11
  7. 07SaBlog2026 主题功能介绍(v1.1.36)2026-10-26
  8. 08PHP2Shell 重大安全漏洞事件深度分析报告2026-10-23
  9. 09LiteLLM v1.106.0 更新内容总结2026-10-20
  10. 10AI Native 受控软件工程2026-10-12
COMMENTS / 一纸留言

读者留言 0 条

一纸留言,便是山水间的一次相逢。

还没有留言,欢迎写下第一段交流。

WRITE A COMMENT

写下你的留言