📄 c_sqlhelper.vb
字号:
' -spName - the name of the stored procedure
' -parameterValues - an array of objects to be assigned as the input values of the stored procedure
' Returns: An int representing the number of rows affected by the command
Public Overloads Shared Function ExecuteNonQuery(ByVal connectionString As String, _
ByVal spName As String, _
ByVal ParamArray parameterValues() As Object) As Integer
If (connectionString Is Nothing OrElse connectionString.Length = 0) Then Throw New ArgumentNullException("connectionString")
If (spName Is Nothing OrElse spName.Length = 0) Then Throw New ArgumentNullException("spName")
Dim commandParameters As oledbParameter()
' If we receive parameter values, we need to figure out where they go
If Not (parameterValues Is Nothing) AndAlso parameterValues.Length > 0 Then
' Pull the parameters for this stored procedure from the parameter cache (or discover them & populate the cache)
commandParameters = SqlHelperParameterCache.GetSpParameterSet(connectionString, spName)
' Assign the provided values to these parameters based on parameter order
AssignParameterValues(commandParameters, parameterValues)
' Call the overload that takes an array of oledbParameters
Return ExecuteNonQuery(connectionString, CommandType.StoredProcedure, spName, commandParameters)
' Otherwise we can just call the SP without params
Else
Return ExecuteNonQuery(connectionString, CommandType.StoredProcedure, spName)
End If
End Function ' ExecuteNonQuery
' Execute a oledbCommand (that returns no resultset and takes no parameters) against the provided oledbconnection.
' e.g.:
' Dim result As Integer = ExecuteNonQuery(conn, CommandType.StoredProcedure, "PublishOrders")
' Parameters:
' -connection - a valid oledbconnection
' -commandType - the CommandType (stored procedure, text, etc.)
' -commandText - the stored procedure name or T-SQL command
' Returns: An int representing the number of rows affected by the command
Public Overloads Shared Function ExecuteNonQuery(ByVal connection As oledbconnection, _
ByVal commandType As CommandType, _
ByVal commandText As String) As Integer
' Pass through the call providing null for the set of oledbParameters
Return ExecuteNonQuery(connection, commandType, commandText, CType(Nothing, oledbParameter()))
End Function ' ExecuteNonQuery
' Execute a oledbCommand (that returns no resultset) against the specified oledbconnection
' using the provided parameters.
' e.g.:
' Dim result As Integer = ExecuteNonQuery(conn, CommandType.StoredProcedure, "PublishOrders", new oledbParameter("@prodid", 24))
' Parameters:
' -connection - a valid oledbconnection
' -commandType - the CommandType (stored procedure, text, etc.)
' -commandText - the stored procedure name or T-SQL command
' -commandParameters - an array of SqlParamters used to execute the command
' Returns: An int representing the number of rows affected by the command
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")
' Create a command and prepare it for execution
Dim cmd As New oledbCommand
Dim retval As Integer
Dim mustCloseConnection As Boolean = False
PrepareCommand(cmd, connection, CType(Nothing, oledbTransaction), commandType, commandText, commandParameters, mustCloseConnection)
cmd.CommandTimeout = 200
' Finally, execute the command
retval = cmd.ExecuteNonQuery()
' Detach the oledbParameters from the command object, so they can be used again
cmd.Parameters.Clear()
If (mustCloseConnection) Then connection.Close()
Return retval
End Function ' ExecuteNonQuery
' Execute a stored procedure via a oledbCommand (that returns no resultset) against the specified oledbconnection
' using the provided parameter values. This method will discover the parameters for the
' stored procedure, and assign the values based on parameter order.
' This method provides no access to output parameters or the stored procedure' s return value parameter.
' e.g.:
' Dim result As integer = ExecuteNonQuery(conn, "PublishOrders", 24, 36)
' Parameters:
' -connection - a valid oledbconnection
' -spName - the name of the stored procedure
' -parameterValues - an array of objects to be assigned as the input values of the stored procedure
' Returns: An int representing the number of rows affected by the command
Public Overloads Shared Function ExecuteNonQuery(ByVal connection As oledbconnection, _
ByVal spName As String, _
ByVal ParamArray parameterValues() As Object) As Integer
If (connection Is Nothing) Then Throw New ArgumentNullException("connection")
If (spName Is Nothing OrElse spName.Length = 0) Then Throw New ArgumentNullException("spName")
Dim commandParameters As oledbParameter()
' If we receive parameter values, we need to figure out where they go
If Not (parameterValues Is Nothing) AndAlso parameterValues.Length > 0 Then
' Pull the parameters for this stored procedure from the parameter cache (or discover them & populate the cache)
commandParameters = SqlHelperParameterCache.GetSpParameterSet(connection, spName)
' Assign the provided values to these parameters based on parameter order
AssignParameterValues(commandParameters, parameterValues)
' Call the overload that takes an array of oledbParameters
Return ExecuteNonQuery(connection, CommandType.StoredProcedure, spName, commandParameters)
Else ' Otherwise we can just call the SP without params
Return ExecuteNonQuery(connection, CommandType.StoredProcedure, spName)
End If
End Function ' ExecuteNonQuery
' Execute a oledbCommand (that returns no resultset and takes no parameters) against the provided oledbTransaction.
' e.g.:
' Dim result As Integer = ExecuteNonQuery(trans, CommandType.StoredProcedure, "PublishOrders")
' Parameters:
' -transaction - a valid oledbTransaction associated with the connection
' -commandType - the CommandType (stored procedure, text, etc.)
' -commandText - the stored procedure name or T-SQL 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) As Integer
' Pass through the call providing null for the set of oledbParameters
Return ExecuteNonQuery(transaction, commandType, commandText, CType(Nothing, oledbParameter()))
End Function ' ExecuteNonQuery
' 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-SQL command
' -commandParameters - an array of SqlParamters 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()
' Detach the oledbParameters from the command object, so they can be used again
cmd.Parameters.Clear()
Return retval
End Function ' ExecuteNonQuery
' Execute a stored procedure via a oledbCommand (that returns no resultset) against the specified oledbTransaction
' using the provided parameter values. This method will discover the parameters for the
' stored procedure, and assign the values based on parameter order.
' This method provides no access to output parameters or the stored procedure' s return value parameter.
' e.g.:
' Dim result As Integer = SqlHelper.ExecuteNonQuery(trans, "PublishOrders", 24, 36)
' Parameters:
' -transaction - a valid oledbTransaction
' -spName - the name of the stored procedure
' -parameterValues - an array of objects to be assigned as the input values of the stored procedure
' Returns: An int representing the number of rows affected by the command
Public Overloads Shared Function ExecuteNonQuery(ByVal transaction As oledbTransaction, _
ByVal spName As String, _
ByVal ParamArray parameterValues() As Object) 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")
If (spName Is Nothing OrElse spName.Length = 0) Then Throw New ArgumentNullException("spName")
Dim commandParameters As oledbParameter()
' If we receive parameter values, we need to figure out where they go
If Not (parameterValues Is Nothing) AndAlso parameterValues.Length > 0 Then
' Pull the parameters for this stored procedure from the parameter cache (or discover them & populate the cache)
commandParameters = SqlHelperParameterCache.GetSpParameterSet(transaction.Connection, spName)
' Assign the provided values to these parameters based on parameter order
AssignParameterValues(commandParameters, parameterValues)
' Call the overload that takes an array of oledbParameters
Return ExecuteNonQuery(transaction, CommandType.StoredProcedure, spName, commandParameters)
Else ' Otherwise we can just call the SP without params
Return ExecuteNonQuery(transaction, CommandType.StoredProcedure, spName)
End If
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 = SqlHelper.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-SQL 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-SQL command
' -commandParameters - an array of SqlParamters 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, _
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -