无法从app.config获取连接字符串

时间:2013-10-29 09:54:04

标签: vb.net wcf connection-string app-config

我有一个类库,其中包含由asp.net Web应用程序和WCF Web服务共享的类。库中的类需要从数据库中检索信息,因此库在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>

这是检索连接字符串的方法:

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

问题在于,当方法运行时,“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: 

我在app.config文件中声明连接字符串的方式或者检索它的方法有问题吗?

2 个答案:

答案 0 :(得分:1)

将连接字符串添加到asp.net应用程序的web.config

您可以查看给定here

的解决方案

答案 1 :(得分:1)

添加如下连接字符串:ConfigurationManager.ConnectionStrings Property

示例代码:

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