【问题标题】:Insert statements more than values clause must match the number of columns插入语句超过 values 子句必须匹配列数
【发布时间】:2017-11-27 22:49:54
【问题描述】:

我收到此错误:

INSERT 语句中的列多于VALUES 子句中指定的值。 VALUES 子句中的值数必须与 INSERT 语句中指定的列数匹配。'

这是我的代码:

if (pictureBox1.Image != null)
{
                MemoryStream ms = new MemoryStream();
                pictureBox1.Image.Save(ms, pictureBox1.Image.RawFormat);
                byte[] a = ms.GetBuffer();
                ms.Close();

                MemoryStream ms1 = new MemoryStream();
                pictureBox2.Image.Save(ms1, pictureBox2.Image.RawFormat);
                byte[] a2 = ms1.GetBuffer();
                ms1.Close();

                MemoryStream ms2 = new MemoryStream();
                pictureBox3.Image.Save(ms2, pictureBox3.Image.RawFormat);
                byte[] a3 = ms2.GetBuffer();
                ms2.Close();

                MemoryStream ms3 = new MemoryStream();
                pictureBox4.Image.Save(ms3, pictureBox4.Image.RawFormat);
                byte[] a4 = ms3.GetBuffer();
                ms3.Close();


                cmd.Parameters.Clear();
               // cmd1.Parameters.Clear();

                cmd.Connection = con;
               cmd1.Connection = con;
                cmd.Parameters.AddWithValue("@img1", a);
                cmd.Parameters.AddWithValue("@img2", a2);
                cmd.Parameters.AddWithValue("@img3", a3);
                cmd.Parameters.AddWithValue("@img4", a4);

             cmd1.CommandText = "Insert into proiecte(numeproiect,judet,oras,strada,numaretajeimobil,clasaenergetica,parcare,mezanin,demisol,mansarda,descriereproiect)values('"
+ nameofproject.Text + "','" + district_text.Text + "','" + city_text.Text + "','" + street_text.Text + "','" + bunifuDropdown2.selectedValue + "','" + bunifuMaterialTextbox1.Text + "','" + bunifuDropdown1.selectedValue + "','" + mezanine + "','" + semibasement + "','" + mansard + richTextBox1.Text + "')";
               cmd.CommandText = "insert into proiecte(img1,img2,img3,img4)values(@img1,@img2,@img3,@img4)";
                con.Open();
                cmd1.ExecuteNonQuery();
                cmd.ExecuteNonQuery();
                con.Close();
}

【问题讨论】:

  • 这个错误信息有什么令人困惑的地方吗?
  • 您的代码易受 SQL 注入攻击。
  • 我想指出你的cmd1 受到SQL Injection 攻击,你真的应该考虑修复它。有趣的是,您显然知道如何使用参数化查询,因为您在 cmd 中这样做 - 一定要对 cmd1 做同样的事情!
  • 就像我写的一样:insert into sometable(column1) values(1,2,3,4,5)。 sql server 说:来吧,伙计!你要我插入 1 或 2 或 3 或 4 或 5 吗?您需要确保列数与您给我的值数相同。不要给我 5 个值并要求我将它们插入 1 列。聪明的老兄!
  • 您应该查看您的代码生成的 SQL 语句,以确保它是可执行的。这样做之后,我相信找到错误会非常简单。

标签: c# sql sql-server


【解决方案1】:

您的插入命令有 11 列和 10 个值

    cmd1.CommandText = "Insert into proiecte(
                            numeproiect
                            ,judet
                            ,oras
                            ,strada
                            ,numaretajeimobil
                            ,clasaenergetica
                            ,parcare
                            ,mezanin
                            ,demisol
                            ,mansarda
                            ,descriereproiect)

                    values(
                            '"+ nameofproject.Text + "'
                            ,'" + district_text.Text + "'
                            ,'" + city_text.Text + "'
                            ,'" + street_text.Text + "'
                            ,'" + bunifuDropdown2.selectedValue + "'
                            ,'" + bunifuMaterialTextbox1.Text + "'
                            ,'" + bunifuDropdown1.selectedValue + "'
                            ,'" + mezanine + "'
                            ,'" + semibasement + "'
                            ,'" + mansard + richTextBox1.Text + "')";

更新:对于您的评论,通过删除 cmd1 并像这样构建 cmd 来组合 cmd1 和 cmd

    cmd.CommandText = "Insert into proiecte(
                             numeproiect
                            ,judet
                            ,oras
                            ,strada
                            ,numaretajeimobil
                            ,clasaenergetica
                            ,parcare
                            ,mezanin
                            ,demisol
                            ,mansarda
                            ,descriereproiect
                            ,img1
                            ,img2
                            ,img3
                            ,img4)

                    values(@numeproiect
                            ,@judet
                            ,@oras
                            ,@strada
                            ,@numaretajeimobil
                            ,@clasaenergetica
                            ,@parcare
                            ,@mezanin
                            ,@demisol
                            ,@mansarda
                            ,@descriereproiect
                            ,@img1
                            ,@img2
                            ,@img3
                            ,@img4)";

    cmd.Parameters.AddWithValue("@numeproiect", nameofproject.Text);
    cmd.Parameters.AddWithValue("@judet", district_text.Text);
    cmd.Parameters.AddWithValue("@oras", city_text.Text);
    cmd.Parameters.AddWithValue("@strada", street_text.Text);
    cmd.Parameters.AddWithValue("@clasaenergetica", bunifuDropdown2.selectedValue);
    cmd.Parameters.AddWithValue("@parcare", bunifuMaterialTextbox1.Text);
    cmd.Parameters.AddWithValue("@mezanin", mezanine );
    cmd.Parameters.AddWithValue("@demisol", semibasement );
    cmd.Parameters.AddWithValue("@mansarda", mansard);
    cmd.Parameters.AddWithValue("@descriereproiect", a);

    cmd.Parameters.AddWithValue("@img1", a);
    cmd.Parameters.AddWithValue("@img2", a2);
    cmd.Parameters.AddWithValue("@img3", a3);
    cmd.Parameters.AddWithValue("@img4", a4);

记得修复错误 varbinary 和正确的参数。我只是从你的问题中复制粘贴它,所以它包含错误

【讨论】:

  • 谢谢,但现在它给了我这个: System.Data.SqlClient.SqlException: '不允许从数据类型 varchar 到 varbinary 的隐式转换。使用 CONVERT 函数运行此查询。'
  • cmd 或 cmd1 导致该错误?你能发布你的表格结构吗?
  • cmd1 是由字符串组合创建的,因此它们在发送到您的数据库时都具有数据类型 varchar。检查您的表结构并使用 cmd 之类的变量重建 cmd1 然后更正您的参数
  • 我解决了这个错误,在我的表结构中我有 varbinary 而不是 varchar 但是.. 我现在希望这两个命令匹配,因为他在一行上给了我 cmd1,在另一行上给了我 cmd跨度>
  • 好的,非常感谢您,先生,祝您一切顺利。
【解决方案2】:

看起来您在 INSERT 部分中有 11 列,但只有 10 个值要输入。

您可能需要在 ma​​nsard + richTextBox1.Text 部分之间添加另一个逗号。

【讨论】:

    【解决方案3】:

    您的问题在于最后两个 VALUES

    + "','" + mansard + "', '" + richTextBox1.Text + "')";
    

    这假定两者都是文本值。如果没有,只需删除单引号,但您需要逗号。

    【讨论】:

      猜你喜欢
      • 2015-09-17
      • 2020-09-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-21
      • 1970-01-01
      • 2017-09-05
      • 1970-01-01
      相关资源
      最近更新 更多