简体   繁体   English

ASP.Net的web.config中的连接字符串问题

[英]connection string issue in web.config of ASP.Net

I am using VSTS 2010 + C# + .Net 4.0 to develop an ASP.Net application using SQL Server 2008 Enterprise as database. 我正在使用VSTS 2010 + C#+ .Net 4.0开发一个使用SQL Server 2008 Enterprise作为数据库的ASP.Net应用程序。 I am learning someone else's code. 我正在学习别人的代码。 I notice code like this, I am not sure whether the usage is correct and so I come here to ask for advice. 我注意到这样的代码,我不确定用法是否正确,所以我来这里寻求建议。

In the code, I see some code like this, I want to know whether using such method to read connection string from web.config is correct? 在代码中,我看到这样的代码,我想知道使用这种方法从web.config中读取连接字符串是否正确?

ConfigurationManager.ConnectionStrings["DBConnectinString"].ConnectionString

and such code is used to read connection string from web.config like below, please notice that connection string is defined outside of system.web section. 并且此类代码用于从web.config中读取连接字符串,如下所示,请注意,连接字符串是在system.web部分之外定义的。

<?xml version="1.0"?>
<configuration>
  <configSections>
       <!--this section is empty-->
  </configSections>
  <appSettings>
    ...... content of appSettings
  </appSettings>
  <connectionStrings>
    <add name="DBConnectinString" connectionString="data Source=.;uid=foo;pwd=foo;database=FOODB" providerName="System.Data.SqlClient"/>
  </connectionStrings>
<system.web>
... ... content of system.web
</system.web>
</configuration>

That is the standard place to define connection strings in web.config, yes. 那是在web.config中定义连接字符串的标准位置,是的。

See this for more info: http://msdn.microsoft.com/en-us/library/ms178411.aspx 请参阅此以获取更多信息: http : //msdn.microsoft.com/zh-cn/library/ms178411.aspx

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

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