最近开始做C#的DotNet的工作,因为对PostgreSQL数据库比较有兴趣,所以自己研究了一下如何访问PostgreSQL的
数据库的问题。
1、为了访问PostgreSQL数据库,需要从pgfoundry网站,下载Npgsql .Net Data Provider for Postgresql的组件。
注:因为使用的是DotNet3.5,所以下载了Npgsql2.0.6-bin-ms.net3.5sp1.zip。
2、解压缩zip文件,把解压缩的目录中的Npgsql.dll、Mono.Security.dll复制到C#工程的obj目录下,然后在VS2008中
,把 Npgsql.dll文件加入到References。
3、在需要使用Npgsql的C#文件头,加入如下的using语句。
view plaincopy to clipboardprint?
using Npgsql;
using Npgsql;
using Npgsql;
using Npgsql;
4、创建一个PostgreSQL的数据库连接
view plaincopy to clipboardprint?
/* 用指定的connectionString,去实例化一个NpsqlConnection的对象*/
string connectionString = "Server=127.0.0.1;Port=5432;User Id=test;Password=test;Database=testdb;"
NpgsqlConnection conn = new NpgsqlConnection(string connectionString);
// 打开一个数据库连接,在执行相关SQL之前调用
conn.Open();
//关闭一个数据库连接,在执行完相关SQL之后调用
conn.Close();
/* 用指定的connectionString,去实例化一个NpsqlConnection的对象*/
string connectionString = "Server=127.0.0.1;Port=5432;User Id=test;Password=test;Database=testdb;"
NpgsqlConnection conn = new NpgsqlConnection(string connectionString);
// 打开一个数据库连接,在执行相关SQL之前调用
conn.Open();
//关闭一个数据库连接,在执行完相关SQL之后调用
conn.Close();
/* 用指定的connectionString,去实例化一个NpsqlConnection的对象*/
string connectionString = "Server=127.0.0.1;Port=5432;User Id=test;Password=test;Database=testdb;"
NpgsqlConnection conn = new NpgsqlConnection(string connectionString);
// 打开一个数据库连接,在执行相关SQL之前调用
conn.Open();
//关闭一个数据库连接,在执行完相关SQL之后调用
conn.Close();
/* 用指定的connectionString,去实例化一个NpsqlConnection的对象*/
string connectionString = "Server=127.0.0.1;Port=5432;User Id=test;Password=test;Database=testdb;"
NpgsqlConnection conn = new NpgsqlConnection(string connectionString);
// 打开一个数据库连接,在执行相关SQL之前调用
conn.Open();
//关闭一个数据库连接,在执行完相关SQL之后调用
conn.Close();
5、用NpgsqlCommand.ExecuteScalar()方法获得唯一值的检索结果
view plaincopy to clipboardprint?
try
{
string sql = "select count(*) from test";
conn.Open();
NpgSqlCommand objCommand = new NpgSqlCommand(sql, conn);
int count = Convert.ToInt32(objCommand.ExecuteScalar());
}
finally
{
conn.Close();
}
try
{
string sql = "select count(*) from test";
conn.Open();
NpgSqlCommand objCommand = new NpgSqlCommand(sql, conn);
int count = Convert.ToInt32(objCommand.ExecuteScalar());
}
finally
{
conn.Close();
}
try
{
string sql = "select count(*) from test";
conn.Open();
NpgSqlCommand objCommand = new NpgSqlCommand(sql, conn);
int count = Convert.ToInt32(objCommand.ExecuteScalar());
}
finally
{
conn.Close();
}
try
{
string sql = "select count(*) from test";
conn.Open();
NpgSqlCommand objCommand = new NpgSqlCommand(sql, conn);
int count = Convert.ToInt32(objCommand.ExecuteScalar());
}
finally
{
conn.Close();
}
6、用NpgsqlCommand.ExecuteReader() 方法获得一个结果集的检索结果(conn的Open和Close调用省略)
view plaincopy to clipboardprint?
string sql = "select * from test";
NpgsqlCommand objCommand = new NpgsqlCommand(sql,conn);
NpgsqlDataReader dr = command.ExecuteReader();
while(dr.Read())
{
for (i = 0; i < dr.FieldCount; i++)
{
Console.Write("{0} \t", dr[i]); //获得字段名
}
int testId = dr["id"]; // 获得指定字段的值。(id是test表的一个字段)
……
Console.WriteLine();
}
dr.Close();
string sql = "select * from test";
NpgsqlCommand objCommand = new NpgsqlCommand(sql,conn);
NpgsqlDataReader dr = command.ExecuteReader();
while(dr.Read())
{
for (i = 0; i < dr.FieldCount; i++)
{
Console.Write("{0} \t", dr[i]); //获得字段名
}
int testId = dr["id"]; // 获得指定字段的值。(id是test表的一个字段)
……
Console.WriteLine();
}
dr.Close();
string sql = "select * from test";
NpgsqlCommand objCommand = new NpgsqlCommand(sql,conn);
NpgsqlDataReader dr = command.ExecuteReader();
while(dr.Read())
{
for (i = 0; i < dr.FieldCount; i++)
{
Console.Write("{0} \t", dr[i]); //获得字段名
}
int testId = dr["id"]; // 获得指定字段的值。(id是test表的一个字段)
……
Console.WriteLine();
}
dr.Close();
string sql = "select * from test";
NpgsqlCommand objCommand = new NpgsqlCommand(sql,conn);
NpgsqlDataReader dr = command.ExecuteReader();
while(dr.Read())
{
for (i = 0; i < dr.FieldCount; i++)
{
Console.Write("{0} \t", dr[i]); //获得字段名
}
int testId = dr["id"]; // 获得指定字段的值。(id是test表的一个字段)
……
Console.WriteLine();
}
dr.Close();
7、用NpgsqlCommand.ExecuteNonQuery()方法对指定表进行添加、更新和删除一条记录的操作
添加记录:
view plaincopy to clipboardprint?
string sql = "insert test values (1,200)";
NpgsqlCommandobjCommand = new NpgsqlCommand(sql, conn);
objCommand.ExecuteNonQuery();
string sql = "insert test values (1,200)";
NpgsqlCommandobjCommand = new NpgsqlCommand(sql, conn);
objCommand.ExecuteNonQuery();
string sql = "insert test values (1,200)";
NpgsqlCommandobjCommand = new NpgsqlCommand(sql, conn);
objCommand.ExecuteNonQuery();
string sql = "insert test values (1,200)";
NpgsqlCommandobjCommand = new NpgsqlCommand(sql, conn);
objCommand.ExecuteNonQuery();
更新记录
view plaincopy to clipboardprint?
sql = "update test set price=300 where id=1";
NpgsqlCommandobjCommand = new NpgsqlCommand(sql, conn);
objCommand.ExecuteNonQuery();
sql = "update test set price=300 where id=1";
NpgsqlCommandobjCommand = new NpgsqlCommand(sql, conn);
objCommand.ExecuteNonQuery();
sql = "update test set price=300 where id=1";
NpgsqlCommandobjCommand = new NpgsqlCommand(sql, conn);
objCommand.ExecuteNonQuery();
sql = "update test set price=300 where id=1";
NpgsqlCommandobjCommand = new NpgsqlCommand(sql, conn);
objCommand.ExecuteNonQuery();
删除记录
view plaincopy to clipboardprint?
sql = "delete from test where id=1";
NpgsqlCommandobjCommand = new NpgsqlCommand(sql, conn);
objCommand.ExecuteNonQuery();
sql = "delete from test where id=1";
NpgsqlCommandobjCommand = new NpgsqlCommand(sql, conn);
objCommand.ExecuteNonQuery();
sql = "delete from test where id=1";
NpgsqlCommandobjCommand = new NpgsqlCommand(sql, conn);
objCommand.ExecuteNonQuery();
sql = "delete from test where id=1";
NpgsqlCommandobjCommand = new NpgsqlCommand(sql, conn);
objCommand.ExecuteNonQuery();
8、用NpgsqlDataAdapter.Fill方法,把检索的结果集放到DataSet object中,这样可以使用DataSet object设置
DotNet的DataGridView控件的DataSource属性,这样在DataGridView中显示从表中取出的所有记录。
view plaincopy to clipboardprint?
string sql = "select id,price from test";
DataSet ds = new DataSet();
NpgsqlDataAdapter objAdapter = new NpgsqlDataAdapter(sql, conn);
objAdapter.Fill(ds, "a"); //“a”这个表是自定义的
dgvBaseResult.DataSource = ds.Tables["a"]; //dgvBaseResult是DataGridView的一个Object。
string sql = "select id,price from test";
DataSet ds = new DataSet();
NpgsqlDataAdapter objAdapter = new NpgsqlDataAdapter(sql, conn);
objAdapter.Fill(ds, "a"); //“a”这个表是自定义的
dgvBaseResult.DataSource = ds.Tables["a"]; //dgvBaseResult是DataGridView的一个Object。
string sql = "select id,price from test";
DataSet ds = new DataSet();
NpgsqlDataAdapter objAdapter = new NpgsqlDataAdapter(sql, conn);
objAdapter.Fill(ds, "a"); //“a”这个表是自定义的
dgvBaseResult.DataSource = ds.Tables["a"]; //dgvBaseResult是DataGridView的一个Object。
string sql = "select id,price from test";
DataSet ds = new DataSet();
NpgsqlDataAdapter objAdapter = new NpgsqlDataAdapter(sql, conn);
objAdapter.Fill(ds, "a"); //“a”这个表是自定义的
dgvBaseResult.DataSource = ds.Tables["a"]; //dgvBaseResult是DataGridView的一个Object。
注:SQL语句设置也可以用下面的语句 objAdapter.SelectCommand.CommandText = sql;