我合併我的兩個項目,可以共享許多相同的類到一個解決方案與兩個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
實例化時,必須將其傳遞到數據訪問層。 – Jack 2011-04-27 15:32:26