Access 数据库的只读连接字符串
我正在尝试使用 System.Data.OleDb.OleDbConnection 连接到 Access 数据库文件。我需要以只读模式连接,因为另一个应用程序同时使用它。我可以以读/写方式连接到数据库,没有问题,但似乎无法在任何地方找到正确的只读字符串。
我尝试过:
Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Persist Security Info=False;Mode=Read
Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Persist Security信息=假;扩展属性=“只读=真;”
谢谢。
编辑:
(我应该在原来的问题中添加更多信息。)
当访问数据库位于本地计算机上时,我可以成功连接到它,但是当我尝试连接时使用连接字符串访问远程计算机上的数据库时,
Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Persist Security Info=False;Mode=Read
我会收到以下错误:
System.Data.OleDb.OleDbException (0x80004005): The Microsoft Office Access database engine cannot open or write to the file '{0}'. It is already opened exclusively by another user, or you need permission to view and write its data.
我的应用程序正在本地系统帐户下的 Windows 服务中运行。
I'm trying to connect to an Access database file using a System.Data.OleDb.OleDbConnection. I need to connect in readonly mode because another application uses it at the same time. I can connect to the database in read/write no problem but can't seem to find anywhere the correct string for readonly.
I've tried:
Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Persist Security Info=False;Mode=Read
Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Persist Security Info=False;Extended Properties="ReadOnly=true;"
Thanks.
EDIT:
(I should have put more information in the original question.)
I could connect successfully to the access database when it was on the local machine, but when I tried connecting to the access database on a remote machine with the connection string
Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Persist Security Info=False;Mode=Read
I would get the following error:
System.Data.OleDb.OleDbException (0x80004005): The Microsoft Office Access database engine cannot open or write to the file '{0}'. It is already opened exclusively by another user, or you need permission to view and write its data.
My application is running in a windows service under the local system account.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为这必须通过数据库管理员控制的用户权限来处理,或者使用您可以控制的记录集的不同游标类型来处理。我不认为连接字符串指定访问模式,它只是让您到达那里。 ;)
I think that has to be handled either by user permissions that the DB admin would control, or with different cursor types for your recordsets, which you would control. I don't think the connection string specifies access mode, it just gets you there. ;)
真正的问题是 Excel 使连接保持打开状态,直到文件关闭。
在 Excel 2007+ 中,MaintainConnection 设置默认设置为 true。你需要进入vb编辑器并使用代码将其变为false。我还没有找到通过可视化界面来做到这一点的方法。
即使您将连接字符串设置为只读,它也会锁定访问数据库(根据我的经验)。
对于数据透视表连接:
对于 QueryTable:
通过将其设置为 false,表将连接,运行命令,然后断开连接,释放锁定。
The real problem is that Excel leaves the connection open until the file is closed.
In Excel 2007+, the MaintainConnection setting is set to true by default. You need to go into the vb editor and use code to turn it to false. I haven't seen a way to do this through the visual interface.
Even if you set the connection string to readonly, it will lock an access database (from my experience).
For a pivottable connection:
For QueryTable:
By setting it to false, the table will connect, run the command, then disconnect, releasing the lock.