Unable to Open

“Unable to Open the Database File”. Do I Have to Put the Full Physical Location of Sq lite Database When Developing C# Console Application


All non-rooted paths used inside a program are relative to the folder in which the application is started. Note that this is not necessarily the folder that the application itself is in. You have to be very careful with such paths.

What you seem to be after is the folder relative to your own application. There’s a simple method for getting that. If you have the Windows.Forms namespace included in your project, all you need to do to get the application folder is

// Get app folder
String appFolder = Path.GetDirectoryName(Application.ExecutablePath)
// Combine with sqlite db path
String dbPath = Path.Combine(appFolder, @"App_Data\mydatabase.sqlite");
// Build connection string
string cs = String.Format(@"Data Source={0};Version={1}", dbPath, 3);

An alternative for Application.ExecutablePath if you’re not using a Windows forms program is Assembly.GetExecutingAssembly().Location.


Please enter your comment!
Please enter your name here