【问题标题】:ConnectionString In Class Library类库中的连接字符串
【发布时间】:2011-08-13 23:26:01
【问题描述】:

我正在将我的两个可以共享许多相同类的项目合并到一个具有两个 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

达尔:

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

【问题讨论】:

  • 实例化时必须将其传递给数据访问层。

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


【解决方案1】:

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

【讨论】:

  • 是的,我想我可以很容易地做到这一点。这是给定此示例的处理连接字符串的标准/最佳实践方式吗?
  • 虽然说我可以轻松做到,但对我来说似乎不太合适。每次我想调用执行数据访问的函数或子程序时,我都需要传入连接字符串。这不符合不要重复自己。
  • @Westicle 每次实例化只需执行一次。 Dim class As New YourClass(connectionsString) 所以在Sub New 中,你可以给你的_connectionString 属性赋值:_connectionString = connectionString。希望这会有所帮助。
  • @Jack 但是构造函数在类库中,所以我仍然需要每次都将连接字符串传递给它?
  • @Jack 我发现由于我的课程设置方式,我目前无法执行此操作。在我的 DAO 中,构造函数是共享的,它不会让我传入参数。我根据书中的例子创建了我的课程。你认为我需要重新编写我的代码吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-02-23
  • 1970-01-01
  • 1970-01-01
  • 2020-06-04
  • 2017-05-11
相关资源
最近更新 更多