程式套件的配置及撰寫更是容易的, 首先下載程式套件, 下載網址為 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, 若您需要更多的範例, 可以參考
沒有留言:
張貼留言