9

I'm currently storing my sqlite db file in the App_Data folder as per ASP.NET best pattern and practices.

Currently I'm using the following in the webconfig:

  <connectionStrings>
    <add name="sqlite"  
         connectionString="Data Source=|DataDirectory|MyDB; Version=3;" />
  </connectionStrings>

and the following in code:

       public SqliteDAO(string path)
        {
            Connection = new System.Data.SQLite.SQLiteConnection(path );
        }

//...

//where path = |DataDirectory|MyDB

It causes sqlite to make a NEW database (with no tables in it), and thus none of my data access calls work, since they aren't finding the table names. How do I reference the sqlite db file in the App_Data folder from my WebApplication code??

Thanks!

mirezus
  • 13,892
  • 11
  • 37
  • 42

1 Answers1

10

Use Server.MapPath to your db file. So it would be something like

Server.MapPath(@"~\App_Data\Your.db");
Thomas Levesque
  • 286,951
  • 70
  • 623
  • 758
David Basarab
  • 72,212
  • 42
  • 129
  • 156
  • 1
    This is probably a silly question, but how would you do this from a file that isn't a "CodeBehind" file, where you don't have the Server object? Do you have to pass the server object, or the path from a Code-Behind file? – kdmurray Feb 03 '10 at 06:47
  • 1
    @kdmurray - you can use this method: http://msdn.microsoft.com/en-us/library/system.web.httpserverutility.mappath.aspx – Ben Laan Feb 08 '10 at 21:21
  • Thanks Ben. I managed to find a solution to this myself too, by passing the HttpContext object to the other class: MyClass c = new MyClass(); c.myFunc(this.Context); – kdmurray Feb 25 '10 at 08:54
  • 2
    @kdmurray, it is bit late but you can always use static `System.Web.HttpContext.Current` object if you are working in asp.net and don't need to pass it around. – TheVillageIdiot Mar 31 '11 at 10:50