vb.net 无法从 app.config 获取连接字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19654822/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can't get the connection string from app.config
提问by ZipionLive
I have a class library that contains classes shared by an asp.net web application and a WCF web service. The classes inside the library need to retrieve information from the database, and so the library has an entry in the app.config for the connection string, and a method that retrieves that connection string.
我有一个类库,其中包含由 asp.net Web 应用程序和 WCF Web 服务共享的类。库中的类需要从数据库中检索信息,因此库在 app.config 中有一个用于连接字符串的条目,以及一个检索该连接字符串的方法。
Here's how I declare my connection string in the app.config :
这是我在 app.config 中声明连接字符串的方式:
<configuration>
<connectionStrings>
<add name="connstring"
connectionString="Data Source=MYSERVER;Initial Catalog=MY_DB;User Id=mylogin;Password=mypassword;"
providerName="System.Data.SqlClient"/>
</connectionStrings>
[...]
</configuration>
And here's the method that retrieves the connection string :
这是检索连接字符串的方法:
Imports System.Configuration
Imports System.Data.SqlClient
Imports System.Data.OleDb
Public Module modGen
Public Function gGetConnectionString() As String
Dim conn As ConnectionStringSettings = ConfigurationManager.ConnectionStrings("connstring")
Dim connString As String = conn.ConnectionString
Return connString
End Function
The problem is that, when the method runs, "conn" gets "Nothing" for value and the following exception is then thrown :
问题是,当方法运行时,“conn”的值是“Nothing”,然后抛出以下异常:
System.TypeInitializationException was caught
Message=The type initializer for 'CHK.TRK.CLS.modGen' threw an exception.
Source=CHK.TRK.CLS
TypeName=CHK.TRK.CLS.modGen
StackTrace:
at CHK.TRK.CLS.modGen.gGetConnectionString()
at CHK.TRK.CLS.modAuth.CheckUser(String logon, String pwHash) in H:\WorkSpace\CHKTRK\CHKTRK\CHKTRKClasses\Mod\modAuth.vb:line 141
at CHK.TRK.CLS.modAuth.Authenticate(String logon, String pwhash) in H:\WorkSpace\CHKTRK\CHKTRK\CHKTRKClasses\Mod\modAuth.vb:line 9
at CHKTRKWebService.CheckTrackService.Authenticate(String login, String pwHash) in H:\WorkSpace\CHKTRK\CHKTRK\CHKTRKWebService\CheckTrackService.svc.vb:line 20
InnerException: System.NullReferenceException
Message=Object reference not set to an instance of an object.
Source=CHK.TRK.CLS
StackTrace:
at CHK.TRK.CLS.modGen..cctor() in H:\WorkSpace\CHKTRK\CHKTRK\CHKTRKClasses\Mod\modGen.vb:line 138
InnerException:
Is there something wrong with the way I declare my connection string in the app.config file, or with the method that retrieves it ?
我在 app.config 文件中声明连接字符串的方式或检索它的方法有问题吗?
采纳答案by Giannis Paraskevopoulos
回答by Jignesh.Raj
Add the connection string like this :ConfigurationManager.ConnectionStrings Property
添加这样的连接字符串:ConfigurationManager.ConnectionStrings Property
Sample Code:
示例代码:
Public Shared Sub ReadConnectionStrings()
' Get the ConnectionStrings collection.
Dim connections _
As ConnectionStringSettingsCollection = _
ConfigurationManager.ConnectionStrings
If connections.Count <> 0 Then
Console.WriteLine()
Console.WriteLine( _
"Using ConnectionStrings property.")
Console.WriteLine( _
"Connection strings:")
' Get the collection elements.
For Each connection _
As ConnectionStringSettings In connections
Dim name As String = connection.Name
Dim provider As String = _
connection.ProviderName
Dim connectionString As String = _
connection.ConnectionString
Console.WriteLine( _
"Name: {0}", name)
Console.WriteLine( _
"Connection string: {0}", _
connectionString)
Console.WriteLine( _
"Provider: {0}", provider)
Next
Else
Console.WriteLine()
Console.WriteLine( _
"No connection string is defined.")
Console.WriteLine()
End If
End Sub

