更新命令在传递DataRow时需要有效的insertCommand

时间:2013-08-20 07:24:21

标签: c# .net postgresql

执行以下代码时遇到问题。使用Ngpsql数据适配器。该函数采用数据表创建和更新查询

参数字符串值是动态生成的更新值,然后与本地变量字符串查询连接,并创建一个类似的查询 UPDATE 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,我正在更新行,那么为什么要求插入命令请帮助

谢谢

1 个答案:

答案 0 :(得分:1)

当您调用NgAdapter.Update时,它会根据行的rowState为DataTable的每一行调用适当的命令。如果插入了rowState,则调用InsertCommand,如果rowState为Deleted,则调用DeleteCommand,因此您必须在DataAdapter中定义所有命令