情况是这样的:我做的一程序功能要求,数据库都是Access,要求将数据库A中的数据结构和内容都导入到数据库B中。现在有些表中的字段类型为OLE字段,里面存放的是长二进制。请问通过DAO怎么读取数据库中的二进制和写入数据库。

解决方案 »

  1.   

    http://www.chenoe.com/blog/blogview.asp?logID=1
      

  2.   

    http://community.csdn.net/Expert/topic/4276/4276477.xml?temp=.1302606那就用二进制方式来读写文件,请参考(把文件(包括图片)存入数据库):Dim cn As New ADODB.Connection,rs As New ADODB.Recordset
    Private Sub Form_Load()
        cn.CursorLocation = adUseClient
        '这里给的是sql库的连接,具体的数据库连接请自己改过来,如果是Access库,只改数据库的连接即可
        cn.Open "Provider=SQLOLEDB.1;Password=;Persist Security Info=False;User ID=sa;Initial Catalog=Test;Data Source=ljx"
    End SubPrivate Sub cmdSaveFile_Click()
        saveFile App.Path & "\temp.jpg"
    End SubPrivate Sub cmdReadFile_Click()
        readFile App.Path & "\temp1.jpg"
    End Sub'保存文件到数据库
    Private Sub saveFile(Byval strFile As String)
        Dim tmp() As Byte
        Dim lngFile As Long
        If rs.State=adStateOpen Then rs.Close
        rs.Open "select * from test where 1<>1", cn, adOpenDynamic, adLockOptimistic
        
        lngFile = FreeFile
        Open strFile For Binary As #lngFile
        ReDim tmp(LOF(lngFile))
        Get #lngFile, , tmp
        Close #lngFile
        rs.AddNew
        rs.Fields("ID").Value="001"
        rs.Fields("pic").Value = tmp
        rs.Update
    End Sub'读取数据库的文件,保存到硬盘
    Private Sub readFile(Byval strFile As String)
        Dim tmp() As Byte
        If rs.State=adStateOpen Then rs.Close
        rs.Open "select * from test where [ID]='001'", cn
        ReDim tmp(rs.Fields(0).ActualSize)                     '返回2进制文件的字节长度
        tmp = rs.Fields("pic").Value
        Open strFile For Binary As #1
        Put #1, , tmp
        Close #1
    End SubPrivate Sub Form_Unload(Cancel As Integer)
        If rs.State<>adStateClosed Then rs.Close
        If cn.State<>adStateClosed Then cn.Close
        Set rs = Nothing
        Set cn=Nothing
    End Sub
      

  3.   

    是不是只能用ADO,而DAO不能实现
      

  4.   

    不是 , DAO 的我以前见过,我找找 代码