Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sqlite + c#: 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 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

like image 868
Futureman Avatar asked Jun 04 '12 02:06

Futureman


People also ask

Is SQLite written in C?

Since its inception on 2000-05-29, SQLite has been implemented in generic C. C was and continues to be the best language for implementing a software library like SQLite. There are no plans to recode SQLite in any other programming language at this time.

Can I use SQLite with C#?

Getting Started with SQLite from a . Open Visual Studio, select new project, and, in Visual C#, select “Console Application” and provide the name as SQLiteDemo. Click OK. To connect SQLite with C#, we need drivers. Install all required SQLite resources from the NuGet package, as pictured in Figure 1.

Is MySQL better than SQLite?

MySQL has a well-constructed user management system which can handle multiple users and grant various levels of permission. SQLite is suitable for smaller databases. As the database grows the memory requirement also gets larger while using SQLite. Performance optimization is harder when using SQLite.

Which is better SQL or SQLite?

SQLite vs SQL (Comparison) The main difference between them is that SQL stands for Structured Query Language, which is the query language used with databases. But SQLite is a portable database. Extensions can be added to the computer language used to access the database.


3 Answers

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);
like image 162
gajo357 Avatar answered Oct 12 '22 22:10

gajo357


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.
like image 45
Ahmed Ghoneim Avatar answered Oct 12 '22 23:10

Ahmed Ghoneim


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;

like image 41
Bhanuprakash Reddy Avatar answered Oct 13 '22 00:10

Bhanuprakash Reddy