如何连接到远程数据库

How to Connect to a Remote Database

我在连接到位于远程计算机上的 Access 数据库时遇到一些问题:

The Microsoft Access database engine cannot open or write to the file '\ACCESSSERVER-PC\Warehouse Manager\Error Logging\Error Logging.accdb'. It is already opened exclusively by another user, or you need permission to view and write its data.

数据库未打开供任何其他人编辑,我应该可以连接到它。

我正在使用这个连接字符串:

Provider=Microsoft.ACE.OLEDB.12.0;Data Source=\ACCESSSERVER-PC\Warehouse Manager\Error Logging\Error Logging.accdb;Persist Security Info=False;

并且在调用connOpen();时出现错误

using (var conn = new OleDbConnection(ConnectionString))
{
    try
    {    
        conn.Open();
    }
}

我给了自己对服务器以及 .accdb 文件本身的完全权限,并根据 this postNETWORK SERVICE 帐户做了同样的操作,但仍然不走运。

我在连接到存储在本地的数据库时没有问题,这似乎只在尝试通过网络连接时才会发生。

有没有其他人遇到过这种情况并找到了解决方案?非常感谢任何帮助或建议。

我已经检查 this answer 并且可以确认我拥有访问该文件所需的所有权限。

该问题被标记为 this question 的重复问题,但我没有任何程序 运行 可以打开文件流。

事实证明,这是由于尝试使用一些糟糕的 IT 基础架构造成的。

问题是 \ACCESSSERVER-PC 实际上是一台 Windows 7 机器,因此只能处理有限数量的连接。

运行 this answer 中针对数据库文件的 IsLocked 代码给了我一条更有用的错误消息:

No more connections can be made to this remote computer at this time because there are already as many connections as the computer can accept.

让一些用户断开他们的 Access 运行时和映射驱动器与服务器的连接并再次 运行 代码后,IsLocked 返回 false 然后我能够连接到数据库。

这是我用来帮助我找到解决方案的代码:

protected virtual bool IsFileLocked(FileInfo file)
{
    FileStream stream = null;

    try
    {
        stream = file.Open(FileMode.Open, FileAccess.Read, FileShare.None);
    }
    catch (IOException)
    {
        //the file is unavailable because it is:
        //still being written to
        //or being processed by another thread
        //or does not exist (has already been processed)
        // 
        // ** OR There are already too many connections open to the         
        // ** "server" where the file exists!
        return true;
    }
    finally
    {
        if (stream != null)
            stream.Close();
    }

    //file is not locked
    return false;
}

故事的寓意是 当您需要 20 多个并发连接到服务器!