1

我有一个类库,其中包含由 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 文件中声明连接字符串的方式或检索它的方法是否有问题?

4

2 回答 2

1

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

您可以查看此处给出的解决方案

于 2013-10-29T09:55:28.183 回答
1

像这样添加连接字符串:ConfigurationManager.ConnectionStrings 属性

示例代码:

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
于 2013-10-29T10:00:31.040 回答