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

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?



Solution 1:[1]

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.

Solution 2:[2]

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;

Solution 3:[3]

Assuming the db file is accessible (e.g. "because SQLite Administrator can open the file"), then option #2 from the answer by ranmoro and GEEF seems to work. This becomes:

bool parseViaFramework = true;
con = new SQLiteConnection( cs, parseViaFramework );

in code. The rationale is discussed in the SQLite check-in comment "mistachkin added on 2013-05-25 21:06:45" in https://system.data.sqlite.org/index.html/info/bbdda6eae2

My connection strings are of the form:

URI=file:\\SERVER\Data\SqlData\History.db

for UNC paths, or

URI=file:C:\Data\SqlData\History.db

for local paths.

I am using:

Visual Studio 2022
<TargetFramework>net5.0-windows</TargetFramework>
<PackageReference Include="System.Data.SQLite.Core" Version="1.0.115.5" />

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Zustiur
Solution 2
Solution 3 rlarkin