【问题标题】:Not able to get connection string from Web.config file in asp.net 4.5无法从 asp.net 4.5 中的 Web.config 文件中获取连接字符串
【发布时间】:2013-07-05 12:10:38
【问题描述】:

目前我正在尝试从 asp.net 4.5 连接 SQL server DB,将连接字符串保留在 Web.config 文件中,使用以下代码检索连接字符串,但它的返回值为 NULL,

ConnectionStringSettings connString = ConfigurationManager.ConnectionStrings["ConString"];                 
SqlConnection Con = new SqlConnection(connString.ConnectionString);

Web.Config 中的连接字符串是这样的

<connectionStrings>
  <add name="ConString" 
    connectionString="Data Source=myservername;Initial Catalog=dbname;User ID=userid;Password=password;Integrated Security=True" 
    xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/>
</connectionStrings> 

我不明白为什么它返回 null 值,以前在 3.5 中我使用以下代码获取连接字符串,

Con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["ConString"].ConnectionString);

它在 asp.net 3.5 中运行良好,但是在使用时也会出现运行时错误。如果我做错了,请告诉我。

【问题讨论】:

  • connString为空,还是connStringConnectionString属性为空?
  • 什么是运行时错误?
  • Get DataObject 引用未设置为对象实例时出错。
  • Transform 和 Locator 属性有什么用?我以前从未在连接字符串设置中看到过。
  • 它在 4.5 中自动出现,我可以删除它

标签: asp.net asp.net-mvc-4 web-config connection-string


【解决方案1】:

虽然这只是一个建议/需要检查的事情,但我将其添加为答案

1) 请检查一下 web.config 是否在正确的位置?
2) 另外,您是否在与您的页面相同的文件夹中有任何 web.config(覆盖主 web.config)?

【讨论】:

    【解决方案2】:

    试试下面的代码:

    string connectionstring = ConfigurationManager.ConnectionStrings["ConString"].ConnectionString.ToString();
    

    希望对你有帮助.. :)

    【讨论】:

    • ConnectionString 属性 is 是一个字符串,除了无用的 ToString() 之外,它是他正在使用的代码。
    • 收到此错误“获取 DataObject 引用时出错,未将其设置为对象的实例。”
    • 是的,我忘了删除那个.. :P
    【解决方案3】:

    我使用此代码。它适用于我们的应用程序。
    如果这不起作用,您还有其他问题。

        Protected Shared strStaticConnectionString As String = Nothing
    
        Public Shared Function GetConnectionString() As String
            Return GetConnectionString(Nothing)
        End Function
    
        ' Requires reference to System.Configuration
        Public Shared Function GetConnectionString(ByVal strIntitialCatalog As String) As String
            Dim strReturnValue As String = Nothing
    
            If String.IsNullOrEmpty(strStaticConnectionString) Then
                Dim strConnectionStringName As String = System.Environment.MachineName
    
                If String.IsNullOrEmpty(strConnectionStringName) Then
                    strConnectionStringName = "LocalSqlServer"
                End If
    
                ' Walk through the collection and return the first 
                ' connection string matching the connectionString name.
                Dim settings As System.Configuration.ConnectionStringSettingsCollection = System.Configuration.ConfigurationManager.ConnectionStrings
                If (settings IsNot Nothing) Then
                    For Each cs As System.Configuration.ConnectionStringSettings In settings
                        If StringComparer.OrdinalIgnoreCase.Equals(cs.Name, strConnectionStringName) Then
                            strReturnValue = cs.ConnectionString
                            Exit For
                        End If
                    Next
                End If
    
                If String.IsNullOrEmpty(strReturnValue) Then
                    strConnectionStringName = "server"
    
                    Dim conString As System.Configuration.ConnectionStringSettings = System.Configuration.ConfigurationManager.ConnectionStrings(strConnectionStringName)
    
                    If conString IsNot Nothing Then
                        strReturnValue = conString.ConnectionString
                    End If
                End If
    
                settings = Nothing
                strConnectionStringName = Nothing
            Else
                If String.IsNullOrEmpty(strIntitialCatalog) Then
                    Return strStaticConnectionString
                End If
    
                strReturnValue = strStaticConnectionString
            End If
    
            If String.IsNullOrEmpty(strReturnValue) Then
                strReturnValue = GetConnectionString_Old()
    
                If String.IsNullOrEmpty(strReturnValue) Then
                    Throw New ArgumentNullException("ConnectionString ""server"" in file web.config.")
                End If
            Else
                Dim sb As New System.Data.SqlClient.SqlConnectionStringBuilder(strReturnValue)
    
                If String.IsNullOrEmpty(strStaticConnectionString) Then
                    If Not sb.IntegratedSecurity Then
                        sb.Password = DeCrypt(sb.Password)
                    End If
                    strReturnValue = sb.ConnectionString
                    strStaticConnectionString = strReturnValue
                End If
    
                If Not String.IsNullOrEmpty(strIntitialCatalog) Then
                    sb.InitialCatalog = strIntitialCatalog
                End If
    
                strReturnValue = sb.ConnectionString
                sb = Nothing
            End If
    
            Return strReturnValue
        End Function ' GetConnectionString
    

    然后这个 web.config 文件:

    <?xml version="1.0"?>
    <configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0">
    
      <appSettings>
    
      </appSettings>
    
    
      <connectionStrings configSource="connections.config"/>
    
    
      <system.web></system.web>
    
    
      <system.webServer></system.webServer>
    
    </configuration>
    

    还有这个连接字符串文件(connections.config)

    <?xml version="1.0"?>
    <connectionStrings>
    
      <remove name="server"/>
      <add name="server" connectionString="Data Source=localhost;
        Initial Catalog=YOUR_DB;
        Persist Security Info=False;
        User Id=YOUR_USER;
        Password=YOUR_PW;
        MultipleActiveResultSets=False;
        Packet Size=4096;
        Application Name=&quot;YOUR_APPLICATION_NAME&quot;" 
        providerName="System.Data.SqlClient"/>
    
    </connectionStrings>
    

    【讨论】:

    • 不确定这如何帮助 OP 解决他们的问题 - 他们使用的代码应该可以工作。您的代码虽然很有趣,但对于他们的问题(IMO)来说太过分了(更不用说 OP 正在使用 C#)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-03-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-05
    • 2016-03-26
    相关资源
    最近更新 更多