Click here to Skip to main content
15,889,116 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
i use this code to delete records who is selected by checkbox in the datagridview bout it take too long time to do the command

C#
private void delete_Click(object sender, EventArgs e)
        {
            foreach(DataGridViewRow item in advancedDataGridView1.Rows)
            {
                if(bool.Parse(item.Cells[0].Value.ToString()))
                {
                    
                    conn.Open();
                    SqlCommand cmd = new SqlCommand("delete from tabl where id = '" + item.Cells[1].Value.ToString() + "'", conn);
                    cmd.ExecuteNonQuery();
                    conn.Close();
                }
            }
            MessageBox.Show("Successfully Deleted....");
        }


and i use this code to checkbox

C#
private void Chkselectall_CheckedChanged(object sender, EventArgs e)
        {
            for(int n = 0; n< advancedDataGridView1.Rows.Count;n++)
            {
                advancedDataGridView1.Rows[n].Cells[0].Value = chkselectall.Checked;
            }
        }


What I have tried:

what should i do the solve this problem


new update

C#
sql = "delete from tabl where id in (";
           parameterCounter = 0;
           using (SqlConnection cn = new SqlConnection("Data Source=SQL5035.site4now.net;Initial Catalog=DB_A448D1_ilgswork;User Id=**********;Password=**********"))

           {
               using (SqlCommand cmd = new SqlCommand(sql, cn))
               {
                   foreach (DataGridViewRow item in advancedDataGridView1.Rows)
                   {
                       if (bool.Parse(item.Cells[0].Value.ToString()))
                       {
                           parameterCounter++;
                           parameter = new SqlParameter();
                           parameter.ParameterName = "@par" + parameterCounter.ToString();
                           parameter.DbType = System.Data.DbType.Int32;
                           parameter.Value = item.Cells[1].Value;
                           cmd.Parameters.Add(parameter);
                           sql = sql + $"{parameter.ParameterName},";
                           // collecting all ids
                       }
                   }
                   sql = sql.TrimEnd(',');
                   sql = sql + ")";

                   cmd.CommandText = sql;
                   cmd.Connection = cn;
                   cn.Open();
                   cmd.ExecuteNonQuery();
               }
Posted
Updated 29-Jan-19 2:45am
v4
Comments
Maciej Los 9-Jan-19 4:40am    
Seems, you don't want to listen our advices about SQL injection...
Based on this piece of code no one is able to tell the reason of slow code execution.
el_tot93 9-Jan-19 4:43am    
my problem is im new in coding
Karthik_Mahalingam 9-Jan-19 8:49am    
is the ID an integer ?
el_tot93 13-Jan-19 0:03am    
yes
Karthik_Mahalingam 28-Jan-19 2:13am    
is this solved?

Do you know why it is slow, Because of the number of database calls, more number of checked item more number of database call.
Things you can do to avoid that
1) Create a stored procedure in your database which accepts list of items to be removed (How To Pass Array Or List To Stored Procedure[^])
2) Call the stored procedure using command (since you want to use the command)
3) Within a single DB call you will be able to delete N numbers of records.
for your comment that you are new to coding, These kind of problem should motivate you to write good code and it involves learning which will help you to enhance your coding skills.

Call to stored procedure

using (SqlConnection conn = new SqlConnection("yourconnectionstring")) {
    conn.Open();

    // 1.  create a command object identifying the stored procedure
    SqlCommand cmd  = new SqlCommand("yourprocedure", conn);

    // 2. set the command object so it knows to execute a stored procedure
    cmd.CommandType = CommandType.StoredProcedure;

    // 3. add parameter to command, which will be passed to the stored procedure
    cmd.Parameters.Add(new SqlParameter("yourparameter", list));
    
    // execute the command
    cmd.ExecuteNonQuery();
    
    
}


Happy learning.
 
Share this answer
 
Comments
el_tot93 9-Jan-19 7:41am    
bout im new and don't know how to work with stored procedure i didn't lernt it yet
Instead of sending lots of commands you could try sending one command using the "IN" clause, see example here: SQL IN Operator[^]
 
Share this answer
 
Comments
Maciej Los 9-Jan-19 6:18am    
Good idea!
But i'm afraid it won't help. The thing is that OP wants to use commands which may cause sql injection.
Dave Kreskowiak 9-Jan-19 7:26am    
His performance problem is that he appears to be using an ID field that's a string, and I'm willing to bet that field isn't indexed.
RickZeeland 9-Jan-19 9:12am    
That seems very likely, but for now the IN method seems to work for him ...
[no name] 9-Jan-19 7:29am    
As far as I know it is possible to dynamically create the IN clause and set afterwards the Parameter. Something liks SELECT * FROM Tbl WHERE Name IN (@p0, @p1, @p2, @p3,....). With this, SQL injection is out of question

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900