SQL参数错误

本文关键字:错误 参数 SQL | 更新日期: 2023-09-27 17:54:30

这是我的cs代码

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

I got error

变量名'@salbn'已经被声明。变量名在查询批处理或存储过程中必须是唯一的。必须声明标量变量"@basic"

我正在使用c#和SQL Server

SQL参数错误

嗯,就像它说的:你添加了两次名为salbn的参数。第一次在SqlCommand com = ...下面,第二次在下一行

查询中有两个相同名称的参数@salbn。没问题,但是不能在Parameters集合中添加两次。我看到你的代码有两个控制相同的参数,为改变其中一个的名称,例如:

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