I would do this:
- Select
Add New Data Source . . . Add New Data Source . . . in a data source window in Visual Studio - Select
Database from the list that appears and click - Click next
- On the
Choose your data connection page of the wizard, click New connection - Select
Microsoft Access Database File from the list and click Next. - Select a copy of the database file not in your directory and enter any login information
- Click OK
Now Visual Studio will ask you:
The selected connection uses a local data file that is not in the current project. Do you want to copy the file to the project and change the connection?
If you copy the data file to your project, it will be copied to the output project each time the application starts. Press F1 for information on controlling this behavior.
Click Yes, and Visual Studio will add the database to your project and create a connection string that points to the copied database.
- Now, you probably want to save the connection string in App.Config so you don't have to rebuild it every time you want to use the database. Most likely, this will save a connection string that uses
|DataDirectory| . Modify the contents of this page as you wish. - Go through the rest of the wizard and set up your database as needed.
The wizard should configure the connection string and build the information so that your application works regardless of where you take it.
EDIT - my connection string saved in App.config looks like
<connectionStrings> <add name="SOAccessDatabase.My.MySettings.Students_2000formatConnectionString" connectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\Students_2000format.mdb" providerName="System.Data.OleDb" /> </connectionStrings>
I also have a Build Action database file set to "Copy Always".
source share