1

I added a SQLite database to the root of my project and set its "Build Action" property to "Content":

enter image description here

I'm trying to open the database this way:

using System.Data.SQLite;
. . .
string cs = "Data Source=\\F4FDataSQLite_SingleTable.db;";
using var con = new SQLiteConnection(cs);
con.Open();

...but the exception "unable to open database file" is thrown on the con.Open() line above.

I don't want to use the full file path (C:\Users\bclay\source\repos\F4F_Core\F4F_Core\F4FDataSQLite_SingleTable.db) to the database, because that's not going to be a valid path for anybody but myself.

Is there something wrong with my connection string that is causing this error message?

3 Answers 3

2
+500

At first, check the debug folder on your project that there it is

Next:

    var connectoinString=Path.Combine("Data source=", Environment.CurrentDirectory, "F4FDataSQLite_SingleTable.db")    
  //  or    
    var connectoinString=Path.Combine("Data source=", Application.StartupPath, "F4FDataSQLite_SingleTable.db")

using var con = new SQLiteConnection(connectoinString);
con.Open();
Sign up to request clarification or add additional context in comments.

2 Comments

Which is preferable, to use "Application.StartupPath" or "Environment.CurrentDirectory"? IOW, which one will most likely never cause a problem when the app is running on a user's machine?
By default, The result is the same for both. But one of the differences is that you can't set a value for Application.StartupPath, But you can do that for Environment.CurrentDirectory during your project by FolderBrowserDialog and like that. In this situation, I think it's better to use Application.StartupPath
1

Try this:

var dbPath = Path.Combine(Environment.CurrentDirectory, "F4FDataSQLite_SingleTable.db");
string cs = $"Data Source={dbPath}";
using var con = new SQLiteConnection(cs);
con.Open();

Or this:

var dbPath = Path.Combine(Environment.CurrentDirectory, "F4F_Core", "F4FDataSQLite_SingleTable.db");
string cs = $"Data Source={dbPath}";
using var con = new SQLiteConnection(cs);
con.Open();

The path may be one of these two based if your project is in the solution folder or not

Comments

0

Adapting Hesam's answer, I tried this:

string connStr = Path.Combine("Data source=", Environment.CurrentDirectory, "F4FDataSQLite_SingleTable.db");
using var con = new SQLiteConnection(connStr);
con.Open();

...and got this:

Invalid ConnectionString format for part "C:\Users\bclay\source\repos\F4F_Core\F4F_Core\bin\Debug\netcoreapp3.1\F4FDataSQLite_SingleTable.db", no equal sign found

connStr is:

C:\Users\bclay\source\repos\F4F_Core\F4F_Core\bin\Debug\netcoreapp3.1\F4FDataSQLite_SingleTable.db

(the "Data source=" bit is not there)

So I tried this:

string connStr = string.Format(@"Data source={0}\F4FDataSQLite_SingleTable.db", Environment.CurrentDirectory);
using var con = new SQLiteConnection(connStr);
con.Open();

...so connStr is now:

Data source=C:\Users\bclay\source\repos\F4F_Core\F4F_Core\bin\Debug\netcoreapp3.1\F4FDataSQLite_SingleTable.db

And it works.

Oddly enough, if I use "Application.StartupPath" instead of "Environment.CurrentDirectory", I have to leave off the backwhack:

connStr = string.Format(@"Data source={0}F4FDataSQLite_SingleTable.db", Application.StartupPath);

Which is preferable, to use "Application.StartupPath" or "Environment.CurrentDirectory"? IOW, which one will most likely never cause a problem when the app is running on a user's machine?

3 Comments

please see this link
By default, The result is the same for both. But one of the differences is that you can't set a value for Application.StartupPath, But you can do that for Environment.CurrentDirectory during your project by FolderBrowserDialog and like that. In this situation, I think it's better to use Application.StartupPath
@hesamakbari: Yes, I'm going with StartupPath; thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.