VB 数据库操作函数(my_db_sql.cls)

//my_db_sql.cls
//查询数据
Public Function F_select(sqloledb_string As Variant, select_from As Variant, select_id As Variant, select_field As Variant, String_id As Variant) As String
    F_select = ""
    If select_id <> "" And select_field <> "" Then
        Dim db As Connection
        Set db = New Connection
        db.CursorLocation = adUseClient
        db.Open sqloledb_string
        Set rs = New Recordset
        select_string = "select " & select_field & " from " & select_from & " where " & select_id & " =‘" & String_id & "‘"
        rs.Open select_string, db, adOpenStatic, adLockOptimistic
        mm = Val(rs.RecordCount)
        If mm <> "" Then
            String2 = ""
            For t = 0 To Val(rs.RecordCount) - 1
                String2 = Trim(rs.Fields(select_field).Value) & "|" & String2
                rs.MoveNext
            Next t
            If Val(rs.RecordCount) > 0 Then
                F_select = String2
               Else
                F_select = "0"
            End If
        End If
        rs.Close
    End If
End Function
//写入数据
Public Function F_update(sqloledb_string As Variant, select_from As Variant, select_id As Variant, update_field As Variant, String_id As Variant, String2 As Variant) As String
    F_update = ""
    If String_id <> "" And String2 <> "" Then
        Dim db As Connection
        Set db = New Connection
        db.CursorLocation = adUseClient
        db.Open sqloledb_string
        Set rs = New Recordset
        select_string = "select " & select_id & " from " & select_from & " where " & select_id & " =‘" & String_id & "‘"
        rs.Open select_string, db, adOpenStatic, adLockOptimistic
        Dim mm
        mm = Val(rs.RecordCount)
        If mm <> "" Then
            If mm > 0 Then
               update_string = "update " & select_from & " set " & update_field & "= ‘" & String2 & "‘  where " & select_id & "=‘" & String_id & "‘ "
    //           MsgBox (update_string)
               db.Execute update_string
                F_update = "1"
            Else
                F_update = "0"
            End If
        End If
        rs.Close
    End If
End Function

//插入数据
Public Function F_insert(sqloledb_string As Variant, select_from As Variant, insert_field As Variant, String1 As Variant) As String
    F_insert = ""
    If insert_field <> "" And String1 <> "" Then
        Dim db As Connection
        Set db = New Connection
        db.CursorLocation = adUseClient
        db.Open sqloledb_string
        Set rs = New Recordset
        select_string = "select * from " & select_from & " where " & insert_field & " =‘" & String1 & "‘"
        rs.Open select_string, db, adOpenStatic, adLockOptimistic
        Dim mm
        mm = Val(rs.RecordCount)
        If mm <> "" Then
            If mm = 0 Then
               insert_string = "insert  " & select_from & " ( " & insert_field & " )  select ‘" & String1 & "‘"
               db.Execute insert_string
                F_insert = "1"
               Else
                F_insert = "0"
            End If
        End If
        rs.Close
    End If
End Function
//查询字段
Public Function F_get_field(sqloledb_string As Variant, select_from As Variant, select_id As Variant, select_field As Variant, String_id As Variant) As String
    F_get_field = ""
    If select_id <> "" And select_field <> "" Then
        On Error Resume Next
         Dim select_string As String, st, mm
        select_string = "select  " & select_field & "  from  " & select_from & "  where " & select_id & " = ‘" & String_id & "‘"
        Dim db As Connection
        Set db = New Connection
        db.CursorLocation = adUseClient
        db.Open sqloledb_string
        Set rs = New Recordset
        rs.Open select_string, db, adOpenStatic, adLockOptimistic
        mm = Val(rs.RecordCount)
        If mm <> "" Then
                st = Trim(rs.Fields(select_field).Value)
            If Val(rs.RecordCount) > 0 Then
                If st <> "" Then
                    F_get_field = st
                Else
                    F_get_field = "0"
                End If
            End If
        End If
//        MsgBox "返回字段:" & F_get_field
        rs.Close
    End If
End Function

Public Function F_pict_savedb(sqloledb_string As Variant, select_from As Variant, fields_1 As Variant, fields_2 As Variant, fields_3 As Variant, strfilename As Variant, macname As Variant, txtName As Variant) As String
    On Error Resume Next
    F_pict_savedb = ""
    If strfilename <> "" And txtName = "" Then
        F_pict_savedb = ""
    Else
        Set cn = New ADODB.Connection
        Set rs = New ADODB.Recordset
        Set stm = New ADODB.Stream
        select_string = "select * from " & select_from
        cn.ConnectionString = sqloledb_string
        cn.Open
        stm.type = adTypeBinary  
                //类型为一二进制数组
        stm.Open
        stm.LoadFromFile strfilename          
                //图片路径
        rs.Open select_string, cn, adOpenKeyset, adLockOptimistic
        rs.AddNew
        rs.Fields(fields_1).Value = macname  
                //pc_mac 存的文件名
        rs.Fields(fields_2).Value = txtName  
                //pc_mac 存的文件名
        rs.Fields(fields_3).Value = stm.Read 
                //图片文件
        rs.Update
        rs.Close
        stm.Close
         F_pict_savedb = "1"
    End If
End Function

VB 数据库操作函数(my_db_sql.cls)

上一篇:mysql数据的导入与导出


下一篇:C#操作Sql数据库之对数据库进行增删改查