📄 oledbhelper.vb
字号:
' 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
' -dataSet: A dataset wich will contain the resultset generated by the command
' -tableNames: this array will be used to create table mappings allowing the DataTables to be referenced
' by a user defined name (probably the actual table name)
' -commandParameters: An array of OleDBParamters used to execute the command
Public Overloads Shared Sub FillDataset(ByVal connectionString As String, ByVal commandType As CommandType, ByVal commandText As String, ByVal dataSet As DataSet, _
ByVal tableNames() As String, ByVal ParamArray commandParameters() As OleDbParameter)
If (connectionString Is Nothing OrElse connectionString.Length = 0) Then Throw New ArgumentNullException("connectionString")
If (dataSet Is Nothing) Then Throw New ArgumentNullException("dataSet")
' 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
FillDataset(connection, commandType, commandText, dataSet, tableNames, commandParameters)
Finally
If Not connection Is Nothing Then connection.Dispose()
End Try
End Sub
' Execute a OleDBCommand (that returns a resultset and takes no parameters) against the provided OleDBConnection.
' e.g.:
' FillDataset (conn, CommandType.StoredProcedure, "GetOrders", ds, new String() {"orders"})
' Parameters:
' -connection: A valid OleDBConnection
' -commandType: the CommandType (stored procedure, text, etc.)
' -commandText: the stored procedure name or T-OleDB command
' -dataSet: A dataset wich will contain the resultset generated by the command
' -tableNames: this array will be used to create table mappings allowing the DataTables to be referenced
' by a user defined name (probably the actual table name)
Public Overloads Shared Sub FillDataset(ByVal connection As OleDbConnection, ByVal commandType As CommandType, _
ByVal commandText As String, ByVal dataSet As DataSet, ByVal tableNames As String())
FillDataset(connection, commandType, commandText, dataSet, tableNames, Nothing)
End Sub
' Execute a OleDBCommand (that returns a resultset) against the specified OleDBConnection
' using the provided parameters.
' e.g.:
' FillDataset (conn, CommandType.StoredProcedure, "GetOrders", ds, new String() {"orders"}, 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
' -dataSet: A dataset wich will contain the resultset generated by the command
' -tableNames: this array will be used to create table mappings allowing the DataTables to be referenced
' by a user defined name (probably the actual table name)
' -commandParameters: An array of OleDBParamters used to execute the command
Public Overloads Shared Sub FillDataset(ByVal connection As OleDbConnection, ByVal commandType As CommandType, _
ByVal commandText As String, ByVal dataSet As DataSet, ByVal tableNames As String(), _
ByVal ParamArray commandParameters() As OleDbParameter)
FillDataset(connection, Nothing, commandType, commandText, dataSet, tableNames, commandParameters)
End Sub
' Execute a OleDBCommand (that returns a resultset and takes no parameters) against the provided OleDBTransaction.
' e.g.:
' FillDataset (trans, CommandType.StoredProcedure, "GetOrders", ds, new string() {"orders"})
' Parameters:
' -transaction: A valid OleDBTransaction
' -commandType: the CommandType (stored procedure, text, etc.)
' -commandText: the stored procedure name or T-OleDB command
' -dataSet: A dataset wich will contain the resultset generated by the command
' -tableNames: this array will be used to create table mappings allowing the DataTables to be referenced
' by a user defined name (probably the actual table name)
Public Overloads Shared Sub FillDataset(ByVal transaction As OleDbTransaction, ByVal commandType As CommandType, _
ByVal commandText As String, ByVal dataSet As DataSet, ByVal tableNames() As String)
FillDataset(transaction, commandType, commandText, dataSet, tableNames, Nothing)
End Sub
' Execute a OleDBCommand (that returns a resultset) against the specified OleDBTransaction
' using the provided parameters.
' e.g.:
' FillDataset(trans, CommandType.StoredProcedure, "GetOrders", ds, new string() {"orders"}, 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
' -dataSet: A dataset wich will contain the resultset generated by the command
' -tableNames: this array will be used to create table mappings allowing the DataTables to be referenced
' by a user defined name (probably the actual table name)
' -commandParameters: An array of OleDBParamters used to execute the command
Public Overloads Shared Sub FillDataset(ByVal transaction As OleDbTransaction, ByVal commandType As CommandType, _
ByVal commandText As String, ByVal dataSet As DataSet, ByVal tableNames() As String, _
ByVal ParamArray commandParameters() As OleDbParameter)
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")
FillDataset(transaction.Connection, transaction, commandType, commandText, dataSet, tableNames, commandParameters)
End Sub
' Private helper method that execute a OleDBCommand (that returns a resultset) against the specified OleDBTransaction and OleDBConnection
' using the provided parameters.
' e.g.:
' FillDataset(conn, trans, CommandType.StoredProcedure, "GetOrders", ds, new String() {"orders"}, new OleDBParameter("@prodid", 24))
' Parameters:
' -connection: A valid OleDBConnection
' -transaction: A valid OleDBTransaction
' -commandType: the CommandType (stored procedure, text, etc.)
' -commandText: the stored procedure name or T-OleDB command
' -dataSet: A dataset wich will contain the resultset generated by the command
' -tableNames: this array will be used to create table mappings allowing the DataTables to be referenced
' by a user defined name (probably the actual table name)
' -commandParameters: An array of OleDBParamters used to execute the command
Private Overloads Shared Sub FillDataset(ByVal connection As OleDbConnection, ByVal transaction As OleDbTransaction, ByVal commandType As CommandType, _
ByVal commandText As String, ByVal dataSet As DataSet, ByVal tableNames() As String, _
ByVal ParamArray commandParameters() As OleDbParameter)
If (connection Is Nothing) Then Throw New ArgumentNullException("connection")
If (dataSet Is Nothing) Then Throw New ArgumentNullException("dataSet")
' Create a command and prepare it for execution
Dim command As New OleDbCommand()
Dim mustCloseConnection As Boolean = False
PrepareCommand(command, connection, transaction, commandType, commandText, commandParameters, mustCloseConnection)
' Create the DataAdapter & DataSet
Dim dataAdapter As OleDbDataAdapter = New OleDbDataAdapter(command)
Try
' Add the table mappings specified by the user
If Not tableNames Is Nothing AndAlso tableNames.Length > 0 Then
Dim tableName As String = "Table"
Dim index As Integer
For index = 0 To tableNames.Length - 1
If (tableNames(index) Is Nothing OrElse tableNames(index).Length = 0) Then Throw New ArgumentException("The tableNames parameter must contain a list of tables, a value was provided as null or empty string.", "tableNames")
dataAdapter.TableMappings.Add(tableName, tableNames(index))
tableName = tableName & (index + 1).ToString()
Next
End If
' Fill the DataSet using default values for DataTable names, etc
dataAdapter.Fill(dataSet)
Finally
If (Not dataAdapter Is Nothing) Then dataAdapter.Dispose()
End Try
If (mustCloseConnection) Then connection.Close()
End Sub
#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
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")
' Create a OleDBDataAdapter, and dispose of it after we are done
Dim dataAdapter As New OleDbDataAdapter()
Try
' Set the data adapter commands
dataAdapter.UpdateCommand = updateCommand
dataAdapter.InsertCommand = insertCommand
dataAdapter.DeleteCommand = deleteCommand
' Update the dataset changes in the data source
dataAdapter.Update(dataSet, tableName)
' Commit all the changes made to the DataSet
dataSet.AcceptChanges()
Finally
If (Not dataAdapter Is Nothing) Then dataAdapter.Dispose()
End Try
End Sub
#End Region
End Class ' OleDBHelper
' OleDBHelperParameterCache provides functions to leverage a static cache of procedure parameters, and the
' ability to discover parameters for stored procedures at run-time.
Public NotInheritable Class OleDBHelperParameterCache
#Region "private methods, variables, and constructors"
' Since this class provides only static methods, make the default constructor private to prevent
' instances from being created with "new OleDBHelperParameterCache()".
Private Sub New()
End Sub ' New
Private Shared paramCache As Hashtable = Hashtable.Synchronized(New Hashtable())
' Deep copy of cached OleDBParameter array
Private Shared Function CloneParameters(ByVal originalParameters() As OleDbParameter) As OleDbParameter()
Dim i As Integer
Dim j As Integer = originalParameters.Length - 1
Dim clonedParameters(j) As OleDbParameter
For i = 0 To j
clonedParameters(i) = CType(CType(originalParameters(i), ICloneable).Clone, OleDbParameter)
Next
Return clonedParameters
End Function ' CloneParameters
#End Region
#Region "caching functions"
' add parameter array to the cache
' Parameters
' -connectionString - a valid connection string for a OleDBConnection
' -commandText - the stored procedure name or T-OleDB command
' -commandParameters - an array of OleDBParamters to be cached
Public Shared Sub CacheParameterSet(ByVal connectionString As String, _
ByVal commandText As String, _
ByVal ParamArray commandParameters() As OleDbParameter)
If (connectionString Is Nothing OrElse connectionString.Length = 0) Then Throw New ArgumentNullException("connectionString")
If (commandText Is Nothing OrElse commandText.Length = 0) Then Throw New ArgumentNullException("commandText")
Dim hashKey As String = connectionString + ":" + commandText
paramCache(hashKey) = commandParameters
End Sub ' CacheParameterSet
' retrieve a parameter array from the cache
' Parameters:
' -connectionString - a valid connection string for a OleDBConnection
' -commandText - the stored procedure name or T-OleDB command
' Returns: An array of OleDBParamters
Public Shared Function GetCachedParameterSet(ByVal connectionString As String, ByVal commandText As String) As OleDbParameter()
If (connectionString Is Nothing OrElse connectionString.Length = 0) Then Throw New ArgumentNullException("connectionString")
If (commandText Is Nothing OrElse commandText.Length = 0) Then Throw New ArgumentNullException("commandText")
Dim hashKey As String = connectionString + ":" + commandText
Dim cachedParameters As OleDbParameter() = CType(paramCache(hashKey), OleDbParameter())
If cachedParameters Is Nothing Then
Return Nothing
Else
Return CloneParameters(cachedParameters)
End If
End Function ' GetCachedParameterSet
#End Region
End Class ' OleDBHelperParameterCache
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -