Futureman
Futureman

Reputation: 309

Unable to open the database file

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.

Upvotes: 13

Views: 35228

Answers (7)

Alvin
Alvin

Reputation: 11

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);

Upvotes: 1

Tushar B
Tushar B

Reputation: 1

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

Upvotes: 0

CenGokhan
CenGokhan

Reputation: 1

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);
}

Upvotes: 0

user2543087
user2543087

Reputation: 11

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

Upvotes: 1

gajo357
gajo357

Reputation: 978

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);

Upvotes: 48

Bhanuprakash Reddy
Bhanuprakash Reddy

Reputation: 41

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;

Upvotes: 4

Ahmed Ghoneim
Ahmed Ghoneim

Reputation: 7057

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.

Upvotes: 6

Related Questions