Intermittent “System resource exceeded” exception for OleDB connection to Microsoft Access data file

牧云@^-^@ 提交于 2019-12-01 09:58:15

A couple of things I would try in your case:

  1. Since the user of the database is the application only, I would open the database in exclusive mode, this will help the driver get rid of the overhead of managing lock files (and should speed-up access to the db too).
// Share Mode=12 - exclusive mode (16 for multi-user)
string constr = @"Provider=Microsoft.ACE.OLEDB.12.0;" +
                 "Mode=12;Data Source = C:\datafile.res;user id=;password=;";
  1. Open a connection to the Access database when your program start, and leave it open until the application closes.
    In tight loops, lock-file issues creep up and cause all sorts of strange issues that are hard to debug.
    Just have a dummy table with a single record in the Access database, then open that table to read the record, but keep a permanent reference to that connection:
private OleDbCommand PermanentCommand;

void KeepLinkOpen() {
   if (PermanentCommand == null || 
       PermanentCommand.Connection == null || 
       PermanentCommand.Connection.State == System.Data.ConnectionState.Closed) {

     OleDbConnection conn = new OleDbConnection(connectionString);
     conn.Open();
     PermanentCommand = new OleDbCommand("SELECT * FROM DummyTable", conn);
     PermanentCommand.ExecuteReader(System.Data.CommandBehavior.Default);
  }    
}

void Disconnect() {
  if (PermanentCommand != null) {
      if (PermanentCommand.Connection != null) {
          PermanentCommand.Connection.Close();
      }
      PermanentCommand.Dispose();
      PermanentCommand = null;
  }
}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!