目前我正在尝试从asp.net 4.5连接SQL Server DB,将连接字符串保留在Web.config文件中,使用以下代码检索连接字符串,但其retunr 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,但这在使用时也会出现运行时错误。如果我有任何错误,请告诉我。
答案 0 :(得分:4)
虽然这只是一个建议/要检查的事情,但我将其添加为答案
1)您能否检查一下web.config是否位于正确的位置? 2)另外,你有任何web.config(覆盖主web.config)在与你的页面相同的文件夹中吗?
答案 1 :(得分:0)
尝试以下代码:
string connectionstring = ConfigurationManager.ConnectionStrings["ConString"].ConnectionString.ToString();
我希望它会对你有所帮助.. :)
答案 2 :(得分:0)
我使用此代码。它适用于我们的应用程序。
如果这不起作用,你还有其他一些问题。
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>