2012-08-23 14 views
15

मैं अपने डेटाबेस के लिए एक सरल हटाएं बटन को कार्यान्वित करना चाहता हूं। घटना विधि इस तरह दिखता है:प्रत्येक पुनरावृत्ति के माध्यम से 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 घोषित किया गया है। परिवर्तनीय नाम एक क्वेरी बैच या संग्रहीत प्रक्रिया के भीतर अद्वितीय होना चाहिए।

क्या इस चर का पुन: उपयोग करने का कोई तरीका है?

उत्तर

42

Parameters.AddWithValue की तरह इसे कहते आदेश के लिए एक नया पैरामीटर कहते हैं। चूंकि आप उसी नाम से लूप में ऐसा कर रहे हैं, तो आपको अपवाद मिल रहा है "परिवर्तनीय नाम अद्वितीय होना चाहिए"

तो आपको केवल एक पैरामीटर की आवश्यकता है, इसे लूप से पहले जोड़ें और लूप में केवल इसका मान बदलें।

command.CommandText = "DELETE FROM tbl_Users WHERE userID = @id"; 
command.Parameters.Add("@id", SqlDbType.Int); 
int flag; 
foreach (DataGridViewRow row in dgvUsers.SelectedRows) 
{ 
    int selectedIndex = row.Index; 
    int rowUserID = int.Parse(dgvUsers[0,selectedIndex].Value.ToString()); 
    command.Parameters["@id"].Value = rowUserID; 
    // ... 
} 

दूसरा तरीका command.Parameters.Clear(); का उपयोग करना है। फिर आप एक ही पैरामीटर को दो बार बनाये बिना लूप में पैरामीटर भी जोड़ सकते हैं।

0

त्रुटि इसलिए है क्योंकि आप लूप के प्रत्येक पुनरावृत्ति में बार-बार समान पैरामीटर जोड़ रहे हैं।

मैं उस कोड को एक अलग तरीके से ले जाऊंगा ताकि मैं इसे आवश्यकतानुसार कई स्थानों से कॉल कर सकूं।

public bool DeleteUser(int userId) 
{ 
    string connString = "your connectionstring"; 
    try 
    { 
     using (var conn = new SqlConnection(connString)) 
     { 
     using (var cmd = new SqlCommand()) 
     { 
      cmd.Connection = conn; 
      cmd.CommandType = CommandType.Text; 
      cmd.CommandText = "DELETE FROM tbl_Users WHERE userID = @id"; 
      cmd.Parameters.AddWithValue("@id", userId); 
      conn.Open(); 
      cmd.ExecuteNonQuery(); 
      return true; 
     } 
     } 
    } 
    catch(Exception ex) 
    { 
     //Log the Error here for Debugging 
     return false; 
    } 

} 

फिर इस

foreach (DataGridViewRow row in dgvUsers.SelectedRows) 
{ 
    int selectedIndex = row.Index; 
    if(dgvUsers[0,selectedIndex]!=null) 
    { 
    int rowUserID = int.Parse(dgvUsers[0,selectedIndex].Value.ToString()); 
    var result=DeleteUser(rowUserID) 
    } 
    else 
    { 
     //Not able to get the ID. Show error message to user 
    } 
} 
3

बजाय:

command.Parameters.AddWithValue("@id", rowUserID); 

कुछ प्रयोग की तरह:

System.Data.SqlClient.SqlParameter p = new System.Data.SqlClient.SqlParameter(); 

foreach के बाहर, और बस पाश अंदर मैन्युअल रूप से सेट:

p.ParameterName = "@ID"; 
p.Value = rowUserID; 
संबंधित मुद्दे