设为首页 收藏本站
查看: 961|回复: 0

[经验分享] PostgreSQL接口编程一:OLEDB--PGNP驱动

[复制链接]

尚未签到

发表于 2016-11-20 09:55:43 | 显示全部楼层 |阅读模式
 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.执行结果如下:
  
DSC0000.png
 

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-302775-1-1.html 上篇帖子: postgresql-从子查询中构造数组 下篇帖子: PostgreSQL的数据存储(十五)---数据存储
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表