当前位置:编程学习 > VB >>

VB连接access数据库

谁帮我详细解释一下怎么连接,我都看了半天都晕了,如果能解释到我懂,我愿意加到1000争
补充:假如有两个TEXT,一个text1为输入用户名, 一个为text2为输入密码,还有一个command1为登陆,我在D盘有一个用户名的数据库,当我点登陆的时候就连接到数据库,查看用户名和密码是否正确,帮我写下代码吧,并且解释清楚每一步的作用,如果可以留下QQ,
答案:其实很简单,建议不要用ADODC控件,给你一个自己整理的模块

首先引用Microsoft ActiveX Data Objects 2.0 Library,再定义两个

把下面的代码拷贝到模块里

Option Explicit

Public conn As New ADODB.Connection
Public rs   As New ADODB.Recordset

Enum RSParameter
    None_Para = 0     '不排序
    AscOnly = 1      '由小往大的顺序列出
    DescOnly = 2     '大往小的顺序列出
    AscSearch = 3
    DescSearch = 4
    SearchOnly = 5
End Enum
   

Public Function rs_Open(lyFile As String, ByVal lyConn As ADODB.Connection, ByVal lyRs As ADODB.Recordset, _
                       lyPwd As String, lyTable As String, lyFileds As String, _
                       ByVal lyCompositor As RSParameter, _
                       Optional lySearch As String, Optional lyComFiled As String) As Boolean
   
    On Error GoTo errJump
   
    If Dir(lyFile, vbArchive) = "" Then
        MsgBox "错误的文件路径" & vbCrLf & lyFile, vbInformation + vbOKOnly, "警告"
        rs_Open = False
        Exit Function
    End If
   
    If lyConn.State = adStateOpen Then lyConn.Close
    lyConn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & lyFile & _
                              ";Jet OLEDB:Database Password=" & lyPwd & ";persist security info=false"
    lyConn.Open

    If lyRs.State = adStateOpen Then lyRs.Close
    lyRs.CursorLocation = adUseClient

    Select Case lyCompositor
            Case 0
                lyRs.Open "select " & lyFileds & " from " & lyTable, lyConn, adOpenKeyset, adLockOptimistic
            Case 1
                lyRs.Open "select " & lyFileds & " from " & lyTable & " order by " & lyComFiled & " Asc", _
                          lyConn, adOpenKeyset, adLockOptimistic
            Case 2
                lyRs.Open "select " & lyFileds & " from " & lyTable & " order by " & lyComFiled & " Desc", _
                          lyConn, adOpenKeyset, adLockOptimistic
            Case 3
                lyRs.Open "select " & lyFileds & " from " & lyTable & " where " & lySearch & " order by " & lyComFiled & _
                          " Asc", lyConn, adOpenDynamic, adLockOptimistic
            Case 4
                lyRs.Open "select " & lyFileds & " from " & lyTable & " where " & lySearch & " order by " & lyComFiled & _
                          " Desc", lyConn, adOpenDynamic, adLockOptimistic
            Case 5
                lyRs.Open "select " & lyFileds & " from " & lyTable & " where " & lySearch, lyConn, _
                          adOpenDynamic, adLockOptimistic
    End Select

    rs_Open = True

errJump:
    If err.Number <> 0 Then
        rs_Open = False
       
        'Err.Raise Err.Number, "rs_Open", Err.Description
        err.Clear
    End If
End Function

Public Function rs_Close(ByVal lyConn As ADODB.Connection, ByVal lyRs As ADODB.Recordset)

    If lyRs.State = adStateOpen Then lyRs.Close
    If lyConn.State = adStateOpen Then lyConn.Close
   
    Set lyRs = Nothing
    Set lyConn = Nothing
   
End Function

'"alter table [db] drop column [date11]"
'"alter table [db] add column [date11] text(10) not null"
'"DROP TABLE [aaa]"
Public Function Conn_Execute(ByVal lyDataPath As String, ByVal lyPwd As String, _
                            ByVal lyCommand As String) As Boolean

    On Error GoTo errJump
    Dim eConn As New ADODB.Connection
    If eConn.State = adStateOpen Then eConn.Close
    eConn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & lyDataPath & _
    ";Jet OLEDB:Database Password=" & lyPwd & ";persist security info=false"
    eConn.Open
   
    eConn.Execute lyCommand
   
    eConn.Close
    Set eConn = Nothing
    Conn_Execute = True
    Exit Function
   
errJump:
    Conn_Execute = False
    err.Raise err.Number, "Conn_Execute", err.Description
   
End Function

Public Function TableExists(ByVal lyPath As String, ByVal lyPwd As String, ByVal lyTableName As String) As Boolean
   
    Dim bConn   As New ADODB.Connection
    Dim bRs     As New ADODB.Recordset
   
    On Error GoTo errJump
    If bConn.State = adStateOpen Then bConn.Close
    bConn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & lyPath & _
    ";Jet OLEDB:Database Password=" & lyPwd & ";persist security info=false"
    bConn.Open
    Set bRs

上一个:vb与sql问题
下一个:请问VB乐透程序

CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,