मैं अपने डेटाबेस के लिए एक सरल हटाएं बटन को कार्यान्वित करना चाहता हूं। घटना विधि इस तरह दिखता है:प्रत्येक पुनरावृत्ति के माध्यम से SqlCommand पैरामीटर का पुन: उपयोग कैसे करें?
private void btnDeleteUser_Click(object sender, EventArgs e)
{
if (MessageBox.Show("Are you sure?", "delete users",MessageBoxButtons.OKCancel, MessageBoxIcon.Warning) == DialogResult.OK)
{
command = new SqlCommand();
try
{
User.connection.Open();
command.Connection = User.connection;
command.CommandText = "DELETE FROM tbl_Users WHERE userID = @id";
int flag;
foreach (DataGridViewRow row in dgvUsers.SelectedRows)
{
int selectedIndex = row.Index;
int rowUserID = int.Parse(dgvUsers[0,selectedIndex].Value.ToString());
command.Parameters.AddWithValue("@id", rowUserID);
flag = command.ExecuteNonQuery();
if (flag == 1) { MessageBox.Show("Success!"); }
dgvUsers.Rows.Remove(row);
}
}
catch (SqlException ex)
{
MessageBox.Show(ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Information);
}
finally
{
if (ConnectionState.Open.Equals(User.connection.State))
User.connection.Close();
}
}
else
{
return;
}
}
लेकिन मैं यह संदेश प्राप्त:
एक चर @id घोषित किया गया है। परिवर्तनीय नाम एक क्वेरी बैच या संग्रहीत प्रक्रिया के भीतर अद्वितीय होना चाहिए।
क्या इस चर का पुन: उपयोग करने का कोई तरीका है?