Connection string with ASP.net? RRS feed

  • Question

  • User311961666 posted

    This is my current connection string:

    sConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data source= C:\\Invoice1.mdb";
    It works fine in when I run the website from visual studio 2010.  I am just wondering what location should be in there
    when I publish this site.
    I am a little unsure how this will work.  Thanks for your help!
    Monday, September 26, 2011 11:55 AM


All replies

  • User-1199946673 posted

    Put your database in the App_Data folder of your website, where it is protected from downloading. Change your connectionstring to:

    sConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data source=|DataDirectory|Invoice1.mdb";


    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, September 26, 2011 12:48 PM
  • User311961666 posted

    That worked great!  Thank you!

    Tuesday, September 27, 2011 9:33 AM
  • User-1679916991 posted

    If I may, please, tag onto this string.  I have a large app I wrote in classic asp (before it was classic) that I'm converting to Net. For the overall understanding it is a horse competition event management and rider registration system.  Ride and registration data are all in a normalized SQL Server db.  Shortly before the ride, Ride management clicks a button that downloads their particular ride data to a parallel Access database that they take to the ride to checkin riders, record results, etc.  To differentiate among the many rides each Access db's name contains the EventID.  For example, 2003eventdb388.mdb, 2003eventdb434.mdb, 2003eventdb540.mdb, etc.  Therefore the connection string is created programmatically during the run to build that Access db.  Old code:

    csDBcopy="d:\hosting\natrc5\EventDB\2000eventdb" & csEventID & ".mdb"
    If objFSO.FileExists(csDBcopy)=True then
    	objFSO.DeleteFile "d:\hosting\natrc5\EventDB\2000eventdb" & csEventID & ".mdb", False
    End If
    objFSO.CopyFile csDB, csDBcopy
    Set aConn = Server.CreateObject("ADODB.Connection")
    aConn.Open "Driver={Microsoft Access Driver (*.mdb)}; DBQ=" & csDBcopy & ";"
    (then I start the many queries that read the data from the SQL db and write to the Access db)

    This is how I try to do the same thing in Net:

    OleDbCommand cmd = new OleDbCommand();
    csPath2 = Server.MapPath("eventdb\\");
    OleDbConnection aConn = new OleDbConnection();
    aConn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + @csPath2 + csDBmdb;

    In debug, the connection string shows the correct path until I


    then I get an error message saying it cannot find part of the path and the path shown in the error message is somewhere else.  I've been going in circles a couple days with this.  I read your article about putting the files in the App_Data directory but didn't know if I could copy/delete & zip programmatically from this directory.  Please advise.  Thanks!

    Saturday, October 1, 2011 6:10 PM