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 7.0(都是英文版)C/S架构
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 7.0(都是英文版)C/S架构
解决方案 »
- 问个比较大小问题(100分啊)[
- 多媒体问题??
- CommandButton显示标题的字体颜色
- 提问高手 关于图像抖动 zyl910(910:分儿,我又来了!) 请进
- 请 问如何使窗体的最大化最小化按扭有效而关闭按钮处于无效状态?
- vb自定义函数问题求助!
- 在VB中请教容器的问题
- 如何取得本机的子网掩码?
- 怎么编手机与电脑之间的通信程序?用AT命令还是控件??
- 请问在VB中如何在SQL语句中查询ACCESS TABLE Borrow中定义日期/时间类型
- 我有一个把html文件中全部超级链接转成小或大写的程序!!那位想要?有原码!
- 请问Form1.Show(无模式)和Form1.Visible=True有和区别?
哪个英文我也看了!!不错!
很容易明白!!谢谢你们!!