Reputation: 5
I want my connection to the database to be available all the time, so if i move the folder with the project, to an other computer, the connection to be made automaticaly. So, how can i change this connection:
this.oleDbConnection1.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=\"C:\\Documents and Settings\\Cristi\\Do" +
"cuments\\Visual Studio 2008\\Projects\\WindowsApplication3\\bd1.mdb\"";
??? It should read the project directory or something. I don't know. Any ideas? Thank You!
Upvotes: 0
Views: 4004
Reputation: 5533
If you add the access database to the project, and in the file properties set the Copy to Output Directory property to Copy Always (or Copy if Newer if appropriate), you can use a connection string similar to this:
this.oleDbConnection1.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=\".\\bd1.mdb\"";
This will work because the database will be located in the same folder as your binaries.
Similarly, you could use a relative path to your database, relative to where the executing assembly is located.
Upvotes: 2
Reputation: 12815
Sounds like you want to use a relative path in your connection string.
Something like this:
this.oleDbConnection1.ConnectionString =
"Provider=Microsoft.Jet.OleDb.4.0;Data Source=" +
Server.MapPath("~\\MyData\\MyDatabase.mdb");
This will correspond to the /MyData
directory within your application. Be aware of the security concern that the .mdb file may be visible to [un]intentional foul play.
Upvotes: 0