2013年9月11日 星期三

Npgsql (.Net Data Provider) 建置及測試

上回介紹需付費的 PGNP 後, 這回介紹另一個 .Net Data Provider, 呵呵, 人客呀~~是不用錢的~~佛心來的.
程式套件的配置及撰寫更是容易的, 首先下載程式套件, 下載網址為 http://pgfoundry.org/frs/download.php/3067/Npgsql.Provider.zip


  • 在解開程式套件後, 我們只需要選用編譯好的套件,  Npgsql.dll 及 Mono.Security.dll




















  • 開啟一個新專案, 加入程式套件,  選取 project -> add reference , 並且選取Npgsql.dll

















  • 接下來就可以寫程式了, 程式範例如下


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Npgsql;

namespace ConsoleApplication3
{
    class Program
    {
        static void Main(string[] args)
        {
            NpgsqlConnection conn = new NpgsqlConnection("Server=127.0.0.1;Port=5432;User Id=postgres;Password=post123;Database=postgres;");
            conn.Open();

            NpgsqlCommand command = new NpgsqlCommand("select * from pg_class", conn);
           
            try
            {
                NpgsqlDataReader dr = command.ExecuteReader();
                while (dr.Read())
                {
                    for (int i = 0; i < dr.FieldCount; i++)
                    {
                        Console.Write("{0} \t", dr[i]);
                    }
                    Console.WriteLine();
                }
            }

            finally
            {
                conn.Close();
            }
        }
    }
}


怎樣....夠簡單吧~
當然這只是個簡單的lab, 若您需要更多的範例, 可以參考 

沒有留言:

張貼留言

HDB 擴充套件 PXF (Pivotal Extension Framework) 應用

在Hadoop生態圈裡, 雖然擁有眾多生態系統工具, 但大多用戶仍然需要的是SQL的操作介面, Pivotal HDB 提供一個在Hadoop上運行的標準SQL引擎介面, 箇中好處開發者最能感受, 但本篇重點不在這, 哈哈~ 主要分享在HDB上可以輕易整合外部資料的應用. ...