类库中的 ConnectionString

发布于 2024-11-03 09:02:13 字数 5873 浏览 4 评论 0原文

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

现在类不在网络应用程序中,我该如何处理我的连接字符串?

我真的觉得我在这里遗漏了一些东西,所以下面我包含了 BLL 和 DAL 中的一个类的示例。我无法将连接字符串传递给 DAL 的构造函数 - 它表示它不能有任何参数。

比尔:

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

I'm merging two of my projects that can share a lot of the same classes into one solution with two web applications and a shared class library.

I've literally just dumped all the classes into the class library project and as expected I've got a ton of errors to fix. My main problem at the moment is the connection string. Currently I have this (which is obviously not going to work):

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

What do I do with my connection strings now the classes are not in a web application?

I really feel like I'm missing something here so below I'm include an example of a class from the BLL and DAL. I can't pass in the connection string to the constructor of the DAL - it says it can't have any parameters.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

沦落红尘 2024-11-10 09:02:13

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

How about storing the ConnectionStrings in each WebApplication and passing the specific connection string to the class library on instanciation. I don't know how you designed the class library (static methods, singleton, or whatever) but maybe you could pass them (each ConnectionString) to the constructor to set an instance variable which you later use (the same way like before).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文