我的数据库中有一字段为照片,我想将照片存在库中,请问如何存?存为什么类型?在窗体中通过imag控件或pictrue控件和adodata控件如何显示???

解决方案 »

  1.   

    '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 = NothingEnd Sub
      

  2.   

    很多朋友不知道怎么把图片加入到数据库里面,可以看看下面的代码Private Sub Command3_Click()
    Dim conn As New ADODB.Connection
    conn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\1.mdb;Persist Security Info=False"
    conn.Execute "create table a (b longbinary)"
    End SubPrivate Sub Command4_Click()
        Set b = New ADODB.Recordset
        Set c = New ADODB.Stream
            
            
            c.Mode = adModeReadWrite    c.Type = adTypeBinary
        c.Open
        c.LoadFromFile "c:\1.bmp"
        
        b.Open "select * from a", "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\1.mdb;Persist Security Info=False", adOpenDynamic, adLockOptimistic
        b.AddNew
        
        b.Fields.Item(0).Value = c.Read()
        
        
        b.Update
        
        b.Close
        Set b = New ADODB.Recordset
        b.Open "select * from a", "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\1.mdb;Persist Security Info=False", adOpenKeyset, adLockOptimistic
        MsgBox b.RecordCount
        
        b.MoveLast
        
        c.Write (b.Fields.Item(0).Value)
        
        c.SaveToFile "c:\aa.bmp", adSaveCreateOverWrite
        
        Picture1.Picture = LoadPicture("c:\aa.bmp")
    End Sub
      

  3.   

    http://expert.csdn.net/Expert/FAQ/FAQ_Index.asp?id=19363
      

  4.   

    最简单的方法就是在数据库中只保存图片的路径和图片的名字
    到时直接用LoadPicture来加载或直接给有picture属性的控件付值
      

  5.   

    请问在vb6中怎么做?????,图片保存在access 2000数据库中,而不是sql server
      

  6.   

    我的数据库中(access 2000数据库)有一字段为照片,我想将照片存在库中,请问如何存?存为什么类型?在窗体中通过imag控件或pictrue控件和adodata控件如何显示???我想通过上一张,下一张浏览让图片跟着更新。
      

  7.   

    Dim mstream As ADODB.Stream
    CDlg.FileName = ""
    CDlg.ShowOpen
    FileName = CDlg.FileName
    If FileName = "" Then
       FilePath = ""
       Exit Sub
    End If
    If UCase(Right(FileName, 3)) = "JPG" Or UCase(Right(FileName, 3)) = "BMP" Then
       FilePath = App.Path & "\tmpimage.bmp"
       Set mstream = New ADODB.Stream
       mstream.Type = adTypeBinary
       mstream.Open
       mstream.LoadFromFile FileName
    Else
       If MsgBox("选择图片格式不对,请重新选择!", vbOKCancel) = vbOK Then
          FilePath = ""
          Call Picture1_DblClick
          Exit Sub
       Else
          FilePath = ""
          Exit Sub
       End If
       Set mstream = New ADODB.Stream
       mstream.Type = adTypeBinary
       mstream.Open
       mstream.LoadFromFile FileName
    End If
    If vEdit = eEditMode.Add Then
       m_ValueDict("zp") = mstream.Read
    Else
       m_rs.Fields("zp").Value = mstream.Read
    End If
    用我的,imag控件或pictrue控件都可以.
      

  8.   

    看错了,不好意思!我以为是SQL SERVER 2000。