怎样用vb向sql-server 的数据库中插入图片
请赐教

解决方案 »

  1.   

    注:写图片文件到数据库 
        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则写文件失败。 
        还有用此纯代码读取图片时,在代码调试中不会出现错误,在运行时如果快速浏览带有图片的记录时,会出现莫名其妙的程序死机.如上述代码,我都用了错误控制技术,但还是出现。 
        我用的是VB6.0 ADO方法 + SQL SERVER
      

  2.   

    微软的答案:http://support.microsoft.com/default.aspx?scid=http://support.microsoft.com:80/support/kb/articles/Q258/0/38.asp&NoWebContent=1
      

  3.   

    http://vbboshi.myrice.com/vbtech/database/page_2/file55.htm