名位达人.如何用VB+SQL保存,读取数据库中的图片,希望给个完整的代码......

解决方案 »

  1.   

    我这里有一个ACCESS的例子,你参考参考吧
    Option Explicit
    Dim b As ADODB.Recordset
    Dim c As ADODB.Stream
    Dim conn As New ADODB.ConnectionPrivate Sub Command1_Click()    conn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\1.mdb;Persist Security Info=False"
        conn.Execute "create table a (Str longbinary)"
    End SubPrivate Sub Command2_Click()
        Set b = New ADODB.Recordset
        Set c = New ADODB.Stream
        c.Mode = adModeReadWrite
        c.Type = adTypeBinary
        c.Open
        c.LoadFromFile "D:\ss.JPG"
        b.Open "select * from a", "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\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=D:1.mdb;Persist Security Info=False", adOpenKeyset, adLockOptimistic
        MsgBox b.RecordCount
        b.MoveLast
        c.Write (b.Fields.Item(0).Value)
        c.SaveToFile "d:\aa.JPG", adSaveCreateOverWrite
        Picture1.Picture = LoadPicture("D:\aa.JPG")
    End Sub
      

  2.   

    转贴(摘自www.china-askpro.com):
       注:写图片文件到数据库 
        Col为栏位名,ImgFile为要写到数据库的图片文件名,BockSize为每次写多少字节,缺省为每次写8K字节到数据库 
        Public Sub WriteDB(Col As ADODB.Field, ImgFile As String, Optional BlockSize As Long=8192) 
         Dim byteData() As Byte, FileLength As Long, NumBlocks As Integer 
         Dim LeftOver As Long, SourceFileNum As Integer, i As Integer 
         
         SourceFileNum = FreeFile 
         Open ImgFile For Binary As SourceFileNum 
         FileLength = LOF(SourceFileNum) 
         If FileLength > 50 Then 
         NumBlocks = FileLength \ BlockSize 
         LeftOver = FileLength Mod BlockSize 
         
         ReDim byteData(LeftOver) 
         Get SourceFileNum, , byteData() 
         Col.AppendChunk byteData() 
         ReDim byteData(BlockSize) 
         For i = 1 To NumBlocks 
         Get SourceFileNum, , byteData() 
         Col.AppendChunk byteData() 
         Next 
         End If 
         Close SourceFileNum 
        End Sub 
         
         
        ImgFile为从数据库读出数据写到磁盘的文件名,BlockSize为每次向文件写多少个字节,缺省为8K字节,当ReadDB=True,得到图片文件後,可以用LoadPicter(图片文件名)显示图片到PictureBox或Image框中. 
        Public Function ReadDB(Col As ADODB.Field, ImgFile As String,Optional BlockSize As Long=8192) As Boolean 
         Dim byteData() As Byte, NumBlocks As Integer 
         Dim LeftOver As Long, DestFileNum As Integer, i As Integer 
         Dim ColSize As Long 
         
         On Error GoTo ErrRead 
         ReadDB = False 
         
         'If Dir(ImgFile) <> "" Then Kill ImgFile 
         
         DestFileNum = FreeFile 
         Open ImgFile For Binary As #DestFileNum 
         
         ColSize = Col.ActualSize 
         NumBlocks = ColSize \ BlockSize 
         LeftOver = ColSize Mod BlockSize 
         
         ReDim byteData(LeftOver) 
         byteData() = Col.GetChunk(LeftOver) 
         Put DestFileNum, , byteData() 
         ReDim byteData(BlockSize) 
         For i = 1 To NumBlocks 
         byteData() = Col.GetChunk(BlockSize) 
         Put #DestFileNum, , byteData() 
         Next 
         If LOF(DestFileNum) > 200 Then ReadDB = True 
         Close #DestFileNum 
         Exit Function 
         
        ErrRead: 
         MsgBox "READ PICTURE ERR:" & Err.Number 
         ReadDB = False 
         Exit Function 
        End Function//如果ReadDB=False则写文件失败。
      

  3.   

    http://community.csdn.net/Expert/TopicView3.asp?id=4767614
      

  4.   

    使用流对象保存和显示图片 
    打开vb6,新建工程。添加两个按钮,一个image控件
    注意:Access中的photo字段类型为OLE对象.
    SqlServer中的photo字段类型为Image'** 引用 Microsoft ActiveX Data Objects 2.5 Library 及以上版本
    ‘2.5版本以下不支持Stream对象
    Dim iConcstr As String
    Dim iConc As ADODB.Connection
     '保存文件到数据库中
    Sub s_SaveFile()
        Dim iStm As ADODB.Stream
        Dim iRe As ADODB.Recordset
        Dim iConcstr As String    '读取文件到内容
        Set iStm = New ADODB.Stream
        With iStm
            .Type = adTypeBinary   '二进制模式
            .Open
            .LoadFromFile App.Path + "\test.jpg"
        End With
           '打开保存文件的表
        Set iRe = New ADODB.Recordset
        With iRe
            .Open "select * from img", iConc, 1, 3
            .AddNew         '新增一条记录
            .Fields("photo") = iStm.Read
            .Update
        End With
          '完成后关闭对象
        iRe.Close
        iStm.Close
    End Sub
    Sub s_ReadFile()
        Dim iStm As ADODB.Stream
        Dim iRe As ADODB.Recordset
        '打开表
    Set iRe = New ADODB.Recordset
    ‘得到最新添加的纪录
        iRe.Open "select top 1 * from img order by id desc", iConc, adOpenKeyset, adLockReadOnly
        '保存到文件
        Set iStm = New ADODB.Stream
        With iStm
            .Mode = adModeReadWrite
            .Type = adTypeBinary
            .Open
            .Write iRe("photo")
    ‘这里注意了,如果当前目录下存在test1.jpg,会报一个文件写入失败的错误.
            .SaveToFile App.Path & "\test1.jpg"
        End With
           Image1.Picture = LoadPicture(App.Path & "\test1.jpg")
       '关闭对象
        iRe.Close
        iStm.Close
    End Sub
     Private Sub Command1_Click()
    Call s_ReadFile
    End Sub
    Private Sub Command2_Click()
    Call s_SaveFile
    End Sub
    Private Sub Form_Load()
        '数据库连接字符串
        iConcstr = "Provider=Microsoft.Jet.OLEDB.4.0;Persist Security Info=False" & _
            ";Data Source=F:\csdn_vb\database\保存图片\access图片\img.mdb"‘下面的语句是连接sqlserver数据库的.
        ‘iConcstr = "Provider=SQLOLEDB.1;Persist Security Info=True;" & _
    ‘ "User ID=sa;Password=;Initial Catalog=test;Data Source=yang"
        Set iConc = New ADODB.Connection
       iConc.Open iConcstr
    End Sub
     Private Sub Form_Unload(Cancel As Integer)
    iConc.Close
    Set iConc = Nothing
    End Sub
      

  5.   

    我的网站上有源码,你可以下载看看。VB资料->查询“向数据库存取图片”;╭═══════════════════╮
    ║ 免费的源码、工具网站,欢迎大家访问!║
    ║ http://www.j2soft.cn/        ║
    ╰═══════════════════╯
      

  6.   

    '追加图片到数据库
    Public Function savebmptodb(ByVal strdh As String, ByVal ypbh As Long, ByVal sfilename As String) As Boolean
    On Error GoTo err
        savebmptodb = False
        Dim strStream As New ADODB.Stream
        Dim rs As New ADODB.Recordset
        strStream.Type = adTypeBinary
        strStream.Open
        strStream.LoadFromFile sfilename
        Dim strsql As String
        strsql = "select * from tb_images where strdh='" & strdh & "' and ypbh=" & ypbh
        rs.Open strsql, g_pubcnn, adOpenStatic, adLockOptimistic
        If rs.RecordCount <= 0 Then
            rs.AddNew
        End If
        
        rs.Fields("bmp").Value = strStream.Read
        rs.Fields("ypbh").Value = ypbh
        rs.Fields("strdh").Value = strdh
        rs.Fields("flname").Value = strdh & ypbh
        rs.update
        savebmptodb = True
        
        
    err:
        strStream.Close
        Set strStream = Nothing
       PubCloseRecord rs, True
    End Function