2013-10-29 56 views
0

我有一個類庫,其中包含由asp.net Web應用程序和WCF Web服務共享的類。庫中的類需要從數據庫中檢索信息,因此庫在連接字符串的app.config中有一個條目,並且有一個方法用於檢索該連接字符串。無法從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文件中聲明我的連接字符串的方式是否有問題,或者使用檢索它的方法嗎?

回答

1

將連接字符串添加到您的asp.net應用程序的web.config

您可以檢查給here

+0

它已經在兩個asp.net應用程序的Web.config和WCF服務。它和三個配置文件具有相同的名稱。 – ZipionLive

+0

你已經提到這個連接字符串在庫的app.config中。你確定它也存在於web.config中嗎? –

+1

是的,它在web應用程序的web.config中以完全相同的方式聲明。 – ZipionLive

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 
+0

這不是真正的答案,但它無論如何幫助了我很多,謝謝! – ZipionLive

+0

雅這不是真實的,但這是示例代碼爲獲取想法abot如何獲取連接字符串 –

相關問題