查询值和目标字段的数量不同于OLEDB

时间:2018-03-20 14:49:33

标签: c# ms-access oledb oledbcommand

我在尝试将数据插入数据库时​​遇到错误。 这是错误。

  

“System.Data.OleDb.OleDbException:'查询值和目标字段的数量不相同。'”。

    cmd.CommandText = "INSERT INTO [clients]([Firstname],[Lastname],[Email],[Phonenumber],[Address],[CNP],[SeriesCI],[NumberCI],[Sex],[CUI],[J],[Personaldescription],[Temperament],[Provenance],[Registerdata],[Idteam],[NumeAgent])" +
        "Select @f,@l,@e,@ph,@add,@cnp,@ser,@n,@sex,@cui,@j,@pd,@te,@prov,@reg,team.[id] from team where team.[Email]=@email,@agent";


    cmd.Parameters.AddWithValue("@f", materialSingleLineTextField16.Text);
    cmd.Parameters.AddWithValue("@l", materialSingleLineTextField15.Text);
    cmd.Parameters.AddWithValue("@e", materialSingleLineTextField14.Text);
    cmd.Parameters.AddWithValue("@ph", materialSingleLineTextField13.Text);
    cmd.Parameters.AddWithValue("@add", materialSingleLineTextField6.Text);
    cmd.Parameters.AddWithValue("@cnp", materialSingleLineTextField1.Text);
    cmd.Parameters.AddWithValue("@ser", materialSingleLineTextField3.Text);
    cmd.Parameters.AddWithValue("@n", materialSingleLineTextField2.Text);
    cmd.Parameters.AddWithValue("@sex", gender);
    cmd.Parameters.AddWithValue("@cui", materialSingleLineTextField4.Text);
    cmd.Parameters.AddWithValue("@j", materialSingleLineTextField5.Text);
    cmd.Parameters.AddWithValue("@pd", richTextBox2.Text);
    cmd.Parameters.AddWithValue("@te", bunifuDropdown1.selectedValue);
    cmd.Parameters.AddWithValue("@prov", bunifuDropdown2.selectedValue);
    cmd.Parameters.AddWithValue("@reg", DateTime.Now.ToString("dd-MM-yyyy HH: mm:ss"));
    cmd.Parameters.AddWithValue("@email", Form1.Email);
    cmd.Parameters.AddWithValue("@agent", NumeAgent);

在DB(客户端)中,我有17列+ id(但id是自动增量)。 我究竟做错了什么?

clients

team

cmd.CommandText = "INSERT INTO [clients]([Firstname],[Lastname],[Email],[Phonenumber],[Address],[CNP],[SeriesCI],[NumberCI],[Sex],[CUI],[J],[Personaldescription],[Temperament],[Provenance],[Registerdata],[NumeAgent],[Idteam])" + "Select @f,@l,@e,@ph,@add,@cnp,@ser,@n,@sex,@cui,@j,@pd,@te,@prov,@reg,[Firstname]+' '+[Lastname] from team where [Email]=@email,team.[id] from team where team.[Email]=@email";

1 个答案:

答案 0 :(得分:0)

尝试更改INSERT语句,如下所示:

cmd.CommandText = "INSERT INTO [clients]([Firstname],[Lastname],[Email],[Phonenumber],[Address],[CNP],[SeriesCI],[NumberCI],[Sex],[CUI],[J],[Personaldescription],[Temperament],[Provenance],[Registerdata],[Idteam],[NumeAgent])" +
    "Select @f,@l,@e,@ph,@add,@cnp,@ser,@n,@sex,@cui,@j,@pd,@te,@prov,@reg,team.[id],@agent from team where team.[Email]=@email";

注意,我已将,@agent部分从WHERE子句的末尾移至SELECT字段列表的末尾。

但是,我不确定你的整体想法是否正确。如果要从team表中选择要插入的值,为什么还要将它们指定为命令参数?