App.Config与| DataDirectory | \database.mdf和完整路径 [英] App.Config with |DataDirectory|\database.mdf and full path

查看:153
本文介绍了App.Config与| DataDirectory | \database.mdf和完整路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好



我有一个App.Config文件来建立连接字符串。这是我的代码

Hi all

I have an App.Config file to make connection string. this is my code

<connectionStrings>
    <add name="ConnectionString" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;User Instance=True"/>
  </connectionStrings>





当我使用此代码时





when I use this code where

AttachDbFilename=|DataDirectory|\Database.mdf 

我的数据没有保存在数据库中,但当我使用像这样的完整路径时/>

my data hasn't saved in database but when I use full path like this

AttachDbFilenameC:\Users\user1\Documents\...\Database.mdf



数据已成功保存。



发生了什么事?我该怎么办?



谢谢


data has saved successfully.

what's happened? What can I do?

thanks

推荐答案

连接到SQL Server Express数据库

________________________________________

通过将数据库服务器指定为本地SQL Server Express数据源,您可以像连接到任何SQL Server数据库一样连接到SQL Server Express数据库。例如,以下连接字符串连接到名为Customers的数据库。

Connecting to a SQL Server Express Database
________________________________________
You can connect to a SQL Server Express database just like you would connect to any SQL Server database by specifying the database server as the local SQL Server Express data source. For example, the following connection string connects to a database named Customers.
Data Source=.\SQLEXPRESS;Initial Catalog=Customers;Integrated Security=True;



您还可以使用AttachDBFilename连接字符串属性代替InitialCatalog或数据库连接字符串属性来指定要附加到的数据库文件。使用文件名连接到数据库简化了使用应用程序部署数据库(前提是目标服务器正在运行SQL Server Express)。例如,以下连接字符串连接到存储在Customers.mdf文件中的数据库。


You can also specify a database file to attach to by using the AttachDBFilename connection-string attribute in place of the InitialCatalog or Database connection-string attributes. Connecting to the database by using a file name simplifies deploying your database with your application (provided the target server is running SQL Server Express). For example, the following connection string connects to a database that is stored in the Customers.mdf file.

Data Source=.\SQLEXPRESS;AttachDbFileName=e:\data\Customers.mdf;Integrated Security=True;User Instance=True



ASP.NET提供了一个方便的选项,用于将数据存储在Web应用程序的App_Data目录中。不响应Web请求而提供App_Data目录的内容,这样可以提高应用程序数据的安全性。为方便起见,您可以提供| DataDirectory |连接字符串变量代替应用程序的App_Data目录的文件路径。 ASP.NET功能(例如SqlDataSource控件或成员资格,角色,用户配置文件,Web部件个性化等提供程序)将自动将文件路径替换为App_Data目录中的| DataDirectory |。打开与数据库的连接时的connection-string变量。这可确保在将Web应用程序移动到其他目录时,数据库的路径保持最新。以下代码示例显示了包含| DataDirectory |的连接字符串连接字符串变量。


ASP.NET provides a convenient option for storing data in the App_Data directory of a Web application. Contents of the App_Data directory are not served in response to Web requests, which improves the security of the data for your application. As an added convenience, you can supply the |DataDirectory| connection string variable in place of the file path to the App_Data directory for your application. ASP.NET features — such as the SqlDataSource control or the providers for membership, roles, user profiles, Web Parts personalization, and so on — will automatically substitute the file path to the App_Data directory for the |DataDirectory| connection-string variable when opening a connection to the database. This ensures that the path to your database remains current if your Web application is moved to a different directory. The following code example shows a connection string that includes the |DataDirectory| connection-string variable.

Data Source=.\SQLEXPRESS;AttachDbFileName=|DataDirectory|Customers.mdf;Integrated Security=True;User Instance=True



您可以通过右键单击解决方案资源管理器中的数据库并选择分离选项,或右键单击服务器资源管理器中的数据库并选择关闭连接,来关闭Visual Web Developer持有的连接。运行或调试Web应用程序时,Visual Web Developer将自动关闭所有打开的数据库连接。

此外,如果需要释放与SQL Server Express数据库的任何打开连接,则可以卸载Web应用程序通过使用Internet信息服务管理器(IIS管理器)。您还可以通过将名为App_offline.htm的HTML文件添加到Web应用程序的根目录来卸载Web应用程序。若要允许Web应用程序再次开始响应Web请求,只需删除App_offline.htm文件即可。如果要将数据库复制或移动到新位置,则需要释放与SQL Server Express数据库的打开连接。





http://msdn.microsoft.com/en-us/library/ms247257(v = vs.100).ASPX [ ^ ]


这篇关于App.Config与| DataDirectory | \database.mdf和完整路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆