【问题标题】:There are more columns in the INSERT statement than values specified in the VALUES clause. The number of values in the VALUES claus [closed]INSERT 语句中的列多于 VALUES 子句中指定的值。 VALUES 子句中的值的数量[关闭]
【发布时间】:2017-08-08 09:59:20
【问题描述】:

我想在 SQL 表中保存新记录。 这个表字段之一是“用户名”,我想通过使用会话添加记录,其他字段是从用户那里获取的。 这是我在 C# asp.net 中的代码:

protected void savesubmit_Click(object sender, EventArgs e)
{

    SqlConnection con = new SqlConnection(@"Data Source=.;Initial Catalog=Khayati;Integrated Security=True");
    string qu = string.Format("insert into Ordertb (nokar,modeledokht,tozihat,username) values('{0}','{1}',N'{2}',N'{3}')", DropDownList1.Text, DropDownList2.Text, tozihtxt.Text, Convert.ToString(Session["username1"]));
    SqlDataAdapter da = new SqlDataAdapter(qu, con);
    DataSet ds = new DataSet();
    da.Fill(ds, "ordertbl");

}

但是当我运行它时,我看到了这个错误:

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

说明:在执行当前 Web 请求期间发生未处理的异常。 请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息。

异常详细信息:System.Data.SqlClient.SqlException:INSERT 语句中的列多于 VALUES 子句中指定的值。 VALUES 子句中的值数必须与 INSERT 语句中指定的列数匹配。

【问题讨论】:

  • 应该是'{0}','{1}','N','{2}'。观看'

标签: c# asp.net sql-server


【解决方案1】:

您的问题是您尝试插入 3 个值:

values('{0}','{1}',N'{2}')

分成4列:

(nokar,modeledokht,tozihat,username) 

我相信你是故意的:

values('{0}','{1}',N'{2}','{3}')

旁注:

始终使用Command.Parameters,而不是将您的命令解析为string!将命令解析为string 时,您会遇到 SQL 注入和错误,就像您遇到的那样。使用Command.Parameters 更安全、更轻松。

例子:

SqlCommand Command = new SqlCommand();
Command.CommandText = "insert into tbl (col) values (@val)";
Command.Parameters.Add(new SqlParameter("val", valueVariable));
Command.ExecuteNonQuery();

或者,在你的情况下:

SqlCommand Command = new SqlCommand();

Command.CommandText = @"insert into Ordertb 
                        (nokar,modeledokht,tozihat,username) 
                        values 
                        (@nokar,@modeledokht,@tozihat,@username)";

Command.Parameters.Add(new SqlParameter("nokar", DropDownList1.Text));
Command.Parameters.Add(new SqlParameter("modeledokht", DropDownList2.Text));
Command.Parameters.Add(new SqlParameter("tozihat", tozihtxt.Text));
Command.Parameters.Add(new SqlParameter("username", Convert.ToString(Session["username1"])));

Command.ExecuteNonQuery();

【讨论】:

  • 感谢您的回答。我更正了它,但仍然收到此错误:“'Ordertb' 附近的语法不正确。说明:在执行当前 Web 请求期间发生未处理的异常。请查看堆栈跟踪以获取有关错误及其来源的更多信息代码。异常详细信息:System.Data.SqlClient.SqlException:'Ordertb' 附近的语法不正确。"
  • 我尝试了 SQL 命令,如你所说,我收到此错误:“ ExecuteNonQuery:连接属性尚未初始化。说明:执行当前 Web 请求期间发生未处理的异常。请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息。异常详细信息:System.InvalidOperationException:ExecuteNonQuery:连接属性尚未初始化。"
  • 执行前需要打开连接
  • 非常感谢。
【解决方案2】:
insert into Ordertb (nokar,modeledokht,tozihat,username) values('{0}','{1}',N'{2}')

您已指定要设置四列,但只提供 3 个值。

(并对 SQL 注入漏洞投反对票:总是参数化您的查询。)

【讨论】:

    【解决方案3】:

    基本上你只需要将第四个值添加到你的 SQL 语句中。

    protected void savesubmit_Click(object sender, EventArgs e)
    {
    
        SqlConnection con = new SqlConnection(@"Data Source=.;Initial Catalog=Khayati;Integrated Security=True");
        string qu = string.Format("insert into Ordertb (nokar,modeledokht,tozihat,username) values('{0}','{1}',N'{2}', '{3}')", DropDownList1.Text, DropDownList2.Text, tozihtxt.Text, Convert.ToString(Session["username1"]));
        SqlDataAdapter da = new SqlDataAdapter(qu, con);
        DataSet ds = new DataSet();
        da.Fill(ds, "ordertbl");
    
    }
    

    不过你应该考虑使用 SQL 参数。

    【讨论】:

      【解决方案4】:

      为避免此错误,请确保 VALUES 子句中指定的值数与 INSERT INTO 子句中指定的列数相匹配:

      确保所有值不为空或为空。

      (或)

      试试这个方法:

      INSERT INTO Ordertb ( nokar,modeledokht,tozihat,username ) 值(“米奇”、“鼠标”、“M”、“XYZ”)

      【讨论】:

        【解决方案5】:

        当您使用using 建立连接时也很好,这样您就不必担心之后关闭它。

            using (SqlCommand commandInsert = new SqlCommand(YourQuery, YourConnectionString))
                {
                    commandInsert.Parameters.Add(parameterAccount);
                    commandInsert.Parameters.Add(parameterPassword);
                    commandInsert.Parameters.Add(parameterBalance);
                    commandInsert.Parameters.Add(parameterAccountStatus);
                    commandInsert.Parameters.Add(parameterDateOfCreation);
        
                    commandInsert.ExecuteNonQuery();
        
                    connection.Close();
        
                };
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2021-09-11
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-09-22
          • 1970-01-01
          相关资源
          最近更新 更多