13
private void SetConnection()
{
     string a = string.Format(@"Data Source={0};Version=3;New=False;Compress=True;", "~/lodeDb.db");
     sql_con = new SQLiteConnection(a);
}

private void ExecuteQuery(string txtQuery)
{
     SetConnection();
     sql_con.Open();
     sql_cmd = sql_con.CreateCommand();
     sql_cmd.CommandText = txtQuery;
     sql_cmd.ExecuteNonQuery();
     sql_con.Close();
}

When I run sql_cmd.ExecuteNonQuery, Sqlexception is :

"Unable to open the database file".

"lodeDb.db" file on my hosting, I think data source is wrong. If database file in online hosting, how to set datasourse for connection? Permission file is no problem here.

user4157124
  • 2,809
  • 13
  • 27
  • 42
Futureman
  • 309
  • 2
  • 5
  • 11

7 Answers7

47

I got the same exception when trying to open databases that are on the network drive (path starts with "\\myServer\myDbFile...") and I resolved it by putting true to the parseViaFramework parameter in connection constructor.

sql_con = new SQLiteConnection(a, true);
gajo357
  • 958
  • 12
  • 22
  • You may also prepend UNC paths with `\\\`, as described [here](http://sqlite.1065341.n5.nabble.com/System-Data-SQLite-and-UNC-Paths-td72920.htm). – tm1 Sep 13 '16 at 10:12
  • 1
    Updating the UNC path link. It is now here http://sqlite.1065341.n5.nabble.com/System-Data-SQLite-and-UNC-Paths-td72920.html – The Lonely Coder Sep 10 '19 at 17:35
  • one thing i found out. you can still use a folder that only has a UNC path with out adding the true, if your using a relative path. so myfolder\ok.db3 doesn't need to "parse with framework". but \\myserver\myfolder\okay.db3 does even though it's pointing to the same file in the same location. – Kit Ramos Nov 18 '21 at 20:23
6

It's a Connection String Issue,

SQL Lite Connection Strings Formats

Basic :

Data Source=filename;Version=3;

Using UTF16 :

Data Source=filename;Version=3;UseUTF16Encoding=True;

With password :

Data Source=filename;Version=3;Password=myPassword;

Using the pre 3.3x database format :

Data Source=filename;Version=3;Legacy Format=True;

With connection pooling :

Data Source=filename;Version=3;Pooling=False;Max Pool Size=100;

Read only connection :

Data Source=filename;Version=3;Read Only=True;

EDIT 1 :
Connecting to remote database differs, you must check the following.

  1. Firewall port permissibility.
  2. Company/Host providing database is allowing remote connection.
Ahmed Ghoneim
  • 6,834
  • 9
  • 49
  • 79
  • 2
    Sorry but when i run application in my local computer, this app is running very well. But i deploy app to server ---> error.Can you tell me how to connect to database – Futureman Jun 04 '12 at 03:25
5

My problem is solved when add "Journal Mode=Off;" in connection string

Disable the Journal File This one disables the rollback journal entirely.

Data Source=c:\mydb.db;Version=3;Journal Mode=Off;

1

I had the same problem and fixed it using query string like: @"Data Source=C:\ProgramData\proj\lodeDb.db;Version=3;FailIfMissing=False"

By that, I meant that when I use the full path for the location of the DB file, it works, when I use "~/lodeDb.db" it does not work

1

I was facing same issue on the shared hosting server, My C# code was able to read data from SQLIte db file. But while add / update data it was throwing Error "unable to open database"

I tried many options suggested on stackoverflow But after referring https://stackoverflow.com/a/17780808/2021073 and https://www.sqlite.org/pragma.html#pragma_journal_mode I tried adding journal mode=Off; to the Connection string

it worked for me

sample code

SQLiteConnection connection = new SQLiteConnection("Data Source=G:\dbfolder\sqlite3.db;Version=3;Mode=ReadWrite;journal mode=Off;", true);
Alvin
  • 11
  • 4
0

This solution worked for me:

var index = dialog.FileName.IndexOf('\\'); example: "C:\TEST.DB"
if (0 != index) {
    Models.Context.CreateDatabase(dialog.FileName);
    OpenProject(dialog.FileName);
}
else //example: "\\Ceng\Share\MyPC"
{
    var path = dialog.FileName.Replace('\\', '/');
    Models.Context.CreateDatabase(path);
    OpenProject(path);
}
ivcubr
  • 1,988
  • 9
  • 20
  • 28
0

I have resolved this issue by setting up System.Data.SQLite dll=>properties=>Copy local=>"True"

Tushar B
  • 1
  • 1