【问题标题】:update data to postgres database through datagridview using npgsql使用 npgsql 通过 datagridview 将数据更新到 postgres 数据库
【发布时间】:2014-04-04 05:31:20
【问题描述】:

我无法使用 npgsql 从 datagridview 编辑更新我的数据。

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();
}

我尝试了很多不同的方法来更新它,但对我来说没有任何效果。

【问题讨论】:

    标签: c# datagridview npgsql


    【解决方案1】:

    我找到了解决办法。

    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");
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-02-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-27
      • 2015-05-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多