2014-04-04 163 views
0

我無法使用npgsql從datagridview編輯更新我的數據。使用npgsql將數據更新到postgres數據庫通過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(); 
} 

我已經試過很多不同的方式來更新它,但沒有爲我工作。

回答

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"); 
相關問題