'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
'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
解决方案 »
- 10个Text中输入数值自动找出最大值与最小值并且有闪烁功能
- 容易的问题,但是我不知道,请帮帮忙!
- 局域网的一个问题,只要进来说说与上不同的办法就加分!!!
- 自定义按钮如何定义default属性?
- vb 中是否有查找硬盘中文件的函数?
- 在线等待!!在程序运行中出现“~”作用于对象"~"错误,是什么原因引起的,怎么解决
- 非常简单的问题:VB中自定义数据类型?
- 如何用VB調用和管理網上的MDB數據庫?
- 求大神帮忙写:VC6 函数生成dll文件后 VB6调用
- 如何获取网络中所有服务器信息? NetServerEnum 调用为什么出错? 请帮忙!!!
- VB中有哪些方法可以调用C/C++写的函数等。
- 怎样取得网卡序列号 ?
qurstr = "select * from tikubiao where tu=" & "'" & u & "'"
itemrs.Open (qurstr)
'Set typers = problemdb.OpenRecordset(qurstr)
If Not itemrs.EOF Then
Picture1.Picture = LoadPicture("tu\" & itemrs("tu"))
Else
Picture1.Picture = LoadPicture("tu\empty.bmp")
End If
存:
Public Sub save_picture()
Dim cnn As New ADODB.Connection, rst As New ADODB.Recordset
Dim bit() As Byte
dim varPath as string '图片的路径 cnn.open "连接数据库的字符串"
sSql = "SELECT * FROM 表 WHERE 关键字='" & 关键值 & "'"
'选出要增加或修改图片记录的记录
rst.Open sSql, cnn, adOpenKeyset, adLockOptimistic
If Not (rst.EOF And rst.BOF) Then
If VarPath = "" Then
' 然后将字节数组的内容写入数据库即可
rst.Fields("INFO_PICT") = ""
rst.UPDATE
Else
Open VarPath For Binary As #1
ReDim bit(LOF(1)) As Byte
Get 1, 1, bit
Close 1
' 然后将字节数组的内容写入数据库即可
rst.Fields("INFO_PICT").AppendChunk bit
rst.UPDATE
End If
End If
end sub 取:
Public Sub show_picture()
Dim REC As Recordset
Dim sSql As String
Dim I As Integer
Set REC = New Recordset
Dim bit1() As Byte
Dim sa As String
sSql = "SELECT * FROM 表 WHERE 关键字 ='" & 关键值 & "'"
REC.Open sSql, Conn, adOpenStatic, adLockOptimistic, adCmdText
If REC.EOF Or REC.BOF Then
Exit Sub
Else
REC.Movefirst
While Not REC.BOF
Picture1.Picture = Nothing
If REC("INFO_PICT").ActualSize > 0 Then
bit1 = REC.Fields("图片").GetChunk(REC("图片").ActualSize)
'然后将字节数组的内容拼装成文件即可
Open "c:\1.bmp" For Binary As #1
Put 1, 1, bit1
Close 1
Picture1.Picture = LoadPicture"c:\1.bmp")
End If
REC.Movenext
Wend
End If
rec.close
Set REC = Nothing
Exit Sub
Err:
MsgBox "读取图片出错!", OKOnly, "系统提示"
End Sub
On Error GoTo ProcErr
Dim Datafile As Integer
Dim FileLength As Long
Dim chunk() As Byte
Datafile = FreeFile
Open Filename For Binary Access Read As Datafile
FileLength = LOF(Datafile)
If FileLength = 0 Then GoTo ProcErr
ReDim chunk(FileLength)
Get Datafile, , chunk()
Close Datafile
ProcExit:
Image2Chunk = chunk()
Exit FunctionProcErr:
Image2Chunk = 0
End FunctionDim chunk() As Byte
chunk() = Image2Chunk(tmpFile1)
.Fields("tp1").AppendChunk chunk()
.Update