0
我喜欢访问DB,我的一个函数(C#.net)需要使用事务执行超过4000次的SQL。如何防止长时间运行的查询消耗资源?
看来,执行后DB文件保持独占打开。因为有一个* .ldb文件,并且该文件在那里呆了很长时间。
是由于处理资源不正确造成的吗???
private int AmendUniqueData(Trans trn)
{
int reslt = 0;
foreach (DataRow dr in _dt.Rows)
{
OleDbParameter[] _params = {
new OleDbParameter("@templateId",dr["Id"].ToString()),
new OleDbParameter("@templateNumber",dr["templateNumber"].ToString())
};
string sqlUpdateUnique = "UPDATE " + dr["proformaNo"].ToString().Substring(0,2) + "_unique SET templateId = @templateId WHERE [email protected]";
reslt = OleDBHelper.ExecSqlWithTran(sqlUpdateUnique, trn, _params);
if (reslt < 0)
{
throw new Exception(dr["id"].ToString());
}
}
return reslt;
}
交易:
using (Trans trn = new Trans())
{
try
{
int reslt=AmendUniqueData(trn);
trn.Commit();
return reslt;
}
catch
{
trn.RollBack();
throw;
}
finally
{
trn.Colse();
}
}
确保您关闭数据库连接。 – 2012-03-19 12:29:07