DotNetNuke 資料訪問層
資料訪問層(DAL)
資料訪問層(DAL)必須實現資料提供者抽象類中宣告的方法。然而,每一個DAL提供者在實現這些方法時也許很不相同。這種處理允許提供者靈活的選擇他們自己的資料庫訪問協議(也就是說:.NET管理的OleDB, ODBC等)。同樣也允許提供者處理資料庫平臺之間的所有不同之處(例如:儲存過程,sql語言語法,@@IDENTITY)。
每一個資料提供者必須為其在web.config中的自定義屬性指定一個實現方法。
Imports System
Imports System.Data
Imports System.Data.SqlClient
Imports Microsoft.ApplicationBlocks.Data
Imports System.IO
Imports System.Web
Imports DotNetNuke
Namespace DotNetNuke.Data
Public Class SqlDataProvider
Inherits DataProvider
Private Const ProviderType As String = "data"
Private _providerConfiguration As ProviderConfiguration = ProviderConfiguration.GetProviderConfiguration(ProviderType)
Private _connectionString As String
Private _providerPath As String
Private _objectQualifier As String
Private _databaseOwner As String
Public Sub New()
' Read the configuration specific information for this provider
Dim objProvider As Provider = CType(_providerConfiguration.Providers(_providerConfiguration.DefaultProvider), Provider)
' Read the attributes for this provider
_connectionString = objProvider.Attributes("connectionString")
_providerPath = objProvider.Attributes("providerPath")
_objectQualifier = objProvider.Attributes("objectQualifier")
If _objectQualifier <> "" And _objectQualifier.EndsWith("_") = False Then
_objectQualifier += "_"
End If
_databaseOwner = objProvider.Attributes("databaseOwner")
If _databaseOwner <> "" And _databaseOwner.EndsWith(".") = False Then
_databaseOwner += "."
End If
End Sub
Public ReadOnly Property ConnectionString() As String
Get
Return _connectionString
End Get
End Property
Public ReadOnly Property ProviderPath() As String
Get
Return _providerPath
End Get
End Property
Public ReadOnly Property ObjectQualifier() As String
Get
Return _objectQualifier
End Get
End Property
Public ReadOnly Property DatabaseOwner() As String
Get
Return _databaseOwner
End Get
End Property
資料訪問方法必須涉及成簡單的查詢(例如 單一的select,insert,update,delete),以便於在所有的資料庫平臺上他們都能被實現。業務邏輯(例如條件分支,計算或區域性變數)應該在業務邏輯層實現,這樣才能從資料庫抽象出來並集中到一個應用程式(模組)裡處理。如果你經常用那些使你可以在資料庫層實現程式邏輯的富sql語言變數工作的話,這種資料庫訪問是相當簡單的。
DNN中的Sql server/msde 資料提供者用了儲存過程作為最好的資料訪問技術。
' links module
Public Overrides Function GetLinks(ByVal ModuleId As Integer) As IDataReader
Return CType(SqlHelper.ExecuteReader(ConnectionString, DatabaseOwner & ObjectQualifier & "GetLinks", ModuleId), IDataReader)
End Function
Public Overrides Function GetLink(ByVal ItemId As Integer, ByVal ModuleId As Integer) As IDataReader
Return CType(SqlHelper.ExecuteReader(ConnectionString, DatabaseOwner & ObjectQualifier & "GetLink", ItemId, ModuleId), IDataReader)
End Function
Public Overrides Sub DeleteLink(ByVal ItemId As Integer)
SqlHelper.ExecuteNonQuery(ConnectionString, DatabaseOwner & ObjectQualifier & "DeleteLink", ItemId)
End Sub
Public Overrides Sub AddLink(ByVal ModuleId As Integer, ByVal UserName As String, ByVal Title As String, ByVal Url As String, ByVal MobileUrl As String, ByVal ViewOrder As String, ByVal Description As String, ByVal NewWindow As Boolean)
SqlHelper.ExecuteNonQuery(ConnectionString, DatabaseOwner & ObjectQualifier & "AddLink", ModuleId, UserName, Title, Url, MobileUrl, IIf(ViewOrder <> "", ViewOrder, DBNull.Value), Description, NewWindow)
End Sub
Public Overrides Sub UpdateLink(ByVal ItemId As Integer, ByVal UserName As String, ByVal Title As String, ByVal Url As String, ByVal MobileUrl As String, ByVal ViewOrder As String, ByVal Description As String, ByVal NewWindow As Boolean)
SqlHelper.ExecuteNonQuery(ConnectionString, DatabaseOwner & ObjectQualifier & "UpdateLink", ItemId, UserName, Title, Url, MobileUrl, IIf(ViewOrder <> "", ViewOrder, DBNull.Value), Description, NewWindow)
End Sub
DNN用到儲存過程(儲存過程查詢)但是沒有用引數自動查詢的特性( CommandBuilder.DeriveParameters ),因此引數必須明確的定義。
' links module
Public Overrides Function GetLinks(ByVal ModuleId As Integer) As IDataReader
Return CType(OleDBHelper.ExecuteReader(ConnectionString, CommandType.StoredProcedure, ObjectQualifier & "GetLinks", _
New OleDbParameter("@ModuleId", ModuleId)), IDataReader)
End Function
Public Overrides Function GetLink(ByVal ItemId As Integer, ByVal ModuleId As Integer) As IDataReader
Return CType(OleDBHelper.ExecuteReader(ConnectionString, CommandType.StoredProcedure, ObjectQualifier & "GetLink", _
New OleDbParameter("@ItemId", ItemId), _
New OleDbParameter("@ModuleId", ModuleId)), IDataReader)
End Function
Public Overrides Sub DeleteLink(ByVal ItemId As Integer)
OleDBHelper.ExecuteNonQuery(ConnectionString, CommandType.StoredProcedure, ObjectQualifier & "DeleteLink", _
New OleDbParameter("@ItemId", ItemId))
End Sub
Public Overrides Sub AddLink(ByVal ModuleId As Integer, ByVal UserName As String, ByVal Title As String, ByVal Url As String, ByVal MobileUrl As String, ByVal ViewOrder As String, ByVal Description As String, ByVal NewWindow As Boolean)
OleDBHelper.ExecuteNonQuery(ConnectionString, CommandType.StoredProcedure, ObjectQualifier & "AddLink", _
New OleDbParameter("@ModuleId", ModuleId), _
New OleDbParameter("@UserName", UserName), _
New OleDbParameter("@Title", Title), _
New OleDbParameter("@Url", Url), _
New OleDbParameter("@MobileUrl", MobileUrl), _
New OleDbParameter("@ViewOrder", IIf(ViewOrder <> "", ViewOrder, DBNull.Value)), _
New OleDbParameter("@Description", Description), _
New OleDbParameter("@NewWindow", NewWindow))
End Sub
Public Overrides Sub UpdateLink(ByVal ItemId As Integer, ByVal UserName As String, ByVal Title As String, ByVal Url As String, ByVal MobileUrl As String, ByVal ViewOrder As String, ByVal Description As String, ByVal NewWindow As Boolean)
OleDBHelper.ExecuteNonQuery(ConnectionString, CommandType.StoredProcedure, ObjectQualifier & "UpdateLink", _
New OleDbParameter("@ItemId", ItemId), _
New OleDbParameter("@UserName", UserName), _
New OleDbParameter("@Title", Title), _
New OleDbParameter("@Url", Url), _
New OleDbParameter("@MobileUrl", MobileUrl), _
New OleDbParameter("@ViewOrder", IIf(ViewOrder <> "", ViewOrder, DBNull.Value)), _
New OleDbParameter("@Description", Description), _
New OleDbParameter("@NewWindow", NewWindow))
End Sub
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/12639172/viewspace-414920/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 資料訪問層
- 資料訪問層的優化思路優化
- 資料訪問層基礎結構設計
- 如何在資料訪問層中引用資料庫連線池?資料庫
- spring mvc 的jpa JpaRepository資料層訪問SpringMVC
- 使用JDBC構建簡單的資料訪問層JDBC
- ClownFish:比寫程式碼還快的通用資料訪問層
- 2-3法則設計分散式資料訪問層分散式
- 使用2-3法則設計分散式資料訪問層分散式
- 持續整合之路——資料訪問層的單元測試(續)
- 使用MyGeneration生成儲存過程和資料訪問層程式碼儲存過程
- Spring資料訪問Spring
- JDBC資料庫訪問JDBC資料庫
- 資料訪問 - EntityFramework整合Framework
- 控制資料訪問(一)
- jboss訪問資料庫的問題資料庫
- 定製可訪問資料
- Oracle資料庫訪問控制Oracle資料庫
- 異構資料庫訪問資料庫
- Traveller資料訪問路徑
- 資料庫如何處理大資料訪問資料庫大資料
- 兩個資料訪問受限的問題
- 求助:資料庫併發訪問問題資料庫
- Ext迭代陣列訪問資料陣列
- 外網訪問MySQL資料庫MySql資料庫
- SQL Server 資料訪問策略:CLRMESQLServer
- Oracle資料庫限制訪問IPOracle資料庫
- Ubuntu訪問Windows共享資料夾UbuntuWindows
- C#訪問MySQL資料庫C#MySql資料庫
- C#訪問SQLite資料庫C#SQLite資料庫
- 訪問HyperSQL資料庫的方法SQL資料庫
- 限制特定IP訪問資料庫資料庫
- Flex 3處理資料 訪問 XML 資料4FlexXML
- JAVA訪問雲資料mysql出現問題JavaMySql
- 如何限制ip訪問Oracle資料庫Oracle資料庫
- spring boot(四)資料訪問模組Spring Boot
- ABP 資料訪問 - IRepository 倉儲
- 【SpringBoot實戰】資料訪問Spring Boot