SQLite: Cannot open network file programmatically, even though worked before

坚强是说给别人听的谎言 提交于 2019-12-31 19:30:12

问题


I have used the code below to open a SQLite database file that sits on a network computer for more than a year now almost on a daily basis. Suddenly this morning, I am not able to open the file programmatically.

private Boolean Connect(String strPathFile)
{
    // Initialize the connection object.
    this.DbConnection = null;

    try
    {
        // DATABASE: Create the connection string and set the settings.
        String strConnection = @"Data Source=" + strPathFile + @";Version=3;";

        // DATABASE: Connect to the database.
        this.DbConnection = new SQLiteConnection(strConnection);
        this.DbConnection.Open();

        return true;
    }

    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }

    return false;
}

The file is a network resource in the form "\Server\ShareName\FileName.db" (less the double quotes).

Here is the interesting thing. SQLite Administrator has no issues opening up the network database file, none, and repeatedly. I can also open up the file locally. I copied the file to my local drive and simply changed the path inside Visual Studio 2012 (VS2012).

The server seemed fine. It had gone through a reboot at some point since the last time that I checked on it. I presume a Microsoft Update. File Explorer has no issues browsing the folder, and as I said SQLite Administrator can open the network file.

I checked once again on permissions and everyone has full control as well as the server's users have full control, both on the security permissions and on the share permissions. I checked the folder and file, and permissions are the same. I expected as much, because SQLite Administrator can open the file. The server does not have a firewall set up, Windows Firewall or otherwise. I rechecked that this morning as well. Again, SQLite Administrator would have complained about that.

I verified writing, by making a copy of the file on the network drive using File Explorer. That had no issues.

The sever is a Windows Server 2003. I am using Windows 7 Professional 64-bit.

I also tried to open up the database in read only mode, but that failed as well. I was expecting that behavior. SQLite Administrator still works nicely.

I tried various other connection strings including SQLiteConnectionStringBuilder() just to see what happens, and all roads lead to Rome, namely:

System.Data.SQLite.SQLiteException occurred
  HResult=-2147467259
  Message=unable to open database file
  Source=System.Data.SQLite
  ErrorCode=14
  StackTrace:
       at System.Data.SQLite.SQLite3.Open(String strFilename, SQLiteConnectionFlags connectionFlags, SQLiteOpenFlagsEnum openFlags, Int32 maxPoolSize, Boolean usePool)
       at System.Data.SQLite.SQLiteConnection.Open()
       at SQL.cSQL.Connect(String strPathFile) in C:\<Path to source file>:line 367
  InnerException: 

Thoughts?


回答1:


in version > 1.0.82.0

  1. Double the leading two backslashes in the file name (e.g. "\\\\network\share\file.db").

  2. Use a mapped drive letter.

  3. Use the SQLiteConnection constructor that takes the parseViaFramework boolean argument and pass 'true' for that argument.

See the SQL post here




回答2:


I had a similar issue. Replacing the UNC (\server\share\folder\file.db) with a mapped drive path (S:\folder\file.db) resolve the issue in my instance.




回答3:


The error message is very misleading + irritating. Applications working fine in the local environment get failed to start in client server situation.

It has mostly noting to do with the code. Its related to server side.

  • Make sure the Write access is available for the server folder containing the file.

  • UNC [IP based server path] is not supported still, the network path/folder should be mapped to overcome this issue.

  • Some sites+users are saying to mention the Version No. in the connection string. All my applications are working fine without using it.

Connection String:

Data Source=[Mapped Server Location]\[SubFolders]\[FileName].db;

Update:

I tried to prepend \\ to the UNC path and it worked (added additional \\ in the beginning only, not in-between).

Data Source=\\[UNC]\[SubFolders]\[FileName].db;


来源:https://stackoverflow.com/questions/17303076/sqlite-cannot-open-network-file-programmatically-even-though-worked-before

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!