INSERT 语句中的列数多于 VALUES 子句中指定的值



我想在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 语句中指定的列数匹配。

您的问题是您正在尝试插入 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();
insert into Ordertb (nokar,modeledokht,tozihat,username) values('{0}','{1}',N'{2}')

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

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

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

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

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

(或(

试试这种方式:

插入到订单tb ( nokar,modeledokht,tozihat,username ( 值("米奇","鼠标","M","XYZ"(

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

最新更新