0

My update code:

 for (int i = this.MedaxilGridView1.CurrentCell.RowIndex; i < this.MedaxilGridView1.RowCount; i++)
 {
     // KartsifarishiGridView-dən id götürüb ona uyğun sorğumuzu yazırıq.
     sqlSorgu = "UPDATE customer set medaxil_status = '0' WHERE id = " + 
                 this.MedaxilGridView1.Rows[i].Cells["id"].Value;
     //Sorğunu icra edirk.
     Program.esas.sqlSorguCommand.CommandText = sqlSorgu;
     Program.esas.sqlSorguCommand.Connection = Program.esas.bazayaQosul;
     Program.esas.sqlSorguCommand.ExecuteNonQuery();
     MedaxilGridView1.Rows[i].DefaultCellStyle.BackColor = Color.Empty;

 }

Row count : 18286

This version takes 5 minutes

How do I make it faster?

4
  • how do you extract the id's to operate with ? Commented Jun 19, 2012 at 7:05
  • 1
    you mean you have to update all the row with medaxil_status = '0' ? Commented Jun 19, 2012 at 7:09
  • You can try using a stored procedure. It will work a bit faster. Before that you can try executing the single query "UPDATE customer set medaxil_status = '0' WHERE id = " + this.MedaxilGridView1.Rows[i].Cells["id"].Value;" with some real value and check how long it takes. This will help you realise which part takes too much time.the loop or the sql itself. Commented Jun 19, 2012 at 7:10
  • 1
    You should also look into parameterized queries to make sure you're code isn't vurnerable to sql-injection. Commented Jun 19, 2012 at 7:10

3 Answers 3

1

You should think which query you use to extract the id's and use it directly into the update, so you call the database once:

"UPDATE customer set medaxil_status = '0' WHERE id in (select xxx xxx xxx)"

if you want to update all the rows, just remove the where clause and call the statement just once. If you have just an id list, maybe chunking the calls by using alwais the IN clause will reduce the number of queryes and hopefully the overall execution time.

Sign up to request clarification or add additional context in comments.

2 Comments

@Ozgur Dogus Abi bir misal koy bakim
All interlinear not. Selected row - last row
0

You can use BeginExecuteNonQuery for that, that will save you the connection initiation overhead, by opening one connection, and batch executing all of your queries.

A full example from MSDN can be found here

Furthermore, I would advise you to start using SQL Parameters

1 Comment

@FelicePollano Yes you can. All I want to 0
0

Have you tried to enclose your loop in a Transaction ? This speeds up MS Access update operations.

Take a look on this sample here that shows how to use MS Access Transaction in C#

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.