2017-08-08 617 views
-6

我想在SQL表中保存新记录。 这个表字段之一是我想通过使用会话添加记录的“用户名”,而其他字段是从用户获取的。 这是我在C#asp.net代码:INSERT语句中的列数多于VALUES子句中指定的值。 VALUES claus中值的个数

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"); 

} 

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

There are more columns in the INSERT statement than values specified in the VALUES clause. The number of values in the VALUES clause must match the number of columns specified in the INSERT statement.

Description: An unhandled exception occurred during the execution of the current web request. please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Data.SqlClient.SqlException: There are more columns in the INSERT statement than values specified in the VALUES clause. The number of values in the VALUES clause must match the number of columns specified in the INSERT statement.

+0

应该可能是“{0}”,“{1}”,“N”,“{2}”。注意''' – VDWWD

回答

3

你的问题是,你正试图插入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(); 
+0

感谢您的回答。我纠正了它,但仍然出现这个错误:“Ordertb附近的语法不正确 描述:在执行当前Web请求期间发生未处理的异常请查看堆栈跟踪以获取有关错误的更多信息以及它源于何处代码 异常详细信息:System.Data.SqlClient.SqlException:'Ordertb'附近的语法错误。“ – mino

+0

我试着用SQL命令,如你所说我得到这个错误:“ExecuteNonQuery:连接属性尚未初始化 描述:在执行当前Web请求期间发生未处理的异常请查看堆栈跟踪以获取更多关于错误和源代码的位置 异常详细信息:System.InvalidOperationException:ExecuteNonQuery:连接属性尚未初始化。“ – mino

+0

你需要在执行 –

0
insert into Ordertb (nokar,modeledokht,tozihat,username) values('{0}','{1}',N'{2}') 

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

(并有SQL注入漏洞的downvote:总是 parametrise查询。)

0

基本上你只需要到第四值添加到您的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参数。

0

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

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

(或)

试试这个方法:

INSERT INTO Ordertb(nokar,modeledokht,tozihat,用户名) VALUES( '米奇', '老鼠', 'M', 'XYZ')

0

当你使用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(); 

     }; 
相关问题