【问题标题】:connection string issue in web.config of ASP.NetASP.Net 的 web.config 中的连接字符串问题
【发布时间】:2010-10-10 06:23:49
【问题描述】:

我正在使用 VSTS 2010 + C# + .Net 4.0 开发一个使用 SQL Server 2008 Enterprise 作为数据库的 ASP.Net 应用程序。我正在学习别人的代码。我注意到这样的代码,我不确定使用是否正确,所以我来这里寻求建议。

在代码中,我看到一些这样的代码,我想知道使用这种方法从web.config中读取连接字符串是否正确?

ConfigurationManager.ConnectionStrings["DBConnectinString"].ConnectionString

此类代码用于从 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>

【问题讨论】:

    标签: c# asp.net .net visual-studio-2010 web-config


    【解决方案1】:

    这是在 web.config 中定义连接字符串的标准位置,是的。

    更多信息请参见:http://msdn.microsoft.com/en-us/library/ms178411.aspx

    【讨论】:

      猜你喜欢
      • 2019-02-21
      • 2016-03-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多