Database operations using DbProviderFactories in asp.net

Source: Internet
Author: User

Encapsulate database operations and provide transaction processing.
Copy codeThe Code is as follows:
Database Operation class using DbProviderFactories
Imports System. Data
Imports System. Configuration
Imports System. Data. Common

'*************************************** ****************************
'* Page/Class Name: XPDBHelper. vb
'* Title: database operation class using DbProviderFactories
'* Description: simple three-tier database operation class
'* Copyright:
'* Company:
'* @ Author: Owen, Yuan
'* Create Date: 2010/3/9 14:04:56
'* Last Modifier:
'* Last Modify Date:
'*************************************** ****************************
Public Class XPDBHelper

'Get the database driver provider in the connection string
Private Shared dbProviderName As String = ConfigurationManager. ConnectionStrings ("ConnectionString"). ProviderName

'Get the database connection string
Private Shared dbConnectionString As String = ConfigurationManager. ConnectionStrings ("ConnectionString"). ConnectionString

'Database connection.
Private connection As DbConnection

Public Sub New ()
Me. connection = CreateConnection (XPDBHelper. dbConnectionString)
End Sub

Public Sub New (ByVal connectionString As String)
Me. connection = CreateConnection (connectionString)
End Sub

'Create a database connection
Public Shared Function CreateConnection () As DbConnection
Dim dbfactory As DbProviderFactory = DbProviderFactories. GetFactory (dbProviderName)
Dim dbconn As DbConnection = dbfactory. CreateConnection ()
Dbconn. ConnectionString = XPDBHelper. dbConnectionString
Return dbconn
End Function

'Create a database connection
Public Shared Function CreateConnection (ByVal connectionString As String) As DbConnection
Dim dbfactory As DbProviderFactory = DbProviderFactories. GetFactory (dbProviderName)
Dim dbconn As DbConnection = dbfactory. CreateConnection ()
Dbconn. ConnectionString = connectionString
Return dbconn
End Function

'Create a DbCommand Based on the Stored Procedure
Public Function GetStoredProcCommond (ByVal storedProcedure As String) As DbCommand
Dim dbCommand As DbCommand = connection. CreateCommand ()
DbCommand. CommandText = storedProcedure
DbCommand. CommandType = CommandType. StoredProcedure
Return dbCommand
End Function

'Create a DbCommand Based on the SQL statement
Public Function GetSqlStringCommond (ByVal sqlQuery As String) As DbCommand
Dim dbCommand As DbCommand = connection. CreateCommand ()
DbCommand. CommandText = sqlQuery
DbCommand. CommandType = CommandType. Text
Return dbCommand
End Function

# Region "add Parameters"
Public Sub AddParameterCollection (ByVal cmd As DbCommand, ByVal dbParameterCollection As DbParameterCollection)
For Each dbParameter As DbParameter In dbParameterCollection
Cmd. Parameters. Add (dbParameter)
Next
End Sub

Public Sub AddOutParameter (ByVal cmd As DbCommand, ByVal parameterName As String, ByVal dbType As DbType, ByVal size As Integer)
Dim dbParameter As DbParameter = cmd. CreateParameter ()
DbParameter. DbType = dbType
DbParameter. ParameterName = parameterName
DbParameter. Size = size
DbParameter. Direction = ParameterDirection. Output
Cmd. Parameters. Add (dbParameter)
End Sub


Public Sub AddInParameter (ByVal cmd As DbCommand, ByVal parameterName As String, ByVal dbType As DbType, ByVal value As Object)
Dim dbParameter As DbParameter = cmd. CreateParameter ()
DbParameter. DbType = dbType
DbParameter. ParameterName = parameterName
DbParameter. Value = value
DbParameter. Direction = ParameterDirection. Input
Cmd. Parameters. Add (dbParameter)
End Sub

Public Sub AddReturnParameter (ByVal cmd As DbCommand, ByVal parameterName As String, ByVal dbType As DbType)
Dim dbParameter As DbParameter = cmd. CreateParameter ()
DbParameter. DbType = dbType
DbParameter. ParameterName = parameterName
DbParameter. Direction = ParameterDirection. ReturnValue
Cmd. Parameters. Add (dbParameter)
End Sub

Public Function GetParameter (ByVal cmd As DbCommand, ByVal parameterName As String) As DbParameter
Return cmd. Parameters (parameterName)
End Function
# End Region

# Region "execution"
'Execute DbCommand and return DataSet
Public Function ExecuteDataSet (ByVal cmd As DbCommand) As DataSet
Dim dbfactory As DbProviderFactory = DbProviderFactories. GetFactory (dbProviderName)
Dim dbDataAdapter As DbDataAdapter = dbfactory. CreateDataAdapter ()
DbDataAdapter. SelectCommand = cmd
Dim ds As DataSet = New DataSet ()
DbDataAdapter. Fill (ds)
Return ds
End Function

'Execute DbCommand and return the DataTable
Public Function ExecuteDataTable (ByVal cmd As DbCommand) As DataTable
Dim dbfactory As DbProviderFactory = DbProviderFactories. GetFactory (dbProviderName)
Dim dbDataAdapter As DbDataAdapter = dbfactory. CreateDataAdapter ()
DbDataAdapter. SelectCommand = cmd
Dim dataTable As DataTable = New DataTable ()
DbDataAdapter. Fill (dataTable)
Return dataTable
End Function

'Execute DbCommand and return DbDataReader
Public Function ExecuteReader (ByVal cmd As DbCommand) As DbDataReader
Cmd. Connection. Open ()
Dim reader As DbDataReader = cmd. ExecuteReader (CommandBehavior. CloseConnection)
Cmd. Connection. Close ()
Return reader
End Function

Public Function ExecuteNonQuery (ByVal cmd As DbCommand) As Integer
Cmd. Connection. Open ()
Dim ret As Integer = cmd. ExecuteNonQuery ()
Cmd. Connection. Close ()
Return ret
End Function

Public Function ExecuteScalar (ByVal cmd As DbCommand) As Object
Cmd. Connection. Open ()
Dim ret As Object = cmd. ExecuteScalar ()
Cmd. Connection. Close ()
Return ret
End Function
# End Region

# Region "executing transactions"

Public Function ExecuteDataSet (ByVal cmd As DbCommand, ByVal t As XPTransaction) As DataSet
Cmd. Connection = t. DbConnection
Cmd. Transaction = t. DbTransact
Dim dbfactory As DbProviderFactory = DbProviderFactories. GetFactory (XPDBHelper. dbProviderName)
Dim dbDataAdapter As DbDataAdapter = dbfactory. CreateDataAdapter ()
DbDataAdapter. SelectCommand = cmd
Dim ds As DataSet = New DataSet ()
DbDataAdapter. Fill (ds)
Return ds
End Function

Public Function ExecuteDataTable (ByVal cmd As DbCommand, ByVal t As XPTransaction) As DataTable
Cmd. Connection = t. DbConnection
Cmd. Transaction = t. DbTransact
Dim dbfactory As DbProviderFactory = DbProviderFactories. GetFactory (XPDBHelper. dbProviderName)
Dim dbDataAdapter As DbDataAdapter = dbfactory. CreateDataAdapter ()
DbDataAdapter. SelectCommand = cmd
Dim dataTable As DataTable = New DataTable ()
DbDataAdapter. Fill (dataTable)
Return dataTable
End Function

Public Function ExecuteReader (ByVal cmd As DbCommand, ByVal t As XPTransaction) As DbDataReader
Cmd. Connection. Close ()
Cmd. Connection = t. DbConnection
Cmd. Transaction = t. DbTransact
Dim reader As DbDataReader = cmd. ExecuteReader ()
Return reader
End Function

Public Function ExecuteNonQuery (ByVal cmd As DbCommand, ByVal t As XPTransaction) As Integer
Cmd. Connection. Close ()
Cmd. Connection = t. DbConnection
Cmd. Transaction = t. DbTransact
Return cmd. ExecuteNonQuery ()
End Function

Public Function ExecuteScalar (ByVal cmd As DbCommand, ByVal t As XPTransaction) As Object
Cmd. Connection. Close ()
Cmd. Connection = t. DbConnection
Cmd. Transaction = t. DbTransact
Return cmd. ExecuteScalar ()
End Function
# End Region

End Class



Simple three-tier structure database transaction processing class
Copy codeThe Code is as follows:
Imports System. Data
Imports System. Data. Common

'*************************************** ****************************
'* Page/Class Name: XPTransaction. vb
'* Title: Database Transaction processing class
'* Description: simple three-tier Database Transaction processing class
'* Copyright:
'* Company:
'* @ Author: Owen, Yuan
'* Create Date: 2010/3/22 10:44:20
'* Last Modifier:
'* Last Modify Date:
'*************************************** ****************************
Public Class XPTransaction
Implements IDisposable

Private conn As DbConnection
Private dbTrans As DbTransaction

Public Property DbConnection () As DbConnection
Get
Return Me. conn
End Get
Set (ByVal Value As DbConnection)
Me. conn = Value
End Set
End Property

Public Property DbTransact () As DbTransaction
Get
Return Me. dbTrans
End Get
Set (ByVal Value As DbTransaction)
Me. dbTrans = Value
End Set
End Property

Public Sub New ()
Conn = XPDBHelper. CreateConnection ()
Conn. Open ()
DbTrans = conn. BeginTransaction ()
End Sub

Public Sub New (ByVal connectionString As String)
Conn = XPDBHelper. CreateConnection (connectionString)
Conn. Open ()
DbTrans = conn. BeginTransaction ()
End Sub

Public Sub Commit ()
DbTrans. Commit ()
Me. Colse ()
End Sub

Public Sub RollBack ()
DbTrans. Rollback ()
Me. Colse ()
End Sub

Public Sub Colse ()
If conn. State = ConnectionState. Open Then
Conn. Close ()
End If
End Sub

Private disposedValue As Boolean = false' checks redundant calls

'Idisposable
Protected Overridable Sub Dispose (ByVal disposing As Boolean)
If Not Me. disposedValue Then
If disposing Then
Conn. Close ()
'Todo: release other statuses (managed objects ).
End If
Conn = Nothing
DbTrans = Nothing
'Todo: release your own status (unmanaged object ).
'Todo: Set large fields to null.
End If
Me. disposedValue = True
End Sub

# Region "IDisposable Support"
'Visual Basic adds this code to correctly implement the disposal mode.
Public Sub Dispose () Implements IDisposable. Dispose
'Do not change this code. Put the cleanup code in the above Dispose (ByVal disposing As Boolean.
Dispose (True)
GC. SuppressFinalize (Me)
End Sub
# End Region

End Class

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.