类库中的ConnectionString

时间:2011-04-27 15:26:43

标签: vb.net connection-string class-library

我正在将两个可以共享很多相同类的项目合并到一个带有两个Web应用程序和一个共享类库的解决方案中。

我实际上只是将所有类转储到类库项目中,正如预期的那样,我有很多错误要修复。我目前的主要问题是连接字符串。目前我有这个(显然不会起作用):

    ''' <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

1 个答案:

答案 0 :(得分:0)

如何在每个WebApplication中存储ConnectionStrings并将特定连接字符串传递给instanciation上的类库。我不知道你是如何设计类库的(静态方法,单例或其他),但也许你可以将它们(每个ConnectionString)传递给构造函数来设置一个你以后使用的实例变量(和以前一样)。