我正在尝试使用 NpgsqlCommandBuilder 自动生成 SQL 命令以对数据集执行插入/更新/删除操作。数据集将绑定到数据网格(我真的不知道该怎么做)。所以我不会手动编辑数据集。尝试自动生成sql命令时,调用NpgsqlDataAdaptor.Update()时会抛出异常
这是 db 表的架构:
tblFee
{
character(4) dest,
character(4) id,
character(2) indicator,
double fee
}
the primary key is (dest, id, indicator)
这是我的代码:
string m_strConnection = "Server=192.168.253.20;Port=5432;User Id=alex;Password=asdf;Database=mydb;";
DataSet m_ds = new DataSet("EcnFeeData");
NpgsqlConnection m_conn = new NpgsqlConnection(m_strConnection);
NpgsqlDataAdapter m_dAdapter = new NpgsqlDataAdapter();
m_dAdapter.SelectCommand = new NpgsqlCommand("SELECT * FROM \"tblFee\"", m_conn);
NpgsqlCommandBuilder builder = new NpgsqlCommandBuilder(m_dAdapter);
m_conn.Open();
m_dAdapter.Fill(m_ds, "tblFee");
m_ds.Tables[0].Rows[0]["fee"] = 1;
builder.GetUpdateCommand();
m_dAdapter.Update(m_ds, "tblFee");
在执行最后一行时会抛出此异常:
A first chance exception of type 'System.InvalidOperationException' occurred in System.Data.dll
Additional information: Update requires a valid UpdateCommand when passed DataRow collection with modified rows.
尝试插入行时也会发生同样的事情,只是异常消息不同:
Additional information: Update requires a valid InsertCommand when passed DataRow collection with new rows.
最后一个代码示例尝试使用 NpgsqlCommandBuilder,就像在 .NET 4.0 文档提供的示例中使用 SqlCommandBuilder 一样。Npgsql 文档没有提供这样做的示例。NpgsqlCommandBuilder 不支持此功能吗?我希望 NpgsqlCommandBuilder 自动生成 sql 命令,但如果它不起作用,那么我将不得不手动创建它们。
在按照 Npgsql 文档中手动生成 InsertCommand 的示例之后,我能够让它工作,但我不知道如何为 UpdateCommand 执行此操作。这是我用来创建 InsertCommand 的代码:
m_dAdapter.InsertCommand = new NpgsqlCommand("insert into \"tblFee\" (dest, id, indicator, fee) values (:a, :b, :c, :d)", m_conn);
m_dAdapter.InsertCommand.Parameters.Add(new NpgsqlParameter("a", DbType.AnsiStringFixedLength));
m_dAdapter.InsertCommand.Parameters.Add(new NpgsqlParameter("b", DbType.AnsiStringFixedLength));
m_dAdapter.InsertCommand.Parameters.Add(new NpgsqlParameter("c", DbType.AnsiStringFixedLength));
m_dAdapter.InsertCommand.Parameters.Add(new NpgsqlParameter("d", DbType.Double));
m_dAdapter.InsertCommand.Parameters[0].Direction = ParameterDirection.Input;
m_dAdapter.InsertCommand.Parameters[1].Direction = ParameterDirection.Input;
m_dAdapter.InsertCommand.Parameters[2].Direction = ParameterDirection.Input;
m_dAdapter.InsertCommand.Parameters[3].Direction = ParameterDirection.Input;
m_dAdapter.InsertCommand.Parameters[0].SourceColumn = "dest";
m_dAdapter.InsertCommand.Parameters[1].SourceColumn = "id";
m_dAdapter.InsertCommand.Parameters[2].SourceColumn = "indicator";
m_dAdapter.InsertCommand.Parameters[3].SourceColumn = "fee";
m_dAdapter.InsertCommand.Connection = m_conn;
这是我尝试执行 UpdateCommand,但它不起作用:
m_dAdapter.UpdateCommand = new NpgsqlCommand("update \"tblFee\" set dest = :a, id = :b, indicator = :c, fee = :d where dest = :e and id = :f and indicator = :g");
m_dAdapter.UpdateCommand.Parameters.Add(new NpgsqlParameter("a", DbType.AnsiStringFixedLength));
m_dAdapter.UpdateCommand.Parameters.Add(new NpgsqlParameter("b", DbType.AnsiStringFixedLength));
m_dAdapter.UpdateCommand.Parameters.Add(new NpgsqlParameter("c", DbType.AnsiStringFixedLength));
m_dAdapter.UpdateCommand.Parameters.Add(new NpgsqlParameter("d", DbType.Double));
m_dAdapter.UpdateCommand.Parameters.Add(new NpgsqlParameter("e", DbType.AnsiStringFixedLength));
m_dAdapter.UpdateCommand.Parameters.Add(new NpgsqlParameter("f", DbType.AnsiStringFixedLength));
m_dAdapter.UpdateCommand.Parameters.Add(new NpgsqlParameter("g", DbType.AnsiStringFixedLength));
m_dAdapter.UpdateCommand.Parameters[0].Direction = ParameterDirection.Input;
m_dAdapter.UpdateCommand.Parameters[1].Direction = ParameterDirection.Input;
m_dAdapter.UpdateCommand.Parameters[2].Direction = ParameterDirection.Input;
m_dAdapter.UpdateCommand.Parameters[3].Direction = ParameterDirection.Input;
m_dAdapter.UpdateCommand.Parameters[4].Direction = ParameterDirection.Input;
m_dAdapter.UpdateCommand.Parameters[5].Direction = ParameterDirection.Input;
m_dAdapter.UpdateCommand.Parameters[6].Direction = ParameterDirection.Input;
m_dAdapter.UpdateCommand.Parameters[0].SourceColumn = "dest";
m_dAdapter.UpdateCommand.Parameters[1].SourceColumn = "id";
m_dAdapter.UpdateCommand.Parameters[2].SourceColumn = "indicator";
m_dAdapter.UpdateCommand.Parameters[3].SourceColumn = "fee";
m_dAdapter.UpdateCommand.Parameters[4].SourceColumn = "dest_orig";
m_dAdapter.UpdateCommand.Parameters[5].SourceColumn = "id_orig";
m_dAdapter.UpdateCommand.Parameters[6].SourceColumn = "indicator_orig";
m_dAdapter.UpdateCommand.Connection = m_conn;
任何帮助将非常感激。
亚历克斯