`

PostgreSQL接口编程一:OLEDB--PGNP驱动

阅读更多

 1介绍

PGNP Native Provider是以OLEDB接口访问PostgreSQL数据库的驱动程序。以下简称PGNPPostgreSQL数据库以下简称pg。

 

PGNPpg数据库的OLEDB接口驱动程序,他介于微软OLEDBADO.NETOLEDBPostgreSQL libpq库接口之间,实现了大多数OLEDB接口,并使用pglibpq访问pg数据库。PGNP可以为.NETNATIVE32/64位应用程序提供支持访问pg

 

这是一个商业软件,Business license390$

http://pgoledb.com/可以下载使用版PGNP-1.3.0.2251.exe

 

 

2安装

点击PGNP-1.3.0.2251.exe开始安装,按下一步即可完成。

 

 

3psql连到postgreSQLpsql中命令 \i sql_script_file_name即可

sql_script_file_name文件中内容如下:

 

--建模式、表、插入记录、建函数

-- Create schema for PGNP samples

 

-- DROP SCHEMA pgnp_samples;

CREATE SCHEMA pgnp_samples AUTHORIZATION postgres;

GRANT ALL ON SCHEMA pgnp_samples TO postgres;

 

SET search_path='pgnp_samples';

 

--删除photo类型字段

-- DROP TABLE pgnp_samples.contact;

CREATE TABLE contact

(

  contact_id bigint not null,

  fname character varying(64),

  lname character varying(64),

  revenue double precision,

--  photo lo,

  created_date timestamp without time zone NOT NULL DEFAULT now(),

  modified_date timestamp without time zone NOT NULL DEFAULT now(),

  CONSTRAINT pk_contact_id PRIMARY KEY (contact_id)

);

 

INSERT INTO contact(contact_id, fname, lname, revenue) VALUES (1, 'James', 'Smith', 20000.0), (2, 'Sue', 'McMartin', 35000.0);

 

-- DROP TABLE pgnp_samples."group";

CREATE TABLE "group"

(

  group_id bigint not null,

  group_name character varying(128),

  region uuid,

  created_date timestamp without time zone NOT NULL DEFAULT now(),

  modified_date timestamp without time zone NOT NULL DEFAULT now(),

  CONSTRAINT pk_group_id PRIMARY KEY (group_id)

);

 

INSERT INTO "group"(group_id, group_name, region) VALUES (1, 'EMEA', '00000000000000000000000000000001'), (2, 'NA', '00000000000000000000000000000002');

 

-- DROP FUNCTION pgnp_samples.ContactsLike(character varying(64));

CREATE OR REPLACE FUNCTION ContactsLike(character varying(64)) RETURNS bigint AS 'SELECT count(1) FROM contact WHERE lname ~~* $1;' LANGUAGE SQL;

 

--as后边加空格

 DROP FUNCTION pgnp_samples.sptest2(integer);

CREATE OR REPLACE FUNCTION pgnp_samples.sptest2(IN integer)

  RETURNS TABLE(f1 integer, f2 text) AS 

$BODY$

  SELECT $1, CAST($1 AS text) || ' is text'

  UNION ALL

  SELECT $1*2, CAST($1 AS text) || ' is text too'    

$BODY$

  LANGUAGE 'sql';

 

   

-- DROP FUNCTION pgnp_samples.GetMultipleResults();

CREATE OR REPLACE FUNCTION GetMultipleResults() RETURNS SETOF refcursor AS

'DECLARE refContact refcursor; refGroup refcursor;

BEGIN

  OPEN refContact FOR SELECT * FROM contact; RETURN NEXT refContact;

  OPEN refGroup FOR SELECT * FROM "group"; RETURN NEXT refGroup;

  RETURN;

END;' LANGUAGE plpgsql;

 

-- DROP TABLE arrays

CREATE TABLE arrays

(

  id serial NOT NULL,

  test1d character varying(15)[],

  test2d numeric(7,3)[][],

  test3d integer[][][],

  CONSTRAINT pk_arrays_id PRIMARY KEY (id)

);

 

INSERT INTO arrays(test1d, test2d, test3d)

VALUES('{"New York", Paris, Bejing}', '{{3000.146,17.101,0.000,0.667},{3.14159,2.71,-1.778,128.3}}',

 '{{3,16,9,22,15,0,100},{20,8,21,14,2,0,100},{7,25,13,1,19,0,100},{24,12,5,18,6,0,100},{11,4,17,10,23,0,100}}')

 

4. C#代码如下:

    public partial class Program

    {

        static readonly string connStr = "Provider=PGNP.1;Data Source=localhost;Initial Catalog=postgres;User ID=postgres;Password=12345;Extended Properties=\"NESTED_TRANS=ON;SEARCH_PATH=pgnp_samples,public;PORT=5432\"";

        static int Main(string[] args)

        {

            int error_count = 0;

 

            error_count += Get_Arrays();

 

 

            if (error_count > 0)

                ConsoleWriteError("Errors count: " + error_count, "");

            else

                ConsoleWriteSuccess("All samples ran successfully!");

            return error_count;

        }

 

        static void ConsoleWriteSampleHeader(String sample_hdr)

        {

            Console.ForegroundColor = ConsoleColor.Blue;

            

            Console.WriteLine(sample_hdr);

        }

 

        static void ConsoleWriteMessage(String msg)

        {

            ConsoleWriteMessage(msg, "    ");

        }

 

        static void ConsoleWriteMessage(String msg, String blank)

        {

            Console.ForegroundColor = ConsoleColor.Gray;

 

            Console.WriteLine(blank + msg);

        }

 

        static void ConsoleWriteError(String error, String optionalStmt)

        {

            Console.ForegroundColor = ConsoleColor.Red;

 

            Console.WriteLine("** " + error);

 

            if (optionalStmt.Length > 0)

            {

                Console.ForegroundColor = ConsoleColor.Gray;

                Console.WriteLine("   Last stmt: " + optionalStmt);

            }

        }

 

        static void ConsoleWriteSuccess(String success_msg)

        {

            Console.ForegroundColor = ConsoleColor.Green;

 

            Console.WriteLine(success_msg);

        }

 

        static public int Get_Arrays()

        {

            String lastStmt = "";

 

            try

            {

                OleDbConnection conn = new OleDbConnection(connStr);

                conn.Open();

 

                // Read records from pgnp_samples.contact table.

                ConsoleWriteSampleHeader("Reading array elements from database.");

 

                OleDbCommand cmd = conn.CreateCommand();

                cmd.CommandText = lastStmt = "SELECT test1d, test2d, test3d FROM arrays";

 

                OleDbDataReader dr = cmd.ExecuteReader();

 

                while (dr.Read())

                {

                    for (int fieldIndex = 0; fieldIndex < 3; fieldIndex++)

                    {

                        if (dr.IsDBNull(fieldIndex))

                        {

                            ConsoleWriteMessage(String.Format("{0}: [NULL]", dr.GetName(fieldIndex)));

                            continue;

                        }

                        PrintArrayElements(dr.GetName(fieldIndex), dr.GetValue(fieldIndex) as Array);

                    }

                }

            }

            catch (Exception ex)

            {

                ConsoleWriteError(ex.Message, lastStmt);

                return 1;

            }

            return 0;

        }

 

        static public void PrintArrayElements(string fieldName, Array field)

        {

            ConsoleWriteMessage(fieldName + ":", "  ");

 

            switch (field.Rank)

            {

                case 1:

                    for (int i = field.GetLowerBound(0); i < field.GetLowerBound(0) + field.GetLength(0); i++)

                    {

                        object x = field.GetValue(i);

                        ConsoleWriteMessage(String.Format("{0}>{1}", i, x));

                    }

                    break;

 

                case 2:

                    for (int j = field.GetLowerBound(1); j < field.GetLowerBound(1) + field.GetLength(1); j++)

                    {

                        for (int i = field.GetLowerBound(0); i < field.GetLowerBound(0) + field.GetLength(0); i++)

                        {

                            object x = field.GetValue(i, j);

                            ConsoleWriteMessage(String.Format("({0},{1}) > {2}", i, j, x));

                        }

                    }

                    break;

 

                case 3:

                    for (int k = field.GetLowerBound(2); k < field.GetLowerBound(2) + field.GetLength(2); k++)

                    {

                        for (int j = field.GetLowerBound(1); j < field.GetLowerBound(1) + field.GetLength(1); j++)

                        {

                            for (int i = field.GetLowerBound(0); i < field.GetLowerBound(0) + field.GetLength(0); i++)

                            {

                                object x = field.GetValue(i, j, k);

                                ConsoleWriteMessage(String.Format("({0},{1},{2}) > {3}", i, j, k, x));

                            }

                        }

                    }

                    break;

            }

        }

    }

 

5.执行结果如下:



 

  • 大小: 15 KB
1
1
分享到:
评论

相关推荐

    OLEDB驱动程序大全 PostgreSQL-OleDB-Provider

    OLEDB(Object Linking and Embedding, Database)驱动程序是一种数据访问接口,它允许应用程序以统一的方式访问多种数据源,包括数据库、文件系统、Web服务等。在本话题中,我们将深入探讨“OLEDB驱动程序大全”中...

    PostgreSQL(postgresql-13.5-1-windows-x64.exe)

    PostgreSQL(postgresql-13.5-1-windows-x64.exe)适用于Windows x86-64 PostgreSQL是一种特性非常齐全的自由软件的对象-关系型数据库管理系统(ORDBMS),是以加州大学计算机系开发的POSTGRES,4.2版本为基础的...

    PostgreSQL(postgresql-14.2-2-windows-x64.exe)

    PostgreSQL(postgresql-14.2-2-windows-x64.exe),适用于Windows系统:PostgreSQL是一种特性非常齐全的自由软件的对象-关系型数据库管理系统(ORDBMS),是以加州大学计算机系开发的POSTGRES,4.2版本为基础的对象...

    timescaledb-postgresql-12_1.7.1-windows-amd64.zip

    1. 解压"timescaledb-postgresql-12_1.7.1-windows-amd64.zip"压缩包,找到并运行"setup.exe",按照向导进行安装。 2. 安装完成后,将解压目录下的"timescaledb"文件夹复制到PostgreSQL的"share\extension"目录下。 ...

    timescaledb-postgresql-13_2.3.0-windows-amd64.zip

    在“timescaledb-postgresql-13_2.3.0-windows-amd64.zip”这个压缩包中,包含了TimescaleDB v2.3.0版本针对PostgreSQL 13的安装文件,适用于Windows 64位系统。 1. **TimescaleDB核心组件解析**: - `timescaledb...

    postgresql-13.2-1-windows-x64.rar

    postgresql-13.2-1-windows-x64.rar postgresql-13.2-1-windows-x64.rar postgresql-13.2-1-windows-x64.rar postgresql-13.2-1-windows-x64.rar postgresql-13.2-1-windows-x64.rar

    postgresql-10.16-2-windows-x64.zip

    此压缩包"postgresql-10.16-2-windows-x64.zip"包含了PostgreSQL 10.16的64位Windows版本,适合在Windows操作系统上搭建数据库服务。 PostgreSQL 10.16是该数据库系统的一个稳定版本,它在前一版本的基础上进行了多...

    postgresql-42.2.2-API文档-中文版.zip

    赠送jar包:postgresql-42.2.2.jar; 赠送原API文档:postgresql-42.2.2-javadoc.jar; 赠送源代码:postgresql-42.2.2-sources.jar; 赠送Maven依赖信息文件:postgresql-42.2.2.pom; 包含翻译后的API文档:...

    postgresql-12.4-1-windows-x64.exe

    postgresql-12.4-1-windows-x64.exe

    postgresql-16.2-1-windows-x64.exe

    PostgreSQL 是一个强大的开源关系型数据库管理系统,其最新版本为16.2,在Windows操作系统上提供了64位的安装程序,文件名为"postgresql-16.2-1-windows-x64.exe"。这个安装程序是专为在Windows环境中运行PostgreSQL...

    postgresql-10.12-1-linux-x64-binaries.tar.gz

    标题中的"postgresql-10.12-1-linux-x64-binaries.tar.gz"指示了这是PostgreSQL 10.12.1版本的Linux 64位二进制文件包,通常用于在Linux环境中安装该数据库系统。 **PostgreSQL 10.12版本的关键特性:** 1. **多...

    PostgreSQL(postgresql14-plpython3-14.1-1PGDG.rhel7.x86_64.rpm)

    PostgreSQL是一种特性非常齐全的自由软件的对象-关系型数据库管理系统(ORDBMS),是以加州大学计算机系开发的POSTGRES,4.2版本为基础的对象关系型数据库管理系统。POSTGRES的许多领先概念只是在比较迟的时候才出现...

    postgresql-12.18-1-windows-x64.exe

    postgresql-12.18-1-windows-x64.exe

    PostgreSQL(postgresql14-llvmjit-14.1-1PGDG.rhel7.x86_64.rpm)

    PostgreSQL是一种特性非常齐全的自由软件的对象-关系型数据库管理系统(ORDBMS),是以加州大学计算机系开发的POSTGRES,4.2版本为基础的对象关系型数据库管理系统。POSTGRES的许多领先概念只是在比较迟的时候才出现...

    postgresql-9.4.15-1-linux-x64.run

    此工具适用于Redhat-6.x、CentOS-6....安装命令:./postgresql-9.4.15-1-linux-x64.run,之后进入图形化界面,可按界面提醒进行一步步操作; 卸载:进入安装目录,运行uninstall-postgresql命令,进入图形化卸载界面。

    LINUX7 安装PG15所需依赖包,libzstd-1.5.2

    yum install -y postgresql15-server --&gt; Finished Dependency Resolution Error: Package: postgresql15-15.0-1PGDG.rhel7.x86_64 (pgdg15) Requires: libzstd &gt;= 1.4.0 Error: Package: postgresql15-server-15.0...

    PM PostgreSQL OLEDB:PostgreSQL OLEDB 提供程序-开源

    标题中的“PM PostgreSQL OLEDB”指的是一个专为 PostgreSQL 设计的 OLE DB(Object Linking and Embedding, Database)接口,允许开发者使用熟悉的 ADO (ActiveX Data Objects) 或其他支持 OLE DB 的技术来与 ...

    postgresql-42.3.1-API文档-中文版.zip

    赠送jar包:postgresql-42.3.1.jar; 赠送原API文档:postgresql-42.3.1-javadoc.jar; 赠送源代码:postgresql-42.3.1-sources.jar; 赠送Maven依赖信息文件:postgresql-42.3.1.pom; 包含翻译后的API文档:...

    postgresql-12.8-1-windows-x64-binaries.zip

    在Windows平台上,PostgreSQL提供了一个64位版本的安装包,如"postgresql-12.8-1-windows-x64-binaries.zip",用于在Windows系统上部署和运行PostgreSQL服务。 **1. 关系型数据库:** 关系型数据库遵循ACID(原子性...

    postgresql15-server-15-alpha_20220417_PGDG.rhel7.x86_64.rpm

    postgresql15-server-15-alpha_20220417_PGDG.rhel7.x86_64.rpm 适用 于CentOS 7

Global site tag (gtag.js) - Google Analytics