C#访问postgresql数据库
using Npgsql;
using Npgsql;
/* 用指定的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();
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();
}
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 = "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();
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 = "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();
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。
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。