1.向SQL Server储存图片
创建一个测试表:
CREATE TABLE [dbo].[TABLE1] (
[Image_ID] [int] IDENTITY (1, 1) NOT NULL ,
[Image_File] [image] NULL 
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO2.储存图片
Private Sub Form_Load()
    Dim oStream As New ADODB.Stream
    Dim binFile As Variant
    oStream.Mode = adModeReadWrite
    oStream.Type = adTypeBinary
    oStream.Open
    oStream.LoadFromFile "D:\sample.jpg"
    binFile = oStream.Read(-1)
    oStream.Close
    Set oStream = Nothing
    
    Dim cn As New ADODB.Connection
    Dim rs As New ADODB.Recordset
    
    cn.Open "dsn=test;uid=sa"
    rs.Open "select Image_File from table1 where 1<>1", cn, 3, 3
    
    rs.AddNew
    rs.Fields("Image_File").AppendChunk binFile
    rs.Update
    rs.Close
    Set rs = Nothing
    cn.Close
    Set cn = Nothing
    
End Sub3.从读取SQL Server读取图片:
添加一个PictureBox和CommandButton
Private Sub Command1_Click()
    Dim cn As New ADODB.Connection
    Dim rs As New ADODB.Recordset
    
    cn.Open "dsn=test;uid=sa"
    rs.Open "select Image_File from table1 where Image_ID=1", cn, 3, 3
    Set Me.Picture1.DataSource = rs
    Me.Picture1.DataField = "Image_File"
    rs.Close
    Set rs = Nothing
    cn.Close
    Set cn = Nothing
End Sub
测试环境:VB6.0+SQL Server7.0

解决方案 »

  1.   

    please refer....Public Sub ShowImage(pDb As Object, pFace As Object, pImage As Object, pChunkSize As Long, pSql As String)
     On Error GoTo Errhandler
        Dim lngOffset As Long
        Dim lngLogoSize As Long
        Dim varLogo As Variant
        Dim varChunk As Variant
        Dim i As Long
        
        Dim Fragment As Integer, Chunk() As Byte
        Dim Chunks As Integer
        Dim FileName As String
        Dim DataFile As Integer
        
        Dim rsTmp As ADODB.Recordset
        
        Set rsTmp = New ADODB.Recordset
          
        Set rsTmp = pDb.DoSQLQuery(pSql)
        If Not rsTmp.EOF Then
             lngLogoSize = rsTmp.Fields("photo").ActualSize                '相片大小
             If lngLogoSize = 0 Then
                pImage.Picture = LoadPicture("")
                Exit Sub
             End If
             DataFile = 1
             FileName = App.Path & "\temp.bmp"
             gPhotoPath = FileName
             Open FileName For Binary Access Write As DataFile
            
             Chunks = lngLogoSize \ pChunkSize
             Fragment = lngLogoSize Mod pChunkSize
             ReDim Chunk(Fragment)
             Chunk() = rsTmp!photo.GetChunk(Fragment)
             Put DataFile, , Chunk()
    '         For i = 1 To Chunks
    '           ReDim Buffer(pChunkSize)
    '           Chunk() = rsTmp!photo.GetChunk(pChunkSize)
    '           Put DataFile, , Chunk()
    '         Next i
            
             Close DataFile
             pImage.Picture = LoadPicture(FileName)
          Else
             pImage.Picture = LoadPicture("")
          End If
         
         Set rsTmp = Nothing
         Exit Sub
    Errhandler:
        Set rsTmp = Nothing
         HandleError pFace
    End Sub
    Public Sub SaveImage(pServer As String, pSessionId As String, pFace As Object, pImage As Object, pSql As String, pChunkSize As Long, Optional pFlag As String = "", Optional pPath As String = "", Optional pValue As String = "")
     On Error GoTo Errhandler
        Dim TmpPhoto As Object 'hrms.clsPhoto
        Dim lngLogoSize As Long
        Dim Fragment As Integer, Chunk() As Byte
        Dim Chunks As Integer
        
        Dim msg As String
        
        Dim i As Long
        Dim isok As Boolean    Dim FileName As String
        Dim DataFile As Integer
             
        FileName = pPath    DataFile = 1
        Open FileName For Binary Access Read As DataFile
        lngLogoSize = LOF(DataFile)
        If lngLogoSize = 0 Then Close DataFile: Exit Sub
         
         Chunks = lngLogoSize \ pChunkSize
         Fragment = lngLogoSize Mod pChunkSize
         If Chunks > 0 Then
          '  MsgBox pFace.res.GetString(1070)
            pFace.MsgInfoById (1070)
            Close DataFile
            Exit Sub
         End If
    '   rsTmp!photo.AppendChunk Null
        ReDim Chunk(Fragment)
        Get DataFile, , Chunk()
    '    rsTmp!photo.AppendChunk Chunk()
    '    ReDim Chunk(pChunkSize)
     '   For i = 1 To Chunks
     '     Get DataFile, , Chunk()
     '     rsTmp!photo.AppendChunk Chunk()
     '   Next i
     
        Set TmpPhoto = CreateObject("hrms.clsPhoto")
        TmpPhoto.ConnectDB pServer, pSessionId, GetMacAddress
        TmpPhoto.UpdateImage pSql, Chunk, pFlag, pValue
    '       If pFlag = "0" Then
    '          pImage.Picture = LoadPicture("")
    '       End If
        Close DataFile    Set TmpPhoto = Nothing
        Exit Sub
    Errhandler:
          Set TmpPhoto = Nothing
          HandleError pFace
    End Sub
      

  2.   

    suker2000(飘) ,我怎样调用这两个函数呢?
      

  3.   

    hydnoahark(诺亚方舟) Dim oStream As New ADODB.Stream出错
        
      

  4.   

    http://www.microsoft.com/data/download.htm
      

  5.   

    我也安装了mdac_typ.exe,但是在vb里面怎么引用呢?
      

  6.   

    在Project的References...中选取:Microsoft ActiceX Data Objects 2.5(或以上版本) Library即可