Search code examples
sqlitevisual-studio-2012runtime-error

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

  • 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