locked
How to change the default file/db location in Sqlite RRS feed

  • Question

  • User-1315793439 posted

    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?

    Friday, August 12, 2016 8:01 AM

Answers

  • User-821857111 posted
    SQLiteConnection sqlite_conn = new SQLiteConnection("Data Source=|DataDirectory|test_database2.db;Version=3;New=True;Compress=True;");
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, August 12, 2016 8:18 AM
  • User-821857111 posted

    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.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, August 12, 2016 9:11 AM

All replies

  • User-821857111 posted
    SQLiteConnection sqlite_conn = new SQLiteConnection("Data Source=|DataDirectory|test_database2.db;Version=3;New=True;Compress=True;");
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, August 12, 2016 8:18 AM
  • User-1315793439 posted

    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?

    Friday, August 12, 2016 8:26 AM
  • User-821857111 posted

    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.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, August 12, 2016 9:11 AM
  • User-1315793439 posted

    Thank you Mike for all the help. All my doubts have been cleared. Thank you again.

    Friday, August 12, 2016 11:00 AM