我是一个VB新手,现在想做一个VB连接数据库的程序,用DAO方法。 
比如我现在有一个fst.mdb数据库文件,里面有一个名为“biao”的表, 
在这个表你有“first”“second”“third”三个字段。 
现在我想知道的是怎么给这些字段添加值,读取值。读取到一个变量里面就行。

解决方案 »

  1.   

    不一定要用DAO了,其他方法也行,不过最好有详细一点的注释,谢谢啦!
      

  2.   

    我是用的ADO,首先引用 Microsoft ActiveX DataObjects 2.8Library
    和 Microsoft ActiveX DataObjects 2.8Library以下是代码:
        Dim Str As String
        Dim Con As New ADODB.Connection
        Dim Rst As New ADODB.Recordset
        
        Dim STR_first As String
        Dim STR_second As String
        Dim STR_third As String
        
        Str = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\lala\db2.mdb;Persist Security Info=False"
    '数据库连接字符串
        Con.Open Str ' 打开ACCESS数据库
        
        
        Str = "select first,second,third from biao"
        Rst.Open Str, Con ' 打开记录集
        
        Do Until Rst.EOF
            STR_first = Rst!First '将字段值读入变量中
            STR_second = Rst!Second
            STR_third = Rst!third
            Rst.MoveNext
        Loop
        
        Rst.Close '关闭记录集
        
        
        Rst.Open Str, Con, adOpenKeyset, adLockOptimistic '打开用于更新的记录集,注意adOpenKeyset, adLockOptimistic 两个参数。
        
        Rst.AddNew '向表中添加数据
            Rst!First = ""
            Rst!Second = ""
            Rst!third = """"
        Rst.Update
        
        Rst.Close '
        Con.Close '关闭数据库