无法从app.config中的连接字符串(Can't get the connection

2019-09-29 06:36发布

我有一个包含由一个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将”得到“没有”的值,然后下面的异常被抛出:

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文件我的连接字符串,或检索它的方法呢?

Answer 1:

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

您可以检查给出的解决方案在这里



Answer 2:

添加连接字符串是这样的: 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


文章来源: Can't get the connection string from app.config