VB.NET学习笔记:ADO.NET操作ACCESS数据库——读写ACCESS数据库的OleDbHelper帮助类

本文代码由微软官方原版SqlHelper类改编而来,如何获取微软官方原版SqlHelper类请阅读《ADO.NET操作ACCESS数据库——微软官方原版SqlHelper类

Imports System.Data
Imports System.Data.OleDb

Public NotInheritable Class OleDbHelper
   #Region "私有构造函数和方法"
    ''' <summary>
    ''' 由于此类只提供静态方法,因此将默认构造函数设为私有以防止使用“new OleDbHelper()”创建的实例'。
    ''' </summary>
    Private Sub New()
    End Sub ' New

    ''' <summary>
    '''此方法用于将OleDbparameters参数数组(参数值)分配给OleDbcommand命令。
    '''这个方法将给任何一个参数分配DBNull.Value
    '''将阻止使用默认值
    ''' </summary>
    ''' <param name="command">要分配OleDbparameters参数的OleDbcommand命令</param>
    ''' <param name="commandParameters">OleDbparameters参数数组</param>
    Private Shared Sub AttachParameters(ByVal command As OleDbCommand, ByVal commandParameters() As OleDbParameter)
        If (command Is Nothing) Then Throw New ArgumentNullException("command")
        If (Not commandParameters Is Nothing) Then
            Dim p As OleDbParameter
            For Each p In commandParameters
                If (Not p Is Nothing) Then
                    '检查未分配值的派生输出值
                    If (p.Direction = ParameterDirection.InputOutput OrElse p.Direction = ParameterDirection.Input) AndAlso p.Value Is Nothing Then
                        p.Value = DBNull.Value
                    End If
                    command.Parameters.Add(p)
                End If
            Next p
        End If
    End Sub ' AttachParameters

    ''' <summary>
    ''' 此方法打开(如果需要)并分配连接、事务、命令类型和参数到提供的命令。
    ''' </summary>
    ''' <param name="command">要准备的OleDbCommand命令</param>
    ''' <param name="connection">一个有效的数据库连接,用于执行此命令</param>
    ''' <param name="transaction">一个有效的事务或者是null值</param>
    ''' <param name="commandType">命令类型 (存储过程,命令文本等)</param>
    ''' <param name="commandText">存储过程名或SQL命令文本</param>
    ''' <param name="commandParameters">与该命令关联的OLEDBParameters数组,如果不需要参数,则为“null”</param>
    ''' <param name="mustCloseConnection">如果连接是打开的,则为true,其它情况下为false</param>
    Private Shared Sub PrepareCommand(ByVal command As OleDbCommand,
                                      ByVal connection As OleDbConnection,
                                      ByVal transaction As OleDbTransaction,
                                      ByVal commandType As CommandType,
                                      ByVal commandText As String,
                                      ByVal commandParameters() As OleDbParameter, ByRef mustCloseConnection As Boolean)

        If (command Is Nothing) Then Throw New ArgumentNullException("command")
        If (commandText Is Nothing OrElse commandText.Length = 0) Then Throw New ArgumentNullException("commandText")

        '如果提供的连接未打开,我们将打开它
        If connection.State <> ConnectionState.Open Then
            If connection.State = ConnectionState.Broken Then
                connection.Close()
                connection.Open()
            Else
                connection.Open()
            End If

            mustCloseConnection = True
        Else
            mustCloseConnection = False
        End If

        '将连接与命令关联
        command.Connection = connection

        '设置命令文本(存储过程名称或SQL语句)
        command.CommandText = commandText

        '分配事务
        If Not (transaction Is Nothing) Then
            If transaction.Connection Is Nothing Then Throw New ArgumentException("The transaction was rollbacked or commited, please provide an open transaction.", "transaction")
            command.Transaction = transaction
        End If

        ' 设置命令类型.
        command.CommandType = commandType

        ' 分配命令参数
        If Not (commandParameters Is Nothing) Then
            AttachParameters(command, commandParameters)
        End If
        Return
    End Sub ' PrepareCommand
#End Region

#Region "ExecuteNonQuery"

    ''' <summary>
    ''' 对指定连接字符串的数据库执行OLEDBCommand命令(不返回任何结果集,不接受任何参数)。
    ''' 示例:
    ''' dim result as integer=executeNonQuery(connstring,commandType.storedProcedure,“publishOrders”)。
    ''' </summary>
    ''' <param name="connectionString">一个有效的数据库连接字符串</param>
    ''' <param name="commandType">命令类型 (存储过程,命令文本等)</param>
    ''' <param name="commandText">存储过程名称或SQL语句</param>
    ''' <returns>返回命令影响的行数</returns>
    Public Overloads Shared Function ExecuteNonQuery(ByVal connectionString As String,
                                                   ByVal commandType As CommandType,
                                                   ByVal commandText As String) As Integer
        'OledBParameters参数数组为Nothing
        Return ExecuteNonQuery(connectionString, commandType, commandText, CType(Nothing, OleDbParameter()))
    End Function ' ExecuteNonQuery

    ''' <summary>
    ''' 使用提供的参数对指定连接字符串的数据库执行OLEDBCommand命令(不返回任何结果集)
    ''' 示例:
    ''' Dim result As Integer = ExecuteNonQuery(connString, CommandType.StoredProcedure, "PublishOrders", new OleDbParameter("@prodid", 24))
    ''' </summary>
    ''' <param name="connectionString">一个有效的数据库连接字符串</param>
    ''' <param name="commandType">命令类型 (存储过程,命令文本等)</param>
    ''' <param name="commandText">存储过程名称或SQL语句</param>
    ''' <param name="commandParameters">用于执行命令的OleDbParameter参数数组</param>
    ''' <returns>返回命令影响的行数</returns>
    Public Overloads Shared Function ExecuteNonQuery(ByVal connectionString As String,
                                                     ByVal commandType As CommandType,
                                                     ByVal commandText As String,
                                                     ByVal ParamArray commandParameters() As OleDbParameter) As Integer
        If (connectionString Is Nothing OrElse connectionString.Length = 0) Then Throw New ArgumentNullException("connectionString")
        '创建并打开一个OLEDB连接,完成后将其丢弃
        Dim connection As OleDbConnection
        Try
            connection = New OleDbConnection(connectionString)
            connection.Open()

            '调用将连接替换为连接字符串的重载
            Return ExecuteNonQuery(connection, commandType, commandText, commandParameters)
        Finally
            If Not connection Is Nothing Then connection.Dispose()
        End Try
    End Function ' ExecuteNonQuery

    ''' <summary>
    ''' 对提供的数据库连接对象执行OleDbCommand命令(不返回结果集,不接受参数)。
    ''' 示例:
    ''' Dim result As Integer = ExecuteNonQuery(conn, CommandType.StoredProcedure, "PublishOrders")
    ''' </summary>
    ''' <param name="connection">一个有效的数据库连接对象</param>
    ''' <param name="commandType">命令类型(存储过程,命令文本等)</param>
    ''' <param name="commandText">存储过程名称或SQL语句</param>
    ''' <returns>返回影响的行数</returns>
    Public Overloads Shared Function ExecuteNonQuery(ByVal connection As OleDbConnection,
                                                     ByVal commandType As CommandType,
                                                     ByVal commandText As String) As Integer
        'OledBParameters参数数组为Nothing
        Return ExecuteNonQuery(connection, commandType, commandText, CType(Nothing, OleDbParameter()))

    End Function ' ExecuteNonQuery

    ''' <summary>
    ''' 使用提供的参数对指定的数据库连接对象执行OLEDBCommand(不返回任何结果集)
    ''' 示例:
    ''' Dim result As Integer = ExecuteNonQuery(conn, CommandType.StoredProcedure, "PublishOrders", new OleDbParameter("@prodid", 24))
    ''' </summary>
    ''' <param name="connection">一个有效的数据库连接对象</param>
    ''' <param name="commandType">命令类型(存储过程,命令文本等)</param>
    ''' <param name="commandText">存储过程名称或SQL语句</param>
    ''' <param name="commandParameters">用于执行命令的OleDbParameter参数数组</param>
    ''' <returns>返回影响的行数</returns>
    Public Overloads Shared Function ExecuteNonQuery(ByVal connection As OleDbConnection,
                                                     ByVal commandType As CommandType,
                                                     ByVal commandText As String,
                                                     ByVal ParamArray commandParameters() As OleDbParameter) As Integer

        If (connection Is Nothing) Then Throw New ArgumentNullException("connection")

        '创建命令并准备执行它
        Dim cmd As New OleDbCommand
        Dim retval As Integer
        Dim mustCloseConnection As Boolean = False

        PrepareCommand(cmd, connection, CType(Nothing, OleDbTransaction), commandType, commandText, commandParameters, mustCloseConnection)

        '最后,执行命令
        retval = cmd.ExecuteNonQuery()

        '清空参数
        cmd.Parameters.Clear()

        If (mustCloseConnection) Then connection.Close()

        Return retval
    End Function ' ExecuteNonQuery

    ''' <summary>
    ''' 对提供的OleDbTransaction(事务)执行OleDbCommand命令(不返回任何结果集且不接受任何参数)。
    ''' 示例:
    ''' Dim result As Integer = ExecuteNonQuery(trans, CommandType.StoredProcedure, "PublishOrders")
    ''' </summary>
    ''' <param name="transaction">与连接关联的有效OleDbTransaction(事务)</param>
    ''' <param name="commandType">命令类型(存储过程,命令文本等)</param>
    ''' <param name="commandText">存储过程名称或SQL语句</param>
    ''' <returns>返回影响的行数</returns>
    Public Overloads Shared Function ExecuteNonQuery(ByVal transaction As OleDbTransaction,
                                                     ByVal commandType As CommandType,
                                                     ByVal commandText As String) As Integer
        'OledBParameters参数数组为Nothing
        Return ExecuteNonQuery(transaction, commandType, commandText, CType(Nothing, OleDbParameter()))
    End Function ' ExecuteNonQuery

    ''' <summary>
    ''' 使用提供的参数对指定的OleDbTransaction(事务)执行OleDbCommand命令(不返回任何结果集)
    ''' 示例:
    ''' Dim result As Integer = ExecuteNonQuery(trans, CommandType.StoredProcedure, "GetOrders", new OleDbParameter("@prodid", 24))
    ''' </summary>
    ''' <param name="transaction">与连接关联的有效OleDbTransaction(事务)</param>
    ''' <param name="commandType">命令类型(存储过程,命令文本等)</param>
    ''' <param name="commandText">存储过程名称或SQL语句</param>
    ''' <param name="commandParameters">用于执行命令的OleDbParameter参数数组</param>
    ''' <returns>返回影响的行数</returns>
    Public Overloads Shared Function ExecuteNonQuery(ByVal transaction As OleDbTransaction,
                                                     ByVal commandType As CommandType,
                                                     ByVal commandText As String,
                                                     ByVal ParamArray commandParameters() As OleDbParameter) As Integer

        If (transaction Is Nothing) Then Throw New ArgumentNullException("transaction")
        If Not (transaction Is Nothing) AndAlso (transaction.Connection Is Nothing) Then Throw New ArgumentException("The transaction was rollbacked or commited, please provide an open transaction.", "transaction")

        '创建命令并准备执行它
        Dim cmd As New OleDbCommand
        Dim retval As Integer
        Dim mustCloseConnection As Boolean = False

        PrepareCommand(cmd, transaction.Connection, transaction, commandType, commandText, commandParameters, mustCloseConnection)

        '最后,执行命令
        retval = cmd.ExecuteNonQuery()

        '清空命令里的参数
        cmd.Parameters.Clear()

        Return retval
    End Function ' ExecuteNonQuery
#End Region

#Region "ExecuteDataset"

    ''' <summary>
    ''' 使用指定的连接字符串对数据库执行OLEDBCommand命令(返回结果集且不接受参数)
    ''' 示例:
    ''' Dim ds As DataSet = OleDbHelper.ExecuteDataset("", commandType.StoredProcedure, "GetOrders")
    ''' </summary>
    ''' <param name="connectionString">一个有效的数据库连接字符串</param>
    ''' <param name="commandType">命令类型(存储过程,命令文本等)</param>
    ''' <param name="commandText">存储过程名称或SQL语句</param>
    ''' <returns>返回一个包含结果集的DataSet</returns>
    Public Overloads Shared Function ExecuteDataset(ByVal connectionString As String,
                                                    ByVal commandType As CommandType,
                                                    ByVal commandText As String) As DataSet
        '不提供参数即Nothing
        Return ExecuteDataset(connectionString, commandType, commandText, CType(Nothing, OleDbParameter()))
    End Function ' ExecuteDataset

    ''' <summary>
    ''' 使用提供的参数对指定连接字符串的数据库执行OLEDBCommand命令(返回结果集)
    ''' 示例:
    ''' Dim ds As Dataset = ExecuteDataset(connString, CommandType.StoredProcedure, "GetOrders", new OleDbParameter("@prodid", 24))
    ''' </summary>
    ''' <param name="connectionString">一个有效的数据库连接字符串</param>
    ''' <param name="commandType">命令类型(存储过程,命令文本等)</param>
    ''' <param name="commandText">存储过程名称或SQL语句</param>
    ''' <param name="commandParameters">用于执行命令的OleDbParameter参数数组</param>
    ''' <returns>返回一个包含结果集的DataSet</returns>
    Public Overloads Shared Function ExecuteDataset(ByVal connectionString As String,
                                                    ByVal commandType As CommandType,
                                                    ByVal commandText As String,
                                                    ByVal ParamArray commandParameters() As OleDbParameter) As DataSet

        If (connectionString Is Nothing OrElse connectionString.Length = 0) Then Throw New ArgumentNullException("connectionString")

        '创建并打开一个OLEDB连接,完成后将其销毁
        Dim connection As OleDbConnection
        Try
            connection = New OleDbConnection(connectionString)
            connection.Open()

            '调用将连接替换为连接字符串的重载
            Return ExecuteDataset(connection, commandType, commandText, commandParameters)
        Finally
            If Not connection Is Nothing Then connection.Dispose()
        End Try
    End Function ' ExecuteDataset

    ''' <summary>
    ''' 对提供的数据库连接对象执行OleDBCommand命令(返回结果集且不接受参数)
    ''' 示例:
    ''' Dim ds As Dataset = ExecuteDataset(conn, CommandType.StoredProcedure, "GetOrders")
    ''' </summary>
    ''' <param name="connection">一个有效的数据库连接对象</param>
    ''' <param name="commandType">命令类型(存储过程,命令文本等)</param>
    ''' <param name="commandText">存储过程名称或SQL语句</param>
    ''' <returns>返回一个包含结果集的DataSet</returns>
    Public Overloads Shared Function ExecuteDataset(ByVal connection As OleDbConnection,
                                                    ByVal commandType As CommandType,
                                                    ByVal commandText As String) As DataSet

        Return ExecuteDataset(connection, commandType, commandText, CType(Nothing, OleDbParameter()))
    End Function ' ExecuteDataset

    ''' <summary>
    '''  使用提供的参数对指定的数据库连接对象执行OLEDBCommand(返回结果集)
    '''  示例:
    '''  Dim ds As Dataset = ExecuteDataset(conn, CommandType.StoredProcedure, "GetOrders", new OleDbParameter("@prodid", 24))
    ''' </summary>
    ''' <param name="connection">一个有效的数据库连接对象</param>
    ''' <param name="commandType">命令类型(存储过程,命令文本等)</param>
    ''' <param name="commandText">存储过程名称或SQL语句</param>
    ''' <param name="commandParameters">用于执行命令的OleDbParameter参数数组</param>
    ''' <returns>返回一个包含结果集的DataSet</returns>
    Public Overloads Shared Function ExecuteDataset(ByVal connection As OleDbConnection,
                                                    ByVal commandType As CommandType,
                                                    ByVal commandText As String,
                                                    ByVal ParamArray commandParameters() As OleDbParameter) As DataSet
        If (connection Is Nothing) Then Throw New ArgumentNullException("connection")
        '创建命令并准备执行它
        Dim cmd As New OleDbCommand
        Dim ds As New DataSet
        Dim dataAdatpter As OleDbDataAdapter
        Dim mustCloseConnection As Boolean = False

        PrepareCommand(cmd, connection, CType(Nothing, OleDbTransaction), commandType, commandText, commandParameters, mustCloseConnection)

        Try
            '创建数据适配器和数据集
            dataAdatpter = New OleDbDataAdapter(cmd)

            '使用数据表名称等的默认值填充数据集
            dataAdatpter.Fill(ds)

            '清空命令里的参数
            cmd.Parameters.Clear()
        Finally
            If (Not dataAdatpter Is Nothing) Then dataAdatpter.Dispose()
        End Try
        If (mustCloseConnection) Then connection.Close()

        '返回数据集
        Return ds
    End Function ' ExecuteDataset

    ''' <summary>
    '''  对提供的OleDbTransaction(事务)执行OleDbCommand命令(返回结果集且不带参数)
    '''  示例:
    '''  Dim ds As Dataset = ExecuteDataset(trans, CommandType.StoredProcedure, "GetOrders")
    ''' </summary>
    ''' <param name="transaction">与连接关联的有效OleDbTransaction(事务)</param>
    ''' <param name="commandType">命令类型(存储过程,命令文本等)</param>
    ''' <param name="commandText">存储过程名称或SQL语句</param>
    ''' <returns>返回一个包含结果集的DataSet</returns>
    Public Overloads Shared Function ExecuteDataset(ByVal transaction As OleDbTransaction,
                                                    ByVal commandType As CommandType,
                                                    ByVal commandText As String) As DataSet
        Return ExecuteDataset(transaction, commandType, commandText, CType(Nothing, OleDbParameter()))
    End Function ' ExecuteDataset

    ''' <summary>
    ''' 使用提供的参数对指定的OleDbTransaction(事务)执行OleDbCommand命令(返回结果集)
    ''' 示例:
    ''' Dim ds As Dataset = ExecuteDataset(trans, CommandType.StoredProcedure, "GetOrders", new OleDbParameter("@prodid", 24))
    ''' </summary>
    ''' <param name="transaction">与连接关联的有效OleDbTransaction(事务)</param>
    ''' <param name="commandType">命令类型(存储过程,命令文本等)</param>
    ''' <param name="commandText">存储过程名称或SQL语句</param>
    ''' <param name="commandParameters">用于执行命令的OleDbParameter参数数组</param>
    ''' <returns>返回一个包含结果集的DataSet</returns>
    Public Overloads Shared Function ExecuteDataset(ByVal transaction As OleDbTransaction,
                                                    ByVal commandType As CommandType,
                                                    ByVal commandText As String,
                                                    ByVal ParamArray commandParameters() As OleDbParameter) As DataSet
        If (transaction Is Nothing) Then Throw New ArgumentNullException("transaction")
        If Not (transaction Is Nothing) AndAlso (transaction.Connection Is Nothing) Then Throw New ArgumentException("The transaction was rollbacked or commited, please provide an open transaction.", "transaction")

        '创建命令并准备执行它
        Dim cmd As New OleDbCommand
        Dim ds As New DataSet
        Dim dataAdatpter As OleDbDataAdapter
        Dim mustCloseConnection As Boolean = False

        PrepareCommand(cmd, transaction.Connection, transaction, commandType, commandText, commandParameters, mustCloseConnection)

        Try
            '创建数据适配器和数据集
            dataAdatpter = New OleDbDataAdapter(cmd)

            '使用数据表名称等的默认值填充数据集
            dataAdatpter.Fill(ds)

            '清空命令里的参数
            cmd.Parameters.Clear()
        Finally
            If (Not dataAdatpter Is Nothing) Then dataAdatpter.Dispose()
        End Try

        '返回数据集
        Return ds

    End Function ' ExecuteDataset
#End Region

#Region "ExecuteReader"
    ''' <summary>
    ''' 枚举,标识数据库连接是由OledbHelper创建还是由调用方提供
    ''' 以便我们可以在调用ExecuteReader()时设置适当的CommandBehavior
    ''' </summary>
    Private Enum OleDbConnectionOwnership
        '连接由oledbhelper拥有和管理
        Internal
        '连接由调用方拥有和管理
        [External]
    End Enum ' OleDbConnectionOwnership

    ''' <summary>
    ''' 创建并准备一个OleDbCommand,并使用适当的commandBehavior调用ExecuteReader。
    ''' 如果oledbhelper创建并打开连接,我们希望在关闭DataReader时关闭连接。
    ''' '如果调用方提供了连接,我们希望将其留给他们来管理。
    ''' </summary>
    ''' <param name="connection">一个有效的数据库连接对象</param>
    ''' <param name="transaction">一个有效的事务,或者为 null</param>
    ''' <param name="commandType">命令类型 (存储过程,命令文本等)</param>
    ''' <param name="commandText">存储过程名或SQL语句</param>
    ''' <param name="commandParameters">OleDbParameters参数数组,如果没有参数则为'null'</param>
    ''' <param name="connectionOwnership">标识数据库连接对象是由调用者提供还是由OleDbHelper提供</param>
    ''' <returns>返回包含结果集的OleDbDataReader</returns>
    Private Overloads Shared Function ExecuteReader(ByVal connection As OleDbConnection,
                                                    ByVal transaction As OleDbTransaction,
                                                    ByVal commandType As CommandType,
                                                    ByVal commandText As String,
                                                    ByVal commandParameters() As OleDbParameter,
                                                    ByVal connectionOwnership As OleDbConnectionOwnership) As OleDbDataReader

        If (connection Is Nothing) Then Throw New ArgumentNullException("connection")

        Dim mustCloseConnection As Boolean = False
        '创建命令并准备执行它
        Dim cmd As New OleDbCommand
        Try
            '创建读卡器
            Dim dataReader As OleDbDataReader

            PrepareCommand(cmd, connection, transaction, commandType, commandText, commandParameters, mustCloseConnection)

            '使用适当的commandBehavior调用ExecuteReader
            If connectionOwnership = OleDbConnectionOwnership.External Then
                dataReader = cmd.ExecuteReader()
            Else
                dataReader = cmd.ExecuteReader(CommandBehavior.CloseConnection)
            End If

            '清空命令里的参数
            Dim canClear As Boolean = True
            Dim commandParameter As OleDbParameter
            For Each commandParameter In cmd.Parameters
                If commandParameter.Direction <> ParameterDirection.Input Then
                    canClear = False
                End If
            Next

            If (canClear) Then cmd.Parameters.Clear()

            Return dataReader
        Catch
            If (mustCloseConnection) Then connection.Close()
            Throw
        End Try
    End Function ' ExecuteReader

    ''' <summary>
    ''' 使用指定的连接字符串对数据库执行OLEDBCommand命令(返回结果集且不接受参数)
    ''' 示例:
    ''' Dim dr As OleDbDataReader = ExecuteReader(connString, CommandType.StoredProcedure, "GetOrders")
    ''' </summary>
    ''' <param name="connectionString">一个有效的数据库连接字符串</param>
    ''' <param name="commandType">命令类型 (存储过程,命令文本等)</param>
    ''' <param name="commandText">存储过程名或SQL语句</param>
    ''' <returns>返回包含结果集的OleDbDataReader</returns>
    Public Overloads Shared Function ExecuteReader(ByVal connectionString As String,
                                                   ByVal commandType As CommandType,
                                                   ByVal commandText As String) As OleDbDataReader
        Return ExecuteReader(connectionString, commandType, commandText, CType(Nothing, OleDbParameter()))
    End Function ' ExecuteReader

    ''' <summary>
    ''' 使用提供的参数对指定连接字符串的数据库执行OLEDBCommand命令(返回结果集)
    ''' 示例:
    ''' Dim dr As OleDbDataReader = ExecuteReader(connString, CommandType.StoredProcedure, "GetOrders", new OleDbParameter("@prodid", 24))
    ''' </summary>
    ''' <param name="connectionString">一个有效的数据库连接字符串</param>
    ''' <param name="commandType">命令类型 (存储过程,命令文本等)</param>
    ''' <param name="commandText">存储过程名或SQL语句</param>
    ''' <param name="commandParameters">用于执行命令的OleDbParameters参数数组</param>
    ''' <returns>返回包含结果集的OleDbDataReader</returns>
    Public Overloads Shared Function ExecuteReader(ByVal connectionString As String,
                                                   ByVal commandType As CommandType,
                                                   ByVal commandText As String,
                                                   ByVal ParamArray commandParameters() As OleDbParameter) As OleDbDataReader
        If (connectionString Is Nothing OrElse connectionString.Length = 0) Then Throw New ArgumentNullException("connectionString")

        '创建并打开连接
        Dim connection As OleDbConnection
        Try
            connection = New OleDbConnection(connectionString)
            connection.Open()
            '调用将内部拥有的连接替换为连接字符串的私有重载
            Return ExecuteReader(connection, CType(Nothing, OleDbTransaction), commandType, commandText, commandParameters, OleDbConnectionOwnership.Internal)
        Catch
            '如果我们无法返回OledbDataReader,我们需要自己关闭连接
            If Not connection Is Nothing Then connection.Dispose()
            Throw
        End Try
    End Function ' ExecuteReader

    ''' <summary>
    ''' 对提供的数据库连接对象执行OleDBCommand命令(返回结果集且不接受参数)
    ''' 示例:
    '''  Dim dr As OleDbDataReader = ExecuteReader(conn, CommandType.StoredProcedure, "GetOrders")
    ''' </summary>
    ''' <param name="connection">一个有效的数据库连接对象</param>
    ''' <param name="commandType">命令类型 (存储过程,命令文本等)</param>
    ''' <param name="commandText">存储过程名或SQL语句</param>
    ''' <returns>返回包含结果集的OleDbDataReader</returns>
    Public Overloads Shared Function ExecuteReader(ByVal connection As OleDbConnection,
                                                   ByVal commandType As CommandType,
                                                   ByVal commandText As String) As OleDbDataReader

        Return ExecuteReader(connection, commandType, commandText, CType(Nothing, OleDbParameter()))

    End Function ' ExecuteReader

    ''' <summary>
    ''' 使用提供的参数对指定的数据库连接对象执行OleDBCommand命令(返回结果集)
    ''' 示例:
    ''' Dim dr As OleDbDataReader = ExecuteReader(conn, CommandType.StoredProcedure, "GetOrders", new OleDbParameter("@prodid", 24))
    ''' </summary>
    ''' <param name="connection">一个有效的数据库连接对象</param>
    ''' <param name="commandType">命令类型 (存储过程,命令文本等)</param>
    ''' <param name="commandText">存储过程名或SQL语句</param>
    ''' <param name="commandParameters">用于执行命令的OleDbParameters参数数组</param>
    ''' <returns>返回包含结果集的OleDbDataReader</returns>
    Public Overloads Shared Function ExecuteReader(ByVal connection As OleDbConnection,
                                                   ByVal commandType As CommandType,
                                                   ByVal commandText As String,
                                                   ByVal ParamArray commandParameters() As OleDbParameter) As OleDbDataReader
        '使用空事务值传递对私有重载的调用
        Return ExecuteReader(connection, CType(Nothing, OleDbTransaction), commandType, commandText, commandParameters, OleDbConnectionOwnership.External)

    End Function ' ExecuteReader

    ''' <summary>
    ''' 对提供的OleDbTransaction(事务)执行OleDbCommand命令(返回结果集且不带参数)
    ''' 示例:
    ''' Dim dr As OleDbDataReader = ExecuteReader(trans, CommandType.StoredProcedure, "GetOrders")
    ''' </summary>
    ''' <param name="transaction">与连接关联的有效OleDbTransaction(事务)</param>
    ''' <param name="commandType">命令类型 (存储过程,命令文本等)</param>
    ''' <param name="commandText">存储过程名或SQL语句</param>
    ''' <returns>返回包含结果集的OleDbDataReader</returns>
    Public Overloads Shared Function ExecuteReader(ByVal transaction As OleDbTransaction,
                                                   ByVal commandType As CommandType,
                                                   ByVal commandText As String) As OleDbDataReader
        Return ExecuteReader(transaction, commandType, commandText, CType(Nothing, OleDbParameter()))
    End Function ' ExecuteReader

    ''' <summary>
    ''' 使用提供的参数对指定的OleDbTransaction(事务)执行OleDbCommand命令(返回结果集)
    ''' 示例:
    ''' Dim dr As OleDbDataReader = ExecuteReader(trans, CommandType.StoredProcedure, "GetOrders", new OleDbParameter("@prodid", 24))
    ''' </summary>
    ''' <param name="transaction">与连接关联的有效OleDbTransaction(事务)</param>
    ''' <param name="commandType">命令类型 (存储过程,命令文本等)</param>
    ''' <param name="commandText">存储过程名或SQL语句</param>
    ''' <param name="commandParameters">用于执行命令的OleDbParameters参数数组</param>
    ''' <returns>返回包含结果集的OleDbDataReader</returns>
    Public Overloads Shared Function ExecuteReader(ByVal transaction As OleDbTransaction,
                                                   ByVal commandType As CommandType,
                                                   ByVal commandText As String,
                                                   ByVal ParamArray commandParameters() As OleDbParameter) As OleDbDataReader
        If (transaction Is Nothing) Then Throw New ArgumentNullException("transaction")
        If Not (transaction Is Nothing) AndAlso (transaction.Connection Is Nothing) Then Throw New ArgumentException("The transaction was rollbacked or commited, please provide an open transaction.", "transaction")
        '传递到私有重载,指示连接归调用方所有
        Return ExecuteReader(transaction.Connection, transaction, commandType, commandText, commandParameters, OleDbConnectionOwnership.External)
    End Function ' ExecuteReader
#End Region

#Region "ExecuteScalar"

    ''' <summary>
    ''' 使用指定的连接字符串对数据库执行OLEDBCommand命令(返回1x1结果集且不接受参数)
    ''' 示例:
    ''' Dim orderCount As Integer = CInt(ExecuteScalar(connString, CommandType.StoredProcedure, "GetOrderCount"))
    ''' </summary>
    ''' <param name="connectionString">一个有效的数据库连接字符串</param>
    ''' <param name="commandType">命令类型 (存储过程,命令文本等)</param>
    ''' <param name="commandText">存储过程名或SQL语句</param>
    ''' <returns>包含命令生成的1x1(第一行第一列)结果集中的值的对象</returns>
    Public Overloads Shared Function ExecuteScalar(ByVal connectionString As String,
                                                   ByVal commandType As CommandType,
                                                   ByVal commandText As String) As Object
        '执行参数为空的方法
        Return ExecuteScalar(connectionString, commandType, commandText, CType(Nothing, OleDbParameter()))
    End Function ' ExecuteScalar

    ''' <summary>
    ''' 使用提供的参数对指定连接字符串的数据库执行OLEDBCommand命令(返回1x1结果集)
    ''' 示例:
    ''' Dim orderCount As Integer = Cint(ExecuteScalar(connString, CommandType.StoredProcedure, "GetOrderCount", new OleDbParameter("@prodid", 24)))
    ''' </summary>
    ''' <param name="connectionString">一个有效的数据库连接字符串</param>
    ''' <param name="commandType">命令类型 (存储过程,命令文本等)</param>
    ''' <param name="commandText">存储过程名或SQL语句</param>
    ''' <param name="commandParameters">用于执行命令的OleDbParameters参数数组</param>
    ''' <returns>包含命令生成的1x1(第一行第一列)结果集中的值的对象</returns>
    Public Overloads Shared Function ExecuteScalar(ByVal connectionString As String,
                                                   ByVal commandType As CommandType,
                                                   ByVal commandText As String,
                                                   ByVal ParamArray commandParameters() As OleDbParameter) As Object
        If (connectionString Is Nothing OrElse connectionString.Length = 0) Then Throw New ArgumentNullException("connectionString")
        '创建并打开一个数据库连接,完成后将其丢弃。
        Dim connection As OleDbConnection
        Try
            connection = New OleDbConnection(connectionString)
            connection.Open()

            '调用将连接替换为连接字符串的重载
            Return ExecuteScalar(connection, commandType, commandText, commandParameters)
        Finally
            If Not connection Is Nothing Then connection.Dispose()
        End Try
    End Function ' ExecuteScalar

    ''' <summary>
    ''' 对提供的数据库连接对象执行命令(返回1x1结果集,不接受任何参数)。
    ''' 示例:
    ''' Dim orderCount As Integer = CInt(ExecuteScalar(conn, CommandType.StoredProcedure, "GetOrderCount"))
    ''' </summary>
    ''' <param name="connection">一个有效的数据库连接对象</param>
    ''' <param name="commandType">命令类型 (存储过程,命令文本等)</param>
    ''' <param name="commandText">存储过程名或SQL语句</param>
    ''' <returns>包含命令生成的1x1(第一行第一列)结果集中的值的对象</returns>
    Public Overloads Shared Function ExecuteScalar(ByVal connection As OleDbConnection,
                                                   ByVal commandType As CommandType,
                                                   ByVal commandText As String) As Object
        '执行参数为空的方法
        Return ExecuteScalar(connection, commandType, commandText, CType(Nothing, OleDbParameter()))
    End Function ' ExecuteScalar

    ''' <summary>
    ''' 使用提供的参数对指定的数据库连接对象执行命令(返回1x1结果集)
    ''' 示例:
    ''' Dim orderCount As Integer = CInt(ExecuteScalar(conn, CommandType.StoredProcedure, "GetOrderCount", new OleDbParameter("@prodid", 24)))
    ''' </summary>
    ''' <param name="connection">一个有效的数据库连接对象</param>
    ''' <param name="commandType">命令类型 (存储过程,命令文本等)</param>
    ''' <param name="commandText">存储过程名或SQL语句</param>
    ''' <param name="commandParameters">用于执行命令的OleDbParameters参数数组</param>
    ''' <returns>包含命令生成的1x1(第一行第一列)结果集中的值的对象</returns>
    Public Overloads Shared Function ExecuteScalar(ByVal connection As OleDbConnection,
                                                   ByVal commandType As CommandType,
                                                   ByVal commandText As String,
                                                   ByVal ParamArray commandParameters() As OleDbParameter) As Object

        If (connection Is Nothing) Then Throw New ArgumentNullException("connection")

        '创建命令并准备执行它
        Dim cmd As New OleDbCommand
        Dim retval As Object
        Dim mustCloseConnection As Boolean = False

        PrepareCommand(cmd, connection, CType(Nothing, OleDbTransaction), commandType, commandText, commandParameters, mustCloseConnection)

        '执行命令并返回结果
        retval = cmd.ExecuteScalar()

        '清空命令里的参数
        cmd.Parameters.Clear()

        If (mustCloseConnection) Then connection.Close()

        Return retval

    End Function ' ExecuteScalar

    ''' <summary>
    ''' 对提供的OleDbTransaction(事务)执行OleDbCommand命令(返回1x1结果集,不带参数)。
    ''' 示例:
    ''' Dim orderCount As Integer  = CInt(ExecuteScalar(trans, CommandType.StoredProcedure, "GetOrderCount"))
    ''' </summary>
    ''' <param name="transaction">有效的事务</param>
    ''' <param name="commandType">命令类型 (存储过程,命令文本等)</param>
    ''' <param name="commandText">存储过程名或SQL语句</param>
    ''' <returns>包含命令生成的1x1(第一行第一列)结果集中的值的对象</returns>
    Public Overloads Shared Function ExecuteScalar(ByVal transaction As OleDbTransaction,
                                                   ByVal commandType As CommandType,
                                                   ByVal commandText As String) As Object
        '执行参数为空的方法
        Return ExecuteScalar(transaction, commandType, commandText, CType(Nothing, OleDbParameter()))
    End Function ' ExecuteScalar

    ''' <summary>
    ''' 使用提供的参数对指定的OleDbTransaction(事务)执行OleDbCommand命令(返回1x1结果集)
    ''' 示例:
    ''' Dim orderCount As Integer = CInt(ExecuteScalar(trans, CommandType.StoredProcedure, "GetOrderCount", new OleDbParameter("@prodid", 24)))
    ''' </summary>
    ''' <param name="transaction">有效的事务</param>
    ''' <param name="commandType">命令类型 (存储过程,命令文本等)</param>
    ''' <param name="commandText">存储过程名或SQL语句</param>
    ''' <param name="commandParameters">用于执行命令的OleDbParameters参数数组</param>
    ''' <returns>包含命令生成的1x1(第一行第一列)结果集中的值的对象</returns>
    Public Overloads Shared Function ExecuteScalar(ByVal transaction As OleDbTransaction,
                                                   ByVal commandType As CommandType,
                                                   ByVal commandText As String,
                                                   ByVal ParamArray commandParameters() As OleDbParameter) As Object
        If (transaction Is Nothing) Then Throw New ArgumentNullException("transaction")
        If Not (transaction Is Nothing) AndAlso (transaction.Connection Is Nothing) Then Throw New ArgumentException("The transaction was rollbacked or commited, please provide an open transaction.", "transaction")

        '创建命令并准备执行它
        Dim cmd As New OleDbCommand
        Dim retval As Object
        Dim mustCloseConnection As Boolean = False

        PrepareCommand(cmd, transaction.Connection, transaction, commandType, commandText, commandParameters, mustCloseConnection)

        '执行命令并返回结果
        retval = cmd.ExecuteScalar()

        '清空命令里的参数
        cmd.Parameters.Clear()

        Return retval
    End Function ' ExecuteScalar
#End Region

#Region "UpdateDataset"
    ' Executes the respective command for each inserted, updated, or deleted row in the DataSet.
    ' e.g.:  
    '   UpdateDataset(conn, insertCommand, deleteCommand, updateCommand, dataSet, "Order")
    ' Parameters:
    ' -insertCommand: A valid transact-OleDb statement or stored procedure to insert new records into the data source
    ' -deleteCommand: A valid transact-OleDb statement or stored procedure to delete records from the data source
    ' -updateCommand: A valid transact-OleDb statement or stored procedure used to update records in the data source
    ' -dataSet: the DataSet used to update the data source
    ' -tableName: the DataTable used to update the data source
    ''' <summary>
    ''' 对数据集中的每个插入、更新或删除行执行相应的命令,更新到数据库。
    ''' </summary>
    ''' <param name="insertCommand">[追加记录]一个有效InsertCommand对象</param>
    ''' <param name="deleteCommand">[删除记录]一个有效的DeleteCommand对象</param>
    ''' <param name="updateCommand">[更新记录]一个有效的UpdateCommand对象</param>
    ''' <param name="dataSet">要更新到数据库的DataSet</param>
    ''' <param name="tableName">要更新到数据库的DataTable</param>
    Public Overloads Shared Sub UpdateDataset(ByVal insertCommand As OleDbCommand, ByVal deleteCommand As OleDbCommand, ByVal updateCommand As OleDbCommand, ByVal dataSet As DataSet, ByVal tableName As String)

        If (insertCommand Is Nothing) Then Throw New ArgumentNullException("insertCommand")
        If (deleteCommand Is Nothing) Then Throw New ArgumentNullException("deleteCommand")
        If (updateCommand Is Nothing) Then Throw New ArgumentNullException("updateCommand")
        If (dataSet Is Nothing) Then Throw New ArgumentNullException("dataSet")
        If (tableName Is Nothing OrElse tableName.Length = 0) Then Throw New ArgumentNullException("tableName")

        '创建一个OleDbDataAdapter,并在完成后处理它
        Dim dataAdapter As New OleDbDataAdapter
        Try
            '设置数据适配器命令
            dataAdapter.UpdateCommand = updateCommand
            dataAdapter.InsertCommand = insertCommand
            dataAdapter.DeleteCommand = deleteCommand

            '更新数据源中的数据集更改
            dataAdapter.Update(dataSet, tableName)

            '提交对数据集所做的所有更改
            dataSet.AcceptChanges()
        Finally
            If (Not dataAdapter Is Nothing) Then dataAdapter.Dispose()
        End Try
    End Sub
#End Region
End Class
相关推荐
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页