以C#最佳方式更新MS Access数据库

时间:2012-06-19 07:00:17

标签: c# sql database performance ms-access

我的更新代码:

 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;

 }

行数:18286

此版本需要5分钟

如何让它更快?

3 个答案:

答案 0 :(得分:1)

您应该考虑使用哪个查询来提取ID并将其直接用于更新,因此您只需调用数据库一次:

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

如果您想要更新所有行,只需删除where子句并调用语句一次。 如果您只有一个id列表,可能使用alwais来调用调用,IN子句将减少查询数量,并希望减少总体执行时间。

答案 1 :(得分:0)

您可以使用BeginExecuteNonQuery,通过打开一个连接并批量执行所有查询,可以节省连接启动开销。

可以找到MSDN的完整示例here

此外,我建议您开始使用SQL Parameters

答案 2 :(得分:0)

您是否尝试将循环包含在事务中?这加快了MS Access更新操作。

查看此示例here,其中显示了如何在C#中使用MS Access Transaction