Last post Aug 12, 2016 11:00 AM by invincible_777
Aug 12, 2016 08:01 AM|invincible_777|LINK
I am creating an application using asp.net mvc 5. I want to use Sqlite in the application. I have installed Sqlite using Nuget.
The default file path of the database file is C:\Program Files (x86)\IIS Express.
I want to create the file in the App_Data folder inside the application itself.
Currently I am using the following code to create and open the DB file:
SQLiteConnection sqlite_conn = new SQLiteConnection("Data Source=test_database2.db;Version=3;New=True;Compress=True;");
How can I change the default database file location in Sqlite?
Aug 12, 2016 08:18 AM|Mikesdotnetting|LINK
SQLiteConnection sqlite_conn = new SQLiteConnection("Data Source=|DataDirectory|test_database2.db;Version=3;New=True;Compress=True;");
Aug 12, 2016 08:26 AM|invincible_777|LINK
Hey Mike. That solved my problem. Thank you for the help.
Another related question to the problem. If I want to create the file in a folder other than App_Data then how can we do that?
Aug 12, 2016 09:11 AM|Mikesdotnetting|LINK
The "data source" part of the connection string needs a file path. So just provide one that points to the location where you want the database to live. You can provide a full file path e.g. C:\Data\mydb.sqlite, or a relative one, which will be rooted in
IIS Express (that's what you had originally), or you can use Server.MapPath to resolve a file path from a virtual path:
SQLiteConnection sqlite_conn = new SQLiteConnection("Data Source=" + Server.MapPath("~/db/test_database2.db") + ";Version=3;New=True;Compress=True;");
That will find a database in a folder called "db" in the root folder for your site.
|DataDirectory| is a special token that ASP.NET recognises and will resolve by default to the App_Data folder in your site. You are recommended to place file-based databases there because it is configured to prevent people being able to browse to it and
download the contents.
Aug 12, 2016 11:00 AM|invincible_777|LINK
Thank you Mike for all the help. All my doubts have been cleared. Thank you again.