2011-04-27 60 views
0

我合併我的兩個項目,可以共享許多相同的類到一個解決方案與兩個Web應用程序和共享類庫。ConnectionString在類庫

我真的只是傾倒所有的類到類庫項目和預期我有大量的錯誤修復。目前我的主要問題是連接字符串。目前,我有這個(這顯然是行不通的):

''' <summary> 
    ''' Initialise the data access layer by loading the database connection string from the Web.Config file 
    ''' </summary> 
    ''' <remarks></remarks> 
    Shared Sub New() 

     _connectionString = WebConfigurationManager.ConnectionStrings("ClientFamilyManagementConnectionString").ConnectionString 

    End 

我該怎麼做我的連接字符串現在類不是在Web應用程序?

我真的覺得我在這裏錯過了一些東西,所以下面我包含了一個BLL和DAL類的例子。我無法將連接字符串傳遞給DAL的構造函數 - 它說它不能有任何參數。

BLL:

Imports Microsoft.VisualBasic 
Imports System.Collections.Generic 

Namespace CompanyName 

<Serializable()> Public Class SubMarketSector 

    Private _id As Integer 
    Private _name As String 

    Public Property ID() As Integer 
     Get 
      Return _id 
     End Get 
     Set(ByVal value As Integer) 
      _id = value 
     End Set 
    End Property 

    Public Property Name() As String 
     Get 
      Return _name 
     End Get 
     Set(ByVal value As String) 
      _name = value 
     End Set 
    End Property 

    Public Sub New() 

    End Sub 

    Public Shared Function GetAllSubMarketSectors() As List(Of CompanyName.SubMarketSector) 

     Dim newSubMarketSectorDAO As New SubMarketSectorDAO 
     Return newSubMarketSectorDAO.GetAllSubMarketSectors 

    End Function 

    Public Shared Function GetSubMarketSectorByID(ByVal subMarketSectorID As Integer) As CompanyName.SubMarketSector 

     Dim newSubMarketSectorDAO As New SubMarketSectorDAO 
     Return newSubMarketSectorDAO.GetSubMarketSectorByID(subMarketSectorID) 

    End Function 


End Class 

End Namespace 

DAL:

Namespace CompanyName 

Public Class SubMarketSectorDAO 

    Private Const MainSelectByStatement As String = "SELECT ID, Name FROM Sub_Market_Sectors" 
    Private Const MainOrderByStatement As String = " ORDER BY Name" 

    Private Shared ReadOnly _connectionString As String = String.Empty 

    ''' <summary> 
    ''' Initialise the data access layer by loading the database connection string from the Web.Config file 
    ''' </summary> 
    ''' <remarks></remarks> 
    Shared Sub New() 

     _connectionString = WebConfigurationManager.ConnectionStrings("PursuitsConnectionString").ConnectionString 

    End Sub 

    ''' <summary> 
    ''' Returns a List of all Sub Market Sectors 
    ''' </summary> 
    ''' <returns></returns> 
    ''' <remarks></remarks> 
    Public Function GetAllSubMarketSectors() As List(Of CompanyName.SubMarketSector) 

     ' Create the Connection 
     Dim currentConnection As SqlConnection = New SqlConnection(_connectionString) 

     ' Create the Command Object, set the CommandText, add any required Parameters and set the Connection 
     Dim currentCommand As New SqlCommand 
     currentCommand.CommandText = MainSelectByStatement & MainOrderByStatement 
     currentCommand.Connection = currentConnection 

     Dim listOfSubMarketSectors As New List(Of CompanyName.SubMarketSector) 

     Using currentConnection 

      ' Open the Connection 
      currentConnection.Open() 

      ' Create the DataReader and Execute the Command 
      Dim currentDataReader As SqlDataReader = currentCommand.ExecuteReader() 

      ' Populate the list with data 
      Do While currentDataReader.Read 

       Dim newSubMarketSector As CompanyName.SubMarketSector = PopulateSubMarketSector(currentDataReader) 

       listOfSubMarketSectors.Add(newSubMarketSector) 

      Loop 

     End Using 

     Return listOfSubMarketSectors 

    End Function 

    ''' <summary> 
    ''' Return a single Sub Market Sector 
    ''' </summary> 
    ''' <returns></returns> 
    ''' <remarks></remarks> 
    Public Function GetSubMarketSectorByID(ByVal subMarketSectorID As Integer) As CompanyName.SubMarketSector 

     ' Create the Connection 
     Dim currentConnection As SqlConnection = New SqlConnection(_connectionString) 

     ' Create the Command Object, set the CommandText, add any required Parameters and set the Connection 
     Dim currentCommand As New SqlCommand 
     currentCommand.CommandText = MainSelectByStatement & " WHERE ID = @subMarketSectorID" & MainOrderByStatement 
     currentCommand.Parameters.AddWithValue("@subMarketSectorID", subMarketSectorID) 
     currentCommand.Connection = currentConnection 

     Dim newSubMarketSector As New CompanyName.SubMarketSector 

     Using currentConnection 

      ' Open the Connection 
      currentConnection.Open() 

      ' Create the DataReader and Execute the Command 
      Dim currentDataReader As SqlDataReader = currentCommand.ExecuteReader() 

      ' Populate the Market Sector 
      Do While currentDataReader.Read 

       newSubMarketSector = PopulateSubMarketSector(currentDataReader) 

      Loop 

     End Using 

     Return newSubMarketSector 

    End Function 

    Private Function PopulateSubMarketSector(ByVal currentDataReader As SqlDataReader) As CompanyName.SubMarketSector 

     Dim newSubMarketSector As New CompanyName.SubMarketSector 

     If Not (currentDataReader.IsDBNull(currentDataReader.GetOrdinal("ID"))) Then 
      newSubMarketSector.ID = currentDataReader("ID") 
     End If 

     If Not (currentDataReader.IsDBNull(currentDataReader.GetOrdinal("Name"))) Then 
      newSubMarketSector.Name = currentDataReader("Name") 
     End If 

     Return newSubMarketSector 

    End Function 

End Class 

End Namespace 
+0

實例化時,必須將其傳遞到數據訪問層。 – Jack 2011-04-27 15:32:26

回答

0

如何在每個WebApplication的存儲的ConnectionStrings並通過特定的連接字符串類庫上instanciation。我不知道你是如何設計類庫(靜態方法,單例或其他),但也許你可以將它們(每個ConnectionString)傳遞給構造函數來設置一個實例變量,以後使用它(和以前一樣)。

+0

是的,我想我可以很容易地做到這一點。這是處理連接字符串的標準/最佳實踐方式嗎? – Westicle 2011-04-27 15:47:58

+0

說了我可以輕鬆做到這一點,對我來說看起來不太合適。每次我想調用一個執行數據訪問的函數或子函數時,我都需要傳入連接字符串。這不符合不要重複自己。 – Westicle 2011-04-27 16:13:11

+0

@Westicle每個實例化只需要執行一次。 Dim class As New YourClass(connectionsString)''因此在Sub New中,你可以給你的'_connectionString'屬性賦值:'_connectionString = connectionString'。希望這可以幫助。 – Jack 2011-04-27 18:07:27