Sqlite + C #: unable to open 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, the Sqlexception "Unable to open the database file." "lodeDb.db" on my online hosting, I think the data source is wrong. Can I tell if the db file is in the online hosting. How to install a resource for connection thank you Note. File permissions are not a problem here.

+6
source share
4 answers

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

 sql_con = new SQLiteConnection(a, true); 
+27
source

This is a connection string problem,

SQL Lite String Connection Formats

The main ones:

 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 pool:

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

Only for reading:

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

EDIT 1:
When connecting to a remote database, check the following.

  • Valid firewall port.
  • Hosting company / database allows remote connection.
+5
source

My problem is resolved by adding "Journal Mode = Off"; in the connection string

Disable log file This option completely disables the rollback log.

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

+3
source

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

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

+1
source

Source: https://habr.com/ru/post/917336/


All Articles