Я объединяю два моих проекта, которые могут совместно использовать множество одних и тех же классов, в одно решение с двумя веб-приложениями и общей библиотекой классов.
Я буквально только что свалил все классы впроект библиотеки классов и, как и ожидалось, мне нужно исправить кучу ошибок.Моя главная проблема на данный момент - это строка подключения.В настоящее время у меня есть это (что, очевидно, не будет работать):
''' <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 - он говорит, что не может иметь никаких параметров.
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