Hiện nay công nghệ .NET hầu như đã cung cấp cho chúng ta nhiều driver để kết nối đến nhiều loại Database khác nhau, từ Oracle, SQL Server, Access… Trong số đó có một Hệ quản trị CSDL mã nguồn mở và được sử dụng khá nhiều nhưng .NET quên support đó là Postgres SQL Database. May thay đã có cộng đồng mã nguồn mở phát triển driver và hỗ trợ
1 – Trước tiên download Postgres Database tại đây :
2 – Download database Driver tại đây
3 – Sau khi xả nén thư viện NPSQL chúng ta sẽ thấy tập tin “Npgsql.dll”
4 – Tạo project mới trên VS2k5, VS2k8, VS2010
add reference “Npgsql.dl” vào project
và thêm vài thao tác cơ bản như làm việc trên MSSQL server
using Npgsql;
NpgsqlConnection con;
string connectionString =
“Server=localhost;” +
“Database=northwind;” +
“User ID=postgres;” +
“Password=yourpassword”;
private void button1_Click(object sender, EventArgs e)
{
con = new NpgsqlConnection(connectionString);
try
{
con.Open();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void button2_Click(object sender, EventArgs e)
{
NpgsqlDataAdapter da = new NpgsqlDataAdapter();
NpgsqlCommand cmd = new NpgsqlCommand(“select * from table_name”);
da.SelectCommand = cmd;
cmd.Connection = con;
DataSet ds = new DataSet();
da.Fill(ds, “table”);
dataGridView1.DataSource = ds.Tables[0];
}
Hiện nay công nghệ .NET hầu như đã cung cấp cho chúng ta nhiều driver để kết nối đến nhiều loại Database khác nhau, từ Oracle, SQL Server, Access… Trong số đó có một Hệ quản trị CSDL mã nguồn mở và được sử dụng khá nhiều nhưng .NET quên support đó là Postgres SQL Database. May thay đã có cộng đồng mã nguồn mở phát triển driver và hỗ trợ
http://npgsql.projects.postgresql.org/index.html
1 – Trước tiên download Postgres Database tại đây : http://www.enterprisedb.com/products/pgdownload.do#windows
2 – Download database Driver tại đây http://pgfoundry.org/frs/?group_id=1000140
3 – Sau khi xả nén thư viện NPSQL chúng ta sẽ thấy tập tin “Npgsql.dll”
4 – Tạo project mới trên VS2k5, VS2k8, VS2010 add reference “Npgsql.dl” vào project
và thêm vài thao tác cơ bản như làm việc trên MSSQL server
using Npgsql; NpgsqlConnection con; string connectionString = “Server=localhost;” + “Database=northwind;” + “User ID=postgres;” + “Password=yourpassword”; private void button1_Click(object sender, EventArgs e) { con = new NpgsqlConnection(connectionString); try { con.Open(); } catch (Exception ex) { MessageBox.Show(ex.Message); } }
private void button2_Click(object sender, EventArgs e) { NpgsqlDataAdapter da = new NpgsqlDataAdapter(); NpgsqlCommand cmd = new NpgsqlCommand(“select * from table_name”); da.SelectCommand = cmd; cmd.Connection = con; DataSet ds = new DataSet(); da.Fill(ds, “table”); dataGridView1.DataSource = ds.Tables[0]; }