0

目前我正在尝试从 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,但这在使用时也会出现运行时错误。如果我犯了任何错误,请告诉我。

4

3 回答 3

4

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

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

于 2013-07-05T14:15:16.783 回答
0

试试下面的代码:

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

我希望它会帮助你.. :)

于 2013-07-05T12:14:41.337 回答
-1

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

    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>
于 2013-07-05T12:24:07.840 回答