使用npgsql从datagridview更新不起作用

时间:2014-04-08 03:27:26

标签: c# datagridview npgsql

我在使用Npgsql通过datagridview更新数据时遇到问题。我想从datagridview动态更新我的表。

以下是我要更新的代码:

NpAdapter.UpdateCommand = new NpgsqlCommand("update sessions set \"Visit Number:\" = :visit_num, \"ID:\" = :id, \"ENTERED BY:\" = :entered  " +
"where \"Visit Number:\" = :visit_num_old, \"ID:\" = :id_old, \"ENTERED BY:\" = :entered_old", this.dataconnect);
NpAdapter.UpdateCommand.Parameters.Add(new NpgsqlParameter("visit_num", DbType.Int32, 10) { Direction = ParameterDirection.Input, SourceColumn = "Visit Number:" });
NpAdapter.UpdateCommand.Parameters.Add(new NpgsqlParameter("id", DbType.AnsiStringFixedLength, 50) { Direction = ParameterDirection.Input, SourceColumn = "ID:" });
NpAdapter.UpdateCommand.Parameters.Add(new NpgsqlParameter("entered", DbType.AnsiStringFixedLength, 50) { Direction = ParameterDirection.Input, SourceColumn = "ENTERED BY:" });

NpAdapter.Update(dset, "sessions");

我不知道我做错了什么。有人可以帮帮我吗。我试图弄清楚这个很长一段时间。

1 个答案:

答案 0 :(得分:0)

这是我找到的解决方案。

NpgsqlCommand command = new NpgsqlCommand("UPDATE sessions SET \"Visit Number:\" = @visitnum,\"ENTERED BY:\" = @enteredby  WHERE \"Visit Number:\" = @visitnum", this.connection);

command.Parameters.Add("@visitnum", NpgsqlTypes.NpgsqlDbType.Integer, 12, "Visit Number:");
command.Parameters.Add("@enteredby", NpgsqlTypes.NpgsqlDbType.Varchar, 50, "ENTERED BY:");
NpgsqlParameter parameter = command.Parameters.Add("@oldvisitnum", NpgsqlTypes.NpgsqlDbType.Integer, 12, "Visit Number:");

parameter.SourceVersion = DataRowVersion.Original;
NpAdapter.UpdateCommand = command;
NpAdapter.Update(dset,"sessions");
MessageBox.Show("Data Updated!!!");