我使用此代码。它适用于我们的应用程序。
如果这不起作用,您还有其他问题。
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="YOUR_APPLICATION_NAME""
providerName="System.Data.SqlClient"/>
</connectionStrings>