Excel 的只读 ADO.NET 连接字符串

发布于 2024-10-07 17:36:35 字数 447 浏览 3 评论 0原文

我试图防止在使用 ADO.NET 读取 Excel 文件时出现以下情况:

Microsoft Jet 数据库引擎无法打开文件“”。它已被其他用户以独占方式打开,或者您需要权限才能查看其数据。

我知道发生这种情况是因为另一个用户打开了该文件,但我无法删除它。我尝试将 Read Only=True; 添加到我的连接字符串中,但这不起作用。我还尝试将连接的 Mode 设置为只读,但我不知道如何在 C# 中进行设置。

这是我的连接字符串:

Provider=Microsoft.Jet.OLEDB.4.0; data source=test.xls; Extended Properties="Excel 8.0;HDR=No;IMEX=1"

感谢您的帮助!

I'm trying to prevent the following from coming up when reading an Excel file using ADO.NET:

The Microsoft Jet database engine cannot open the file ''. It is already opened exclusively by another user, or you need permission to view its data.

I know it is happening because another user has the file open, but I can't get rid of it. I've tried adding Read Only=True; to my connection string, but that doesn't work. I've also tried to set the Mode of the connection to read-only, but I can't figure out how to in C#.

Here is my connection string:

Provider=Microsoft.Jet.OLEDB.4.0; data source=test.xls; Extended Properties="Excel 8.0;HDR=No;IMEX=1"

Thanks for the help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

自演自醉 2024-10-14 17:36:35

如果您没有文件的权限,而不仅仅是当文件以独占方式打开时,也会出现这种情况。无论如何,我总是发现解决此问题的最佳方法是自动将文件副本复制到新的临时文件。即使对于大文件,副本也几乎是瞬时的,因此它们不会使您的速度减慢太多,并且由于您的应用程序只是只读的,因此您不必担心事后会丢弃临时文件。

此外,这将解决任何可以解决的权限问题。如果您具有读取权限,则副本将起作用,如果没有,则无论如何都没有解决方法。如果将临时副本的目标路径设置为与工作簿的路径相同,您将拥有复制文件的完全权限。 (Application.Path)

为此,请使用FileCopy srcFile, destName 命令。

This can also come up if you don't have permissions for the file, not just when it's exclusively open. In any event, I always found the best workaround for this problem was to automate a copy of the file to a new temp file. Copies are almost instantaneous, even for large files, so they shouldn't slow you down much, and since your application is only read-only, you don't have to worry about discarding the temp file afterwards.

Furthermore, this will clear up any permissions issues that can be cleared up. If you have read permissions, the copy will work, if not, then there's no workaround anyways. You will have full permissions for the copied file if you make the destination path of the temp copy the same path as the workbook. (Application.Path)

Use the FileCopy srcFile, destName command for this.

女中豪杰 2024-10-14 17:36:35

好吧,你说你尝试将模式更改为只读,你到底是怎么做到的?

根据 MSDN,默认情况下, Microsoft Jet 的 OLE DB 提供程序以读/写模式打开 Microsoft Jet 数据库。要以只读模式打开数据库,请在 ADO 连接对象 到 adModeRead。

所以在 C# 中它应该看起来像:

ADODB.Connection myConn = new new ADODB.Connection();
myConn.Mode = adModeRead; //1
myConn.Open(strConectionString, null, null, -1);

Well, you said you tried changing the mode to read only, how exactly did you do that?

As per MSDN, by default, the OLE DB Provider for Microsoft Jet opens Microsoft Jet databases in read/write mode. To open a database in read-only mode, set the Mode property on the ADO Connection object to adModeRead.

So in C# it should have looked like:

ADODB.Connection myConn = new new ADODB.Connection();
myConn.Mode = adModeRead; //1
myConn.Open(strConectionString, null, null, -1);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文