传递DataRow时,更新命令需要有效的insertCommand
本文关键字:有效 insertCommand 命令 DataRow 更新 传递 | 更新日期: 2023-09-27 18:28:21
我在执行以下代码时遇到问题。使用Ngpsql数据适配器。该函数获取数据表创建和更新查询
参数字符串值是动态生成的更新值,然后与本地变量字符串查询连接,并创建如下查询更新xyz SET ecode=@ecode,name=@name。。。。。等等
public void TransactionRecording(DataTable DT, string values,bool update)
{
string Query = "";
NpgsqlDataAdapter NgAdapter = new NpgsqlDataAdapter();
if (update)
{
Query += "UPDATE Cards SET " + values;
NgAdapter.UpdateCommand = new NpgsqlCommand(Query, connection);
this.UpdateParameterSetForLocalToCentral(NgAdapter);
}
else
{
Query += "INSERT INTO Cards (ecode, ename, fname, address1, address2, address3, deo, employee, active, birth, type, company, Designation, photo, manual, visit_to, visit_name, vehicle, modifideby, registered, validity, tpl, serial, gateno, level, gate_in, gate_out) VALUES(" + values + ")";
NgAdapter.InsertCommand = new NpgsqlCommand(Query, connection);
this.InsertParameterSetForLocalToCentral(NgAdapter);
}
try {
NgAdapter.Update(DT); }catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
public void UpdateParameterSetForLocalToCentral(NpgsqlDataAdapter NgAdapter)
{
foreach (string name in Enum.GetNames(typeof(LocalToCentralFields)))
{
switch (name)
{
case "ecode":
case "ename":
case "fname":
case "address1":
case "address2":
case "address3":
case "type":
case "company":
case "Designation":
case "visit_to":
case "visit_name":
case "vehicle":
case "modifideby":
case "serial":
case "gateno":
case "level":
case "gate_in":
case "gate_out":
NpParam = NgAdapter.UpdateCommand.Parameters.Add("@"+name,NpgsqlTypes.NpgsqlDbType.Varchar);
break;
case "deo":
case "employee":
case "active":
NpParam = NgAdapter.UpdateCommand.Parameters.Add("@" + name, NpgsqlTypes.NpgsqlDbType.Boolean);
break;
case "birth":
case "registered":
case "validity":
NpParam = NgAdapter.UpdateCommand.Parameters.Add("@" + name, NpgsqlTypes.NpgsqlDbType.Timestamp);
break;
case "photo":
case "tpl":
NpParam = NgAdapter.UpdateCommand.Parameters.Add("@" + name, NpgsqlTypes.NpgsqlDbType.Bytea);
break;
}
NpParam.SourceVersion = DataRowVersion.Original;
NpParam.SourceColumn = "ecode";
}
}
当传递DataRow时,得到这个错误更新命令需要一个有效的InsertCommand,好吧,我正在更新行,那么为什么要求插入命令请帮助
感谢
当您调用NgAdapter.Update
时,它会根据行的rowState为DataTable的每一行调用适当的命令。如果rowState被插入,它会调用InsertCommand
,如果rowState是Deleted,它就会调用DeleteCommand
,因此您必须在DataAdapter 中定义所有命令