📄 oledbhelper.vb
字号:
' Execute a OleDBCommand (that returns no resultset) against the specified OleDBTransaction
' using the provided parameters.
' e.g.:
' Dim result As Integer = ExecuteNonQuery(trans, CommandType.StoredProcedure, "GetOrders", new OleDBParameter("@prodid", 24))
' Parameters:
' -transaction - a valid OleDBTransaction
' -commandType - the CommandType (stored procedure, text, etc.)
' -commandText - the stored procedure name or T-OleDB command
' -commandParameters - an array of OleDBParamters used to execute the command
' Returns: An int representing the number of rows affected by the command
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")
' Create a command and prepare it for execution
Dim cmd As New OleDbCommand()
Dim retval As Integer
Dim mustCloseConnection As Boolean = False
PrepareCommand(cmd, transaction.Connection, transaction, commandType, commandText, commandParameters, mustCloseConnection)
' Finally, execute the command
retval = cmd.ExecuteNonQuery()
Return retval
End Function ' ExecuteNonQuery
#End Region
#Region "ExecuteDataset"
' Execute a OleDBCommand (that returns a resultset and takes no parameters) against the database specified in
' the connection string.
' e.g.:
' Dim ds As DataSet = OleDBHelper.ExecuteDataset("", commandType.StoredProcedure, "GetOrders")
' Parameters:
' -connectionString - a valid connection string for a OleDBConnection
' -commandType - the CommandType (stored procedure, text, etc.)
' -commandText - the stored procedure name or T-OleDB command
' Returns: A dataset containing the resultset generated by the command
Public Overloads Shared Function ExecuteDataset(ByVal connectionString As String, _
ByVal commandType As CommandType, _
ByVal commandText As String) As DataSet
' Pass through the call providing null for the set of OleDBParameters
Return ExecuteDataset(connectionString, commandType, commandText, CType(Nothing, OleDbParameter()))
End Function ' ExecuteDataset
' Execute a OleDBCommand (that returns a resultset) against the database specified in the connection string
' using the provided parameters.
' e.g.:
' Dim ds As Dataset = ExecuteDataset(connString, CommandType.StoredProcedure, "GetOrders", new OleDBParameter("@prodid", 24))
' Parameters:
' -connectionString - a valid connection string for a OleDBConnection
' -commandType - the CommandType (stored procedure, text, etc.)
' -commandText - the stored procedure name or T-OleDB command
' -commandParameters - an array of OleDBParamters used to execute the command
' Returns: A dataset containing the resultset generated by the command
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")
' Create & open a OleDBConnection, and dispose of it after we are done
Dim connection As OleDbConnection
Try
connection = New OleDbConnection(connectionString)
connection.Open()
' Call the overload that takes a connection in place of the connection string
Return ExecuteDataset(connection, commandType, commandText, commandParameters)
Finally
If Not connection Is Nothing Then connection.Dispose()
End Try
End Function ' ExecuteDataset
' Execute a OleDBCommand (that returns a resultset and takes no parameters) against the provided OleDBConnection.
' e.g.:
' Dim ds As Dataset = ExecuteDataset(conn, CommandType.StoredProcedure, "GetOrders")
' Parameters:
' -connection - a valid OleDBConnection
' -commandType - the CommandType (stored procedure, text, etc.)
' -commandText - the stored procedure name or T-OleDB command
' Returns: A dataset containing the resultset generated by the command
Public Overloads Shared Function ExecuteDataset(ByVal connection As OleDbConnection, _
ByVal commandType As CommandType, _
ByVal commandText As String) As DataSet
' Pass through the call providing null for the set of OleDBParameters
Return ExecuteDataset(connection, commandType, commandText, CType(Nothing, OleDbParameter()))
End Function ' ExecuteDataset
' Execute a OleDBCommand (that returns a resultset) against the specified OleDBConnection
' using the provided parameters.
' e.g.:
' Dim ds As Dataset = ExecuteDataset(conn, CommandType.StoredProcedure, "GetOrders", new OleDBParameter("@prodid", 24))
' Parameters:
' -connection - a valid OleDBConnection
' -commandType - the CommandType (stored procedure, text, etc.)
' -commandText - the stored procedure name or T-OleDB command
' -commandParameters - an array of OleDBParamters used to execute the command
' Returns: A dataset containing the resultset generated by the command
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")
' Create a command and prepare it for execution
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
' Create the DataAdapter & DataSet
dataAdatpter = New OleDbDataAdapter(cmd)
' Fill the DataSet using default values for DataTable names, etc
dataAdatpter.Fill(ds)
Finally
If (Not dataAdatpter Is Nothing) Then dataAdatpter.Dispose()
End Try
If (mustCloseConnection) Then connection.Close()
' Return the dataset
Return ds
End Function ' ExecuteDataset
' Execute a OleDBCommand (that returns a resultset and takes no parameters) against the provided OleDBTransaction.
' e.g.:
' Dim ds As Dataset = ExecuteDataset(trans, CommandType.StoredProcedure, "GetOrders")
' Parameters
' -transaction - a valid OleDBTransaction
' -commandType - the CommandType (stored procedure, text, etc.)
' -commandText - the stored procedure name or T-OleDB command
' Returns: A dataset containing the resultset generated by the command
Public Overloads Shared Function ExecuteDataset(ByVal transaction As OleDbTransaction, _
ByVal commandType As CommandType, _
ByVal commandText As String) As DataSet
' Pass through the call providing null for the set of OleDBParameters
Return ExecuteDataset(transaction, commandType, commandText, CType(Nothing, OleDbParameter()))
End Function ' ExecuteDataset
' Execute a OleDBCommand (that returns a resultset) against the specified OleDBTransaction
' using the provided parameters.
' e.g.:
' Dim ds As Dataset = ExecuteDataset(trans, CommandType.StoredProcedure, "GetOrders", new OleDBParameter("@prodid", 24))
' Parameters
' -transaction - a valid OleDBTransaction
' -commandType - the CommandType (stored procedure, text, etc.)
' -commandText - the stored procedure name or T-OleDB command
' -commandParameters - an array of OleDBParamters used to execute the command
' Returns: A dataset containing the resultset generated by the command
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")
' Create a command and prepare it for execution
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
' Create the DataAdapter & DataSet
dataAdatpter = New OleDbDataAdapter(cmd)
' Fill the DataSet using default values for DataTable names, etc
dataAdatpter.Fill(ds)
Finally
If (Not dataAdatpter Is Nothing) Then dataAdatpter.Dispose()
End Try
' Return the dataset
Return ds
End Function ' ExecuteDataset
#End Region
#Region "ExecuteReader"
' this enum is used to indicate whether the connection was provided by the caller, or created by OleDBHelper, so that
' we can set the appropriate CommandBehavior when calling ExecuteReader()
Private Enum OleDBConnectionOwnership
' Connection is owned and managed by OleDBHelper
Internal
' Connection is owned and managed by the caller
[External]
End Enum ' OleDBConnectionOwnership
' Create and prepare a OleDBCommand, and call ExecuteReader with the appropriate CommandBehavior.
' If we created and opened the connection, we want the connection to be closed when the DataReader is closed.
' If the caller provided the connection, we want to leave it to them to manage.
' Parameters:
' -connection - a valid OleDBConnection, on which to execute this command
' -transaction - a valid OleDBTransaction, or ' null'
' -commandType - the CommandType (stored procedure, text, etc.)
' -commandText - the stored procedure name or SQL command
' -commandParameters - an array of OleDBParameters to be associated with the command or ' null' if no parameters are required
' -connectionOwnership - indicates whether the connection parameter was provided by the caller, or created by OleDBHelper
' Returns: OleDBDataReader containing the results of the command
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
' Create a command and prepare it for execution
Dim cmd As New OleDbCommand()
Try
' Create a reader
Dim dataReader As OleDbDataReader
PrepareCommand(cmd, connection, transaction, commandType, commandText, commandParameters, mustCloseConnection)
' Call ExecuteReader with the appropriate CommandBehavior
If connectionOwnership = OleDBConnectionOwnership.External Then
dataReader = cmd.ExecuteReader()
Else
dataReader = cmd.ExecuteReader(CommandBehavior.CloseConnection)
End If
Return dataReader
Catch
If (mustCloseConnection) Then connection.Close()
Throw
End Try
End Function ' ExecuteReader
' Execute a OleDBCommand (that returns a resultset and takes no parameters) against the database specified in
' the connection string.
' e.g.:
' Dim dr As OleDBDataReader = ExecuteReader(connString, CommandType.StoredProcedure, "GetOrders")
' Parameters:
' -connectionString - a valid connection string for a OleDBConnection
' -commandType - the CommandType (stored procedure, text, etc.)
' -commandText - the stored procedure name or SQL command
' Returns: A OleDBDataReader containing the resultset generated by the command
Public Overloads Shared Function ExecuteReader(ByVal connectionString As String, _
ByVal commandType As CommandType, _
ByVal commandText As String) As OleDbDataReader
' Pass through the call providing null for the set of OleDBParameters
Return ExecuteReader(connectionString, commandType, commandText, CType(Nothing, OleDbParameter()))
End Function ' ExecuteReader
' Execute a OleDBCommand (that returns a resultset) against the database specified in the connection string
' using the provided parameters.
' e.g.:
' Dim dr As OleDBDataReader = ExecuteReader(connString, CommandType.StoredProcedure, "GetOrders", new OleDBParameter("@prodid", 24))
' Parameters:
' -connectionString - a valid connection string for a OleDBConnection
' -commandType - the CommandType (stored procedure, text, etc.)
' -commandText - the stored procedure name or SQL command
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -