C# 存取PostgreSQL
C# 存取PostgreSQL
元件下載URL: http://pgfoundry.org/projects/npgsql
首先要記得Using Npgsql的東西進來
01.
using Npgsql;//接下來說明連接字串所表示的意義
02.
NpgsqlConnection conn = new NpgsqlConnection(“Server=127.0.0.1;Port=5432;User Id=user;Password=1234;Database=Demo;”);
conn.Open();
03.
NpgsqlCommand command = new NpgsqlCommand(“select * from shop_sale”, conn);
04.
try
{
NpgsqlDataReader reader = command.ExecuteReader();
while (reader.Read())
{
string shop = reader[“shop”].ToString(); ;
string no = reader[“no”].ToString(); ;
Console.WriteLine(shop + “\t” + no);
}
}
finally
{
conn.Close();
}
05.
NpgsqlConnection和NpgsqlCommand 元件必須成雙成對