C# sql参数错误
这是我的cs代码C# sql参数错误,c#,sql-server,C#,Sql Server,这是我的cs代码 protected void Button1_Click(object sender, EventArgs e) { SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["preconn"].ToString()); con.Open(); SqlCommand com = new SqlCommand("update sla
protected void Button1_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["preconn"].ToString());
con.Open();
SqlCommand com = new SqlCommand("update slab set salbn = @salbn,basic = @basic,hra = @hra,trvl = @trvl,mdeca = @mdeca,atnd = @atnd,tote = @tote where salbn = @salbn", con);
com.Parameters.Add("@salbn", DropDownList1.SelectedItem.Text);
com.Parameters.AddWithValue("@salbn", TextBox21.Text);
com.Parameters.AddWithValue("@basic", TextBox12.Text);
com.Parameters.AddWithValue("@hra", TextBox13.Text);
com.Parameters.AddWithValue("@trvl", TextBox15.Text);
com.Parameters.AddWithValue("@mdeca", TextBox16.Text);
com.Parameters.AddWithValue("@atnd", TextBox18.Text);
com.Parameters.AddWithValue("@tote", TextBox20.Text);
com.ExecuteNonQuery();
con.Close();
MsgBox("Updated Successfully");
}
我出错了
变量名'@salbn'已声明。变量名在查询批处理或存储过程中必须是唯一的。必须声明标量变量“@basic”
我正在使用C#和SQL Server,正如它所说的:您添加了两次名为
salbn
的参数。一次在SqlCommand com=…
下面,第二次在下一行。在查询中,您有两个同名的参数@salbn
。没问题,但您不能在参数
集合中添加两次。我看到您的代码中有两个控件用于相同的参数,用于更改其中一个参数的名称,用于示例:
SqlCommand com = new SqlCommand("update slab set salbn = @salbnValue, basic = @basic, hra = @hra, trvl = @trvl, mdeca = @mdeca, atnd = @atnd, tote = @tote where salbn = @salbn", con);
com.Parameters.Add("@salbnValue", DropDownList1.SelectedItem.Text);
com.Parameters.AddWithValue("@salbn", TextBox21.Text);
在查询中使用同一参数两次是可以的(如果您想要的话)。错误的是在
参数
集合中定义它两次。也就是说,看看OP代码,他似乎真的想使用两个不同的参数。否则,集合
代码就没有什么意义了。您可以Add()
@salbn
,然后调用AddWithValue()
,以获得不太好的度量。