Sqlite + c#: Unable to open the database file

北战南征 提交于 2020-01-01 09:15:15

问题


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 to sql_cmd.ExecuteNonQuery, Sqlexception is "Unable to open the database file". "lodeDb.db" file on my online hosting, i think data source is wrong. Can U tell me if db file in online hosting. How to set datasourse for connection Thanks you Note: Permission file is no problem in here


回答1:


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



回答2:


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.



回答3:


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;




回答4:


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




回答5:


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


来源:https://stackoverflow.com/questions/10875612/sqlite-c-unable-to-open-the-database-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!