这是我使用的两个类,它们在 EntLib5 中模仿该模式,而无需使用整个 entlib5 数据库及其依赖项。两个示例调用使用非查询,但有用于标量和数据集的 db 方法。你也可以很容易地为 datareader 添加一个。
使用事务调用类的示例
Private Sub SubmitEntity(ByVal entity As Entity)
Dim db As Database = DatabaseFactory.CreateDatabase()
db.ConnectionString = Me.ConnectionString
Dim transaction As SqlTransaction = Nothing
Dim cmd As DbCommand = db.GetStoredProcCommand("dbo.MystoredProc")
db.AddInParameter(cmd, "entityData", SqlDbType.VarChar, entity.Data)
db.AddInParameter(cmd, "enitityId", SqlDbType.Int, DBNull.Value, ParameterDirection.Output)
Try
transaction = db.Transaction
db.ExecuteNonQuery(cmd, db.Transaction)
transaction.Commit()
db.Dispose()
Catch ex As Exception
If transaction IsNot Nothing Then
transaction.Rollback()
End If
If db IsNot Nothing AndAlso db.Connection IsNot Nothing Then
db.Dispose()
End If
End Try
End Sub
无事务调用示例
Private Sub SubmitEntity(ByVal entity As Entity)
Dim db As Database = DatabaseFactory.CreateDatabase("the connection string")
Dim cmd As DbCommand = db.GetStoredProcCommand("dbo.MystoredProc")
db.AddInParameter(cmd, "entityData", SqlDbType.VarChar, entity.Data)
db.AddInParameter(cmd, "enitityId", SqlDbType.Int, DBNull.Value, ParameterDirection.Output)
db.ExecuteNonQuery(cmd, db.Transaction)
End Sub
数据库工厂类
Imports System.Data.SqlClient
Friend Class DatabaseFactory
Private Shared Property Connection As SqlConnection
Friend Shared Function CreateDatabase(ByVal connectionString As String) As Database
If Connection Is Nothing OrElse Connection.State <> ConnectionState.Open OrElse Connection.ConnectionString <> connectionString Then
'create a new SqlConnection, and assign the connection string property
Connection = New SqlConnection With
{
.ConnectionString = connectionString
}
'connect to the Sql database
Connection.Open()
End If
'return the database with an open connection
Return New Database With {.Connection = Connection, .ConnectionString = connectionString}
End Function
Friend Shared Function CreateDatabase() As Database
Return New Database
End Function
End Class
数据库类
Imports System.Data.SqlClient
Friend Class Database
Friend Property ConnectionString As String
Private ConnectionValue As SqlConnection
Friend Property Connection As SqlConnection
Get
If Me.ConnectionValue Is Nothing OrElse Me.ConnectionValue.State <> ConnectionState.Open Then
If Not String.IsNullOrWhiteSpace(Me.ConnectionString) Then
'create a new SqlConnection, and assign the connection string property
Me.ConnectionValue = New SqlConnection With
{
.ConnectionString = ConnectionString
}
'connect to the Sql database
Me.ConnectionValue.Open()
Else
Throw New Exception("A connection string must be provided.")
End If
End If
Return Me.ConnectionValue
End Get
Set(ByVal value As SqlConnection)
Me.ConnectionValue = value
End Set
End Property
Private TransactionValue As SqlTransaction
Friend Property Transaction As SqlTransaction
Get
If TransactionValue Is Nothing Then
If Me.Connection IsNot Nothing Then
Me.TransactionValue = Me.Connection.BeginTransaction
Else
Throw New Exception("No open connection exists.")
End If
End If
Return Me.TransactionValue
End Get
Set(ByVal value As SqlTransaction)
Me.TransactionValue = value
End Set
End Property
Friend Function GetStoredProcCommand(ByVal storedProcName As String) As SqlCommand
Dim cmd As New SqlCommand() With
{
.CommandText = storedProcName,
.CommandType = CommandType.StoredProcedure,
.Connection = Me.Connection
}
Return cmd
End Function
Friend Function GetSqlStringCommand(ByVal query As String) As SqlCommand
Dim cmd As New SqlCommand() With
{
.CommandText = query,
.CommandType = CommandType.Text,
.Connection = Me.Connection
}
Return cmd
End Function
Friend Sub AddInParameter(ByRef cmd As SqlCommand, ByVal paramterName As String, ByVal sqlDbType As SqlDbType, ByVal value As Object, Optional ByVal direction As ParameterDirection = ParameterDirection.Input)
Dim para As New SqlParameter With
{
.Value = value,
.ParameterName = If(Not paramterName.StartsWith("@"), String.Format("@{0}", paramterName), paramterName),
.SqlDbType = sqlDbType,
.Direction = direction
}
cmd.Parameters.Add(para)
End Sub
Friend Sub ExecuteNonQuery(ByVal cmd As SqlCommand, ByVal transaction As SqlTransaction)
cmd.Transaction = transaction
cmd.ExecuteNonQuery()
End Sub
Friend Sub ExecuteNonQuery(ByVal cmd As SqlCommand)
cmd.ExecuteNonQuery()
End Sub
Friend Function ExecuteScalar(ByVal cmd As SqlCommand, ByVal transaction As SqlTransaction) As Object
cmd.Transaction = transaction
Return cmd.ExecuteScalar()
End Function
Friend Function ExecuteScalar(ByVal cmd As SqlCommand) As Object
Return cmd.ExecuteScalar()
End Function
Friend Function ExecuteDataSet(ByVal cmd As SqlCommand, ByVal transaction As SqlTransaction) As DataSet
cmd.Transaction = transaction
Dim adapter As New SqlDataAdapter
adapter.SelectCommand = cmd
Dim dSet As New DataSet
adapter.Fill(dSet)
adapter.Dispose()
Return dSet
End Function
Friend Function ExecuteDataSet(ByVal cmd As SqlCommand) As DataSet
Dim adapter As New SqlDataAdapter
adapter.SelectCommand = cmd
Dim dSet As New DataSet
adapter.Fill(dSet)
adapter.Dispose()
Return dSet
End Function
Friend Sub Dispose()
Me.Connection.Close()
Me.Connection.Dispose()
End Sub
End Class