回 帖 发 新 帖 刷新版面

主题:[原创]AdodbHelper更新啦

代码功能:

封装常用的数据库访问操作,注意一下,我们这个类支持的是Access数据库。

代码路径:https://code.csdn.net/snippets/1576301

代码:

'////////说明////////
'使用前,请先引用:Microsoft ActiveX Data Objects 2.5 Library
'引用方式:工程→引用→Microsoft ActiveX Data Objects 2.5 Library


'最后修改日期:2015年1月15日22:16:43
'2015年01月15日22:19:23   全部重新格式化
'2016年02月09日15:33:17   更新部分Adodb缺失
'成员变量:连接对象
Private m_Conn       As ADODB.Connection

'成员变量:SQL命令对象
Private m_Command    As ADODB.Command

'成员变量:连接字符串
Private m_ConnString As String

'成员变量:数据库文件路径
Private m_FilePath   As String

'成员变量:自身对象
Private m_Me         As New AdodbHelper

'规定:每个函数执行完成后,都必须清空m_Command,并且创建一个空的对象
'【类初始化、释放】
'<CSCM>
'--------------------------------------------------------------------------------
' Project    :       类库构建项目
' Procedure  :       Class_Initialize
' Description:       [type_description_here]
' Created by :       Project Administrator
' Machine    :       MYSOFT-SUNR01
' Date-Time  :       1-18-2015-18:19:56
'
' Parameters :
'--------------------------------------------------------------------------------
'</CSCM>
Private Sub Class_Initialize()
End Sub

'<CSCM>
'--------------------------------------------------------------------------------
' Project    :       类库构建项目
' Procedure  :       Class_Terminate
' Description:       [type_description_here]
' Created by :       Project Administrator
' Machine    :       MYSOFT-SUNR01
' Date-Time  :       1-18-2015-18:19:56
'
' Parameters :
'--------------------------------------------------------------------------------
'</CSCM>
Private Sub Class_Terminate()
    Set m_Conn = Nothing
    Set m_Command = Nothing
End Sub

'【类属性】
'读取:数据库连接字符串
'<CSCM>
'--------------------------------------------------------------------------------
' Project    :       类库构建项目
' Procedure  :       ConnectionString
' Description:       [type_description_here]
' Created by :       Project Administrator
' Machine    :       MYSOFT-SUNR01
' Date-Time  :       1-18-2015-18:19:56
'
' Parameters :
'--------------------------------------------------------------------------------
'</CSCM>
Public Property Get ConnectionString() As String
    ConnectionString = m_ConnString
End Property

'设置:连接字符串
'<CSCM>
'--------------------------------------------------------------------------------
' Project    :       类库构建项目
' Procedure  :       ConnectionString
' Description:       [type_description_here]
' Created by :       Project Administrator
' Machine    :       MYSOFT-SUNR01
' Date-Time  :       1-18-2015-18:19:56
'
' Parameters :       vNewValue (String)
'--------------------------------------------------------------------------------
'</CSCM>
Public Property Let ConnectionString(ByVal vNewValue As String)
    m_ConnString = vNewValue
End Property

'类是否准备好,是否可以执行SQL
'<CSCM>
'--------------------------------------------------------------------------------
' Project    :       类库构建项目
' Procedure  :       IsReady
' Description:       [type_description_here]
' Created by :       Project Administrator
' Machine    :       MYSOFT-SUNR01
' Date-Time  :       1-18-2015-18:19:56
'
' Parameters :
'--------------------------------------------------------------------------------
'</CSCM>
Public Property Get IsReady() As Boolean
    IsReady = IIf(Len(ConnectionString) > 0, True, False)
End Property

'【类方法】
'执行查询,返回离线记录集
'<CSCM>
'--------------------------------------------------------------------------------
' Project    :       类库构建项目
' Procedure  :       ExecQuery
' Description:       [type_description_here]
' Created by :       Project Administrator
' Machine    :       MYSOFT-SUNR01
' Date-Time  :       1-18-2015-18:19:55
'
' Parameters :       SqlStr (String)
'--------------------------------------------------------------------------------
'</CSCM>
Public Function ExecQuery(ByVal SqlStr As String) As ADODB.Recordset

    Dim tempRes As New ADODB.Recordset

    Set m_Command = New ADODB.Command
    Call OpenConn
    m_Command.ActiveConnection = m_Conn
    m_Command.CommandText = SqlStr
    Set tempRes = m_Command.Execute()
    tempRes.ActiveConnection = Nothing
    Call CloseConn
    Set ExecQuery = tempRes
    Set m_Command = Nothing
End Function

'执行参数化查询,返回离线记录集
'<CSCM>
'--------------------------------------------------------------------------------
' Project    :       类库构建项目
' Procedure  :       ExecParamQuery
' Description:       [type_description_here]
' Created by :       Project Administrator
' Machine    :       MYSOFT-SUNR01
' Date-Time  :       1-18-2015-18:19:55
'
' Parameters :       SqlStr (String)
'                    Params() (Variant)
'--------------------------------------------------------------------------------
'</CSCM>
Public Function ExecParamQuery(ByVal SqlStr As String, _
                               ParamArray Params()) As ADODB.Recordset

    Dim tempRes As New ADODB.Recordset

    Dim i       As Long
    
    Dim paramArr As Variant

    Set m_Command = New ADODB.Command
    '打开连接
    Call OpenConn
    m_Command.ActiveConnection = m_Conn
    m_Command.CommandText = SqlStr
    m_Command.CommandType = adCmdText
    
    '过滤嵌套情况
    paramArr = Params
    If VarType(Params(0)) = 8204 Then
      paramArr = Params(0)
    End If

    '设置参数
    With m_Command
    

        For Each param In paramArr

            Dim Para As ADODB.Parameter

            Set Para = .CreateParameter(CStr(i), GetVarType(param), adParamInput, LenB(param))
            Para.Value = param
            .Parameters.Append Para
        Next

    End With

    '获取执行后记录集
    Set tempRes = m_Command.Execute()
    '与数据库连接脱钩
    tempRes.ActiveConnection = Nothing
    '关闭数据库连接
    Call CloseConn
    '返回数据集对象引用
    Set ExecParamQuery = tempRes
    '清空命令对象
    Set m_Command = Nothing
End Function

'执行查询,返回影响行数
'<CSCM>
'--------------------------------------------------------------------------------
' Project    :       类库构建项目
' Procedure  :       ExecNonQuery
' Description:       [type_description_here]
' Created by :       Project Administrator
' Machine    :       MYSOFT-SUNR01
' Date-Time  :       1-18-2015-18:19:55
'
' Parameters :       SqlStr (String)
'--------------------------------------------------------------------------------
'</CSCM>
Public Function ExecNonQuery(ByVal SqlStr As String) As Long

    '定义影响行数变量
    Dim affectedRows As Long

    '创建绑定Command对象
    Set m_Command = New ADODB.Command
    '打开连接
    Call OpenConn
    '绑定Command到数据库连接
    m_Command.ActiveConnection = m_Conn
    '设置SQL语句
    m_Command.CommandText = SqlStr
    '设置SQL类型
    m_Command.CommandType = adCmdText
    '获取执行后影响行数
    m_Command.Execute affectedRows
    '关闭数据库连接
    Call CloseConn
    '清空命令对象
    Set m_Command = Nothing
    '返回影响行数
    ExecNonQuery = affectedRows
End Function

'执行参数化查询,返回影响行数
'<CSCM>
'--------------------------------------------------------------------------------
' Project    :       类库构建项目
' Procedure  :       ExecParamNonQuery
' Description:       [type_description_here]
' Created by :       Project Administrator
' Machine    :       MYSOFT-SUNR01
' Date-Time  :       1-18-2015-18:19:55
'
' Parameters :       SqlStr (String)
'                    Params() (Variant)
'--------------------------------------------------------------------------------
'</CSCM>
Public Function ExecParamNonQuery(ByVal SqlStr As String, ParamArray Params()) As Long

    Dim i            As Long

    Dim affectedRows As Long
    
    Dim paramArr As Variant

    Set m_Command = New ADODB.Command
    '打开连接
    Call OpenConn
    m_Command.ActiveConnection = m_Conn
    m_Command.CommandText = SqlStr
    m_Command.CommandType = adCmdText
    '过滤嵌套情况
    paramArr = Params
    If VarType(Params(0)) = 8204 Then
      paramArr = Params(0)
    End If
    '设置参数
    With m_Command

        For Each param In paramArr

            Dim Para As ADODB.Parameter

            Set Para = .CreateParameter(CStr(i), GetVarType(param), adParamInput, LenB(param))
            Para.Value = param
            .Parameters.Append Para
        Next

    End With

    '获取执行后记录集
    m_Command.Execute affectedRows
    '关闭数据库连接
    Call CloseConn
    '清空命令对象
    Set m_Command = Nothing
    '返回影响行数
    ExecParamNonQuery = affectedRows
End Function

'根据文件路径设置连接字符串
'<CSCM>
'--------------------------------------------------------------------------------
' Project    :       类库构建项目
' Procedure  :       SetConnToFile
' Description:       [type_description_here]
' Created by :       Project Administrator
' Machine    :       MYSOFT-SUNR01
' Date-Time  :       1-18-2015-18:19:55
'
' Parameters :       FilePath (String)
'--------------------------------------------------------------------------------
'</CSCM>
Public Sub SetConnToFile(ByVal FilePath As String)
    m_ConnString = "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source=" & FilePath & ";"
End Sub

'释放离线记录集
'<CSCM>
'--------------------------------------------------------------------------------
' Project    :       类库构建项目
' Procedure  :       ReleaseRecordset
' Description:       [type_description_here]
' Created by :       Project Administrator
' Machine    :       MYSOFT-SUNR01
' Date-Time  :       1-18-2015-18:19:55
'
' Parameters :       dbRes (ADODB.Recordset)
'--------------------------------------------------------------------------------
'</CSCM>
Public Sub ReleaseRecordset(ByRef dbRes As ADODB.Recordset)
    Set dbRes = Nothing
End Sub

'将制定表数据从一个数据库文件插入到另一个数据库文件中
'<CSCM>
'--------------------------------------------------------------------------------
' Project    :       类库构建项目
' Procedure  :       TransData
' Description:       [type_description_here]
' Created by :       Project Administrator
' Machine    :       MYSOFT-SUNR01
' Date-Time  :       1-18-2015-18:19:55
'
' Parameters :       SourceDb (String)
'                    DestDb (String)
'                    tableNames() (Variant)
'--------------------------------------------------------------------------------
'</CSCM>
Public Sub TransData(ByVal SourceDb As String, _
                     ByVal DestDb As String, _
                     ParamArray tableNames())

    Dim TableName As Variant

    m_Me.SetConnToFile SourceDb

    If UBound(tableNames) <= 0 Then

        Exit Sub

    End If

    For Each TableName In tableNames

        m_Me.ExecQuery "select * into [" & DestDb & "]." & TableName & " from " & TableName
    Next

End Sub

'从打开数据库文件,返回一个打开的数据库连接
'<CSCM>
'--------------------------------------------------------------------------------
' Project    :       类库构建项目
' Procedure  :       DbConnFromFile
' Description:       [type_description_here]
' Created by :       Project Administrator
' Machine    :       MYSOFT-SUNR01
' Date-Time  :       1-18-2015-18:19:55
'
' Parameters :       FilePath (String)
'--------------------------------------------------------------------------------
'</CSCM>
Public Function DbConnFromFile(ByVal FilePath As String) As ADODB.Connection

    Dim tmpConn As New ADODB.Connection

    tmpConn.CursorLocation = adUseClient
    tmpConn.Open "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source=" & FilePath & ";"
    Set DbConnFromFile = tmpConn
End Function

'执行参数化查询,返回首行,首列值
'<CSCM>
'--------------------------------------------------------------------------------
' Project    :       类库构建项目
' Procedure  :       ExecParamQueryScalar
' Description:       [type_description_here]
' Created by :       Project Administrator
' Machine    :       MYSOFT-SUNR01
' Date-Time  :       1-18-2015-18:19:55
'
' Parameters :       SqlStr (String)
'                    Params() (Variant)
'--------------------------------------------------------------------------------
'</CSCM>
Public Function ExecParamQueryScalar(ByVal SqlStr As String, _
                                     ParamArray Params()) As Variant

    Dim varResult As Variant

    Dim tmpRes    As ADODB.Recordset

    Set tmpRes = ExecParamQuery(SqlStr, Params)

    If tmpRes.RecordCount <= 0 Then
        Set ExecParamQueryScalar = Nothing
    Else
        ExecParamQueryScalar = tmpRes.fields(0).Value
    End If

    ReleaseRecordset tmpRes
End Function

'执行查询,返回首行,首列值
'<CSCM>
'--------------------------------------------------------------------------------
' Project    :       类库构建项目
' Procedure  :       ExecQueryScalar
' Description:       [type_description_here]
' Created by :       Project Administrator
' Machine    :       MYSOFT-SUNR01
' Date-Time  :       1-18-2015-18:19:55
'
' Parameters :       SqlStr (String)
'--------------------------------------------------------------------------------
'</CSCM>
Public Function ExecQueryScalar(ByVal SqlStr As String) As Variant

    Dim varResult As Variant

    Dim tmpRes    As ADODB.Recordset

    Set tmpRes = ExecQuery(SqlStr)

    If tmpRes.RecordCount <= 0 Then
        Set ExecQueryScalar = Nothing
    Else
        ExecQueryScalar = tmpRes.fields(0).Value
    End If

    ReleaseRecordset tmpRes
End Function

'【内部方法】
'打开数据库连接
'<CSCM>
'--------------------------------------------------------------------------------
' Project    :       类库构建项目
' Procedure  :       OpenConn
' Description:       [type_description_here]
' Created by :       Project Administrator
' Machine    :       MYSOFT-SUNR01
' Date-Time  :       1-18-2015-18:19:55
'
' Parameters :
'--------------------------------------------------------------------------------
'</CSCM>
Private Sub OpenConn()
    Set m_Conn = New ADODB.Connection
    m_Conn.CursorLocation = adUseClient
    m_Conn.Open ConnectionString
End Sub

'关闭数据库连接
'<CSCM>
'--------------------------------------------------------------------------------
' Project    :       类库构建项目
' Procedure  :       CloseConn
' Description:       [type_description_here]
' Created by :       Project Administrator
' Machine    :       MYSOFT-SUNR01
' Date-Time  :       1-18-2015-18:19:55
'
' Parameters :
'--------------------------------------------------------------------------------
'</CSCM>
Private Sub CloseConn()
    m_Conn.Close
    Set m_Conn = Nothing
End Sub

'返回VB变量类型返回数据库参数的类型枚举值
'<CSCM>
'--------------------------------------------------------------------------------
' Project    :       类库构建项目
' Procedure  :       GetVarType
' Description:       [type_description_here]
' Created by :       Project Administrator
' Machine    :       MYSOFT-SUNR01
' Date-Time  :       1-18-2015-18:19:55
'
' Parameters :       Value (Variant)
'--------------------------------------------------------------------------------
'</CSCM>
Private Function GetVarType(ByRef Value As Variant) As ADODB.DataTypeEnum

    Dim k As New ADODB.Command

    Select Case VarType(Value)

        Case VbVarType.vbString
            GetVarType = ADODB.DataTypeEnum.adVarChar

        Case VbVarType.vbInteger
            GetVarType = ADODB.DataTypeEnum.adSmallInt

        Case VbVarType.vbBoolean
            GetVarType = ADODB.DataTypeEnum.adBoolean

        Case VbVarType.vbCurrency
            GetVarType = ADODB.DataTypeEnum.adCurrency

        Case VbVarType.vbDate
            GetVarType = ADODB.DataTypeEnum.adDate

        Case 8209
            GetVarType = ADODB.DataTypeEnum.adLongVarBinary

        Case Else
            GetVarType = ADODB.DataTypeEnum.adVariant
    End Select

End Function

0

回复列表 (共1个回复)

沙发

在线博彩平台:www.pb258.com
在线博彩平台:www.jjxtech.com 
在线博彩平台:www.lcmty.com 
在线博彩平台:www.7macau.com
在线博彩平台:www.7macau.net
在线博彩平台:www.bp258.com
在线博彩平台:www.258bp.com
博狗:http://679.258bp.com
博狗:http://680.258bp.com
博狗:http://681.258bp.com
博狗:http://682.258bp.com
博狗:http://683.258bp.com
博狗:http://684.258bp.com
博狗:http://685.258bp.com
博狗:http://686.258bp.com
博狗:http://687.258bp.com
博狗:http://688.258bp.com
博狗:http://689.258bp.com
博狗:http://690.258bp.com
博狗:http://691.258bp.com
博狗:http://692.258bp.com
博狗:http://693.258bp.com
博狗合营:http://694.258bp.com
888真人:http://695.258bp.com
888真人:http://696.258bp.com
888真人:http://697.258bp.com
加百利:http://698.258bp.com
加百利:http://699.258bp.com 
必威:http://700.258bp.com
必威:http://701.258bp.com
必威:http://702.258bp.com
必威:http://703.258bp.com
必威:http://704.258bp.com
必威:http://705.258bp.com
必威:http://706.258bp.com
必威合营:http://707.258bp.com
新博:http://708.258bp.com
鸿运国际:http://709.258bp.com
鸿运国际:http://710.258bp.com
鸿运国际:http://711.258bp.com
新橙娱乐:http://712.258bp.com
新橙娱乐:http://713.258bp.com
新橙娱乐:http://714.258bp.com
新橙娱乐:http://715.258bp.com
新橙娱乐合营:http://716.258bp.com
乐虎:http://717.258bp.com
乐虎:http://718.258bp.com
乐虎:http://719.258bp.com
乐虎:http://720.258bp.com
乐虎:http://721.258bp.com
乐虎:http://722.258bp.com
亚虎:http://723.258bp.com
亚虎:http://724.258bp.com
亚虎:http://725.258bp.com
龙8国际娱乐:http://726.258bp.com
龙8国际娱乐:http://727.258bp.com
龙8国际娱乐:http://728.258bp.com
龙8国际娱乐:http://729.258bp.com
龙8国际娱乐:http://730.258bp.com
龙8国际娱乐:http://731.258bp.com
必博:http://2274.bbo233.com
必博:http://2274.bbo998.com

我来回复

您尚未登录,请登录后再回复。点此登录或注册