我无法使用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();
}
我已经尝试了很多不同的方法来更新它,但没有任何方法可以帮助我。
答案 0 :(得分:0)
我找到了解决方案。
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");