在VB中怎样把图形文件内容写入SQL 数据库的表中?
在VB中又怎样读出来呢?

解决方案 »

  1.   

    Option ExplicitDim cn As ADODB.Connection
    Dim rs As ADODB.Recordset
    Dim mst As ADODB.Stream
    Private Sub Command1_Click()
        Dim str As String
        str = App.Path
        Set cn = New ADODB.Connection
        cn.Open "provider=microsoft.jet.oledb.4.0;data source=" + App.Path + "\db1.mdb;"
        Set rs = New ADODB.Recordset
        rs.Open "select * from sample", cn, adOpenStatic, adLockOptimistic
        Set mst = New ADODB.Stream
        mst.Type = adTypeBinary
        mst.Open
        Do While Not rs.EOF
           rs.Delete
           rs.MoveNext
        Loop
        mst.LoadFromFile App.Path + "\" + Text1.Text + ".jpg"
        rs.AddNew
        rs.Fields("Photo").Value = mst.Read
        rs.Update
        rs.Close
        cn.Close
    End SubPrivate Sub Command2_Click()
        Set cn = New ADODB.Connection
        cn.Open "provider=microsoft.jet.oledb.4.0;data source=" + App.Path + "\db1.mdb;"
        Set rs = New ADODB.Recordset
        rs.Open "select * from sample", cn, adOpenKeyset, adLockOptimistic
        Set mst = New ADODB.Stream
        mst.Type = adTypeBinary
        mst.Open
        mst.Write rs.Fields("Photo").Value
        mst.SaveToFile App.Path + "\photo.jpg", adSaveCreateOverWrite
        Image1.Picture = LoadPicture(App.Path + "\photo.jpg")
        Kill App.Path + "\photo.jpg"
        rs.Close
        cn.Close
    End Sub
      

  2.   

    楼上的代码是连接的access数据库!首先声明!此乃转贴!不过我已经调试通过了 !下面是过程定义!'Use  ADODB.Stream  Method  
    'After  ADO  2.6  
    'Import  the  Image  to  SQLServer  
    Private  Sub  ImportBLOB(cn  As  ADODB.Connection)  
             
           Dim  rs  As  New  ADODB.Recordset  
           Dim  stm  As  ADODB.Stream  
             
           Set  stm  =  New  ADODB.Stream  
             
           '  Skip  any  table  not  found  errors  
           On  Error  Resume  Next  
           cn.Execute  "drop  table  BinaryObject"  
             
           On  Error  GoTo  0  
           'Create  the  BinaryObject  table  
           cn.Execute  "create  table  BinaryObject  "  &  _  
                                     "(blob_id  int  IDENTITY(1,1),  "  &  _  
                                       "blob_filename  varchar(256),  "  &  _  
                                       "blob_object  image)"  
                                     
           rs.Open  "Select  *  from  BinaryObject  where  1=2",  cn,  adOpenKeyset,  adLockOptimistic  
           'Read  the  binary  files  from  disk  
           stm.Type  =  adTypeBinary  
           stm.Open  
           stm.LoadFromFile  App.Path  &  "\BLOBsample.jpg"  
             
           rs.AddNew  
           rs!blob_filename  =  App.Path  &  "\BLOBsample.jpg"  
           rs!blob_object  =  stm.Read  
             
           'Insert  the  binary  object  in  the  table  
           rs.Update  
             
           rs.Close  
           stm.Close  
             
           Set  rs  =  Nothing  
           Set  stm  =  Nothing  
             
    End  Sub  
    'Display  the  image  on  image  control  
    Private  Sub  DisplayBLOB(cn  As  ADODB.Connection)  
     
           Dim  rs  As  New  ADODB.Recordset  
             
           '  Select  the  only  image  in  the  table  
           rs.Open  "Select  *  from  BinaryObject  where  blob_id  =  1",  cn  
             
           '  Set  the  DataSource  to  the  recordset  
           Set  imgBinaryData.DataSource  =  rs  
           'Set  the  DataField  to  the  BLOB  field  
           imgBinaryData.DataField  =  rs!blob_object.Name  
             
           'Release  the  recordset  
           rs.Close  
           Set  rs  =  Nothing  
     
    End  Sub  
      

  3.   

    up~解决方法有很多种,只要解决就可以了~呵呵~反正基本上是用ADO2.6