0

I'm connecting to an access database with OleDb, and the data source is located in my project files. Since the project will be zipped and moved around, I want it to be found no matter what.

connetionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=University.accdb;";

Any other time I have worked with a data source, I have been able to put it's location like that, however, when I do that here, it tries to look in 'C:\Program Files (x86)\Common Files\Microsoft Shared\DevServer\10.0\University.accdb'.

Is there any way I can make this automatically look inside the project files?

smritz
  • 108
  • 6

1 Answers1

0

Try putting your database file in your App_Data directory in your solution. Then use this for your connection string...

connetionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\University.accdb;";

|DataDirectory| will find your App_Data folder where it's deployed.

EDIT: Based on this SO post you also need the following line in your Application_Start method of your Global.asax.

AppDomain.CurrentDomain.SetData("DataDirectory", Server.MapPath("~/App_Data/"));
Community
  • 1
  • 1
Kevin
  • 4,586
  • 23
  • 35
  • This now gives me an "unrecognized escape sequence" error at the \. – smritz Apr 10 '14 at 01:15
  • I corrected it... that's standard for C# strings.. you either need the `@` sign to tell it not to use escape sequences or escape a single \ by replacing it with \\. – Kevin Apr 10 '14 at 12:35
  • @smritz Did my edit above fix the issue? I edited the line with the connection string. – Kevin Apr 10 '14 at 14:00