简体   繁体   中英

Access Database connect C# local director

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!

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM