简体   繁体   English

我如何编辑app.config文件以使应用程序从同一文件夹附加数据库

[英]how can i edit the app.config file to let the application attach the database from the same folder

I'm using VS2008 with MS Sql Server 2005 to create an application. 我正在将VS2008与MS Sql Server 2005一起使用来创建应用程序。

I've used linq to query the database, and when I run the application from debug folder on my PC, it runs correctly, but when I copy the debug folder to another PC and try to run it an error message say that can not attach the database or can not find the database. 我已经使用linq来查询数据库,并且当我从PC上的debug文件夹运行应用程序时,它可以正常运行,但是当我将debug文件夹复制到另一台PC并尝试运行它时,出现一条错误消息,提示无法附加数据库或找不到数据库。

I have copied the database files into the same folder as the application DLLs, but how can I edit the config file at the debug folder to let the application attach the database from the same folder? 我已将数据库文件复制到与应用程序DLLs相同的文件夹中,但是如何在调试文件夹中编辑配置文件,以使应用程序从同一文件夹附加数据库?

Put your database in a directory called App_Data and use the connection string like this : 将数据库放置在名为App_Data的目录中,并使用如下连接字符串:

Server=.\SQLExpress;AttachDbFilename=|DataDirectory|mydbfile.mdf;
Database=dbname;Trusted_Connection=Yes;

The final folder structure of the app will be like this : bin directory contains the compiled assemblies, and in the same level as bin folder have the App_Data folder and have the database inside this App_Data folder. 该应用程序的最终文件夹结构将如下所示:bin目录包含已编译的程序集,并且在与bin文件夹相同的级别中,具有App_Data文件夹,并且在此App_Data文件夹内具有数据库。

Change SQLExpress to your database, and supply any username password etc if needed also. SQLExpress更改为您的数据库,并根据需要提供任何用户名密码等。 Look here for examples of good connection strings. 在这里查看良好连接字符串的示例。

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

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