繁体   English   中英

使用npgsql通过datagridview将数据更新到Postgres数据库

[英]update data to postgres database through datagridview using npgsql

我无法使用npgsql从datagridview edit更新数据。

protected NpgsqlConnection dataconnect = new NpgsqlConnection("Server=127.0.0.1;Port=5432;User Id=cpdatabase;Password=5622;Database=cpdb;");
protected DataSet dset = new DataSet("maindata.sessions");
protected NpgsqlDataAdapter NpAdapter = new NpgsqlDataAdapter();

下面的代码在表单加载时将数据加载到datagridview中。

string crossref = "Select * from maindata.sessions where \"DATE:\" BETWEEN '03-01-2014' and '04-01-2014'";
NpAdapter.SelectCommand = new NpgsqlCommand(crossref,this.dataconnect);
NpAdapter.Fill(dset, "sessions");
var dtsource = dset.Tables["sessions"];
dataGridView1.DataSource = dtsource;

以下是我无法执行的更新代码:

private void saveToolStripMenuItem_Click(object sender, EventArgs e)
{
     dtsource.AcceptChanges();
}

我尝试了许多不同的方法来更新它,但对我来说却无济于事。

我找到了解决方案。

NpgsqlCommand command = new NpgsqlCommand("UPDATE sessions SET column1 = @value1, column2 = @value2  WHERE column1 = @value1", this.connection);
command.Parameters.Add("@value1", NpgsqlTypes.NpgsqlDbType.Integer, 12, "column1");
command.Parameters.Add("@value2", NpgsqlTypes.NpgsqlDbType.Varchar, 50, "column2");
NpgsqlParameter parameter = command.Parameters.Add("@oldvalue1", NpgsqlTypes.NpgsqlDbType.Integer, 12, "column1");
parameter.SourceVersion = DataRowVersion.Original;
NpAdapter.UpdateCommand = command;
NpAdapter.Update(dset,"sessions");

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM