我有一个checkedlistbox
,textbox
和combobox
我的形式。我收到此错误:-
The variable name '@Provision' has already been declared. Variable names must be unique within a query batch or stored procedure.
Must declare the scalar variable "@BloodGroup".
在我没有为我的checkedlistbox
. 使用 for 循环时,我combobox
BloodGroup
的 , 出现错误。
string insert = "INSERT INTO Employee(EmpID, EmpName, Address, Provision, BloodGroup) VALUES (@EmpID, @EmpName, @Address, @Provision, @BloodGroup)";
SqlCommand comm = new SqlCommand(insert, sqlCon);
comm.Parameters.AddWithValue("@EmpID", SqlDbType.Int).Value = txtEmpID.Text;
comm.Parameters.AddWithValue("@EmpName", SqlDbType.VarChar).Value = txtName.Text;
comm.Parameters.AddWithValue("@Address", SqlDbType.Text).Value = txtAdd.Text;
for (int a = 0; a < chkProvision.Items.Count; a++)
{
if (chkProvision.GetItemChecked(a))
comm.Parameters.AddWithValue("@Provision", SqlDbType.Char).Value = chkProvision.Items[a].ToString();
else
comm.Parameters.AddWithValue("@Provision", "");
}
if (cbBlood.SelectedValue == null)
comm.Parameters.AddWithValue("@BloodGroup", "");
else
comm.Parameters.AddWithValue("@BloodGroup", SqlDbType.Char).Value = cbBlood.SelectedItem.ToString();
comm.ExecuteNonQuery();
当我更改我的 for 循环在 SqlCommand 之后的位置时,它会在 EmpID 上引发错误:-
Must declare the scalar variable "@EmpID".