Search code examples
c#.netado.nettransactionsdispose

How to prevent resources from being consumed by long-running queries?


I hava access DB, one of my function(C#.net) need to Exec a SQL more than 4000 times with transaction.

It seems that after execution the DB file stay opened exclusively. because there is a *.ldb file, and that file stay there for a long time.

Is that caused by dispose resources incorrectly???

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 templateNumber=@templateNumber";

        reslt = OleDBHelper.ExecSqlWithTran(sqlUpdateUnique, trn, _params);
        if (reslt < 0)
        {
            throw new Exception(dr["id"].ToString());
        }
    }
    return reslt;
}

the transaction:

    using (Trans trn = new Trans())
    {
        try
        {
            int reslt=AmendUniqueData(trn);
            trn.Commit();
            return reslt;
        }
        catch
        {
            trn.RollBack();
            throw;
        }
        finally
        {
            trn.Colse();
        }
    }

Solution

  • forget closing the database connection.