简体   繁体   English

Access数据库连接C#本地控制器

[英]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. 这将对应于您的应用程序中的/MyData目录。 Be aware of the security concern that the .mdb file may be visible to [un]intentional foul play. 请注意安全方面的问题,.mdb文件可能对[意外]犯规是可见的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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