简体   繁体   English

禁用/刷新OleDbConnection缓存

[英]Disable/Flush OleDbConnection Cache

I've been fighting with OleDbConnection for a while now trying to get it to not cache. 我一直在与OleDbConnection战斗一段时间,试图让它不缓存。 Basically I am accessing a shared Access database, which is being written to from another application, and then I'm reading back values (having checked that it is flushed via the Last Write time and a subsequent 1 second delay). 基本上我正在访问一个共享的Access数据库,它正在从另一个应用程序写入,然后我正在读回值(检查它是否通过Last Write时间和随后的1秒延迟刷新)。

Unfortunately, this is entirely unreliable. 不幸的是,这完全不可靠。

I've been reading (and going insane) how to disable the connection pooling, and am subsequently, after each possible update, performing the following before reconnecting: 我一直在阅读(并且疯狂)如何禁用连接池,并且在每次可能的更新之后,在重新连接之前执行以下操作:

_connection.Close();
_connection.Dispose();
_connection = null;
OleDbConnection.ReleaseObjectPool();
GC.Collect();

In addition to this, the connection string disables connection pooling with OLE DB Services = -2 . 除此之外,连接字符串禁用OLE DB Services = -2连接池。 Finally, I have also changed PageTimeout to '10' in the registry for Jet 4.0. 最后,我还在Jet 4.0的注册表中将PageTimeout更改为“10”。

All of these measures are unfortunately having no effect. 遗憾的是,所有这些措施都没有效果。 Now the only thing I can think of doing is what is mentioned in this Microsoft KB Article , and call JRO.JetEngine.RefreshCache . 现在我唯一想到的就是这篇Microsoft知识库文章中提到的内容,并调用JRO.JetEngine.RefreshCache The only issue with that is that it's argument is an ADODB.Connection . 唯一的问题是它的参数是ADODB.Connection I'd rather not rewrite my whole database layer and where the records are being read by my software to use a legacy COM object just to have this functionality, but it appears that it may well be the only way. 我宁愿不重写我的整个数据库层以及我的软件正在读取记录以使用传统COM对象来获得此功能,但似乎它可能是唯一的方法。

My question is, whilst currently undergoing this task of rewriting to use ADODB (not even ADO.NET!), is it possible to disable the caching of an OleDbConnection ? 我的问题是,虽然目前正在进行重写以使用ADODB(甚至不是ADO.NET!)的任务,是否可以禁用OleDbConnection的缓存?

You might have some luck setting the ";Jet OLEDB:Flush Transaction Timeout" property to 0 or some low number. 您可能有一些运气将“; Jet OLEDB:Flush Transaction Timeout”属性设置为0或某个较低的数字。 http://msdn.microsoft.com/en-us/library/windows/desktop/ms681754(v=vs.85).aspx http://msdn.microsoft.com/en-us/library/windows/desktop/ms681754(v=vs.85).aspx

Finally, I found a workaround: Use OdbcConnection instead of OleDbConnection. 最后,我找到了一个解决方法:使用OdbcConnection而不是OleDbConnection。

This is the old code: 这是旧代码:

string mdbConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + mdbFile + ";OLE DB Services=-2";
using (OleDbConnection conn = new OleDbConnection(mdbConnectionString)) {
    conn.Open();
    //Do your query
}

And this is new one: 这是新的:

string mdbConnectionString = "Driver={Microsoft Access Driver (*.mdb, *.accdb)};Dbq=" + mdbFile;
using (OdbcConnection conn = new OdbcConnection(mdbConnectionString)) {
    conn.Open();
    //Do your query
}

All things work fine. 一切正常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM