[英]Update MS Access db in C# best way
我的更新代码:
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分钟
我如何使其更快?
您应该考虑使用哪个查询来提取ID并将其直接用于更新,因此您一次调用数据库:
"UPDATE customer set medaxil_status = '0' WHERE id in (select xxx xxx xxx)"
如果要更新所有行 ,只需删除where子句并仅调用一次语句即可 。 如果您只有一个ID列表,也许可以使用alwais IN子句对调用进行分块处理,从而减少查询数量,并希望减少整体执行时间。
您是否尝试将循环包含在事务中? 这样可以加快MS Access更新操作的速度。
就拿这个样本来看看这里展示了如何在C#中使用的MS Access交易
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.