简体   繁体   中英

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.

尝试打开网络驱动器上的数据库(路径以“\\\\ myServer \\ myDbFile ...”开头)时,我得到了同样的异常,我通过将true到连接构造函数中的parseViaFramework参数来解决它。

sql_con = new SQLiteConnection(a, true);

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.

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;

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

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM