Option Explicit
Dim conn As ADODB.Connection
Dim rs As ADODB.Recordset
Dim StmPic As ADODB.Stream
Dim StrPicTemp As String
Dim i As Integer
Private Sub Form_Load()
Set conn = New ADODB.Connection
conn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\db1.mdb;Persist Security Info=False"
Set rs = New ADODB.Recordset
rs.Open "select * from picture_info", conn, adOpenKeyset, adLockOptimistic
End Sub
Private Sub cmdShow_Click() ‘单击按钮同时显示n幅图像
For i = 0 To n
If Not rs.EOF Then
If Not IsNull(rs.Fields(1)) Then
Set StmPic = New ADODB.Stream
StrPicTemp = "c:\temp.tmp" '临时文件,用来保存读出的图片
With StmPic
.Type = adTypeBinary '二进制类型
.Open '打开
.Write rs.Fields(1) '写入数据库中的二进制文件
.SaveToFile StrPicTemp, adSaveCreateOverWrite '保存读出的图片到临时文件中
.Close
End With
Picture1(i).Picture = LoadPicture(StrPicTemp) '载入临时文件中的图像
End If
rs.MoveNext
Else
rs.MoveFirst
End If
Next
End Sub
Private Sub cmdEnd_Click()
rs.Close
conn.Close
End
End Sub
用VB程序将图片保存到数据库 SQL SERVER 中,并从数据库中读出该图片。
Private Sub Command3_Click()
Dim c As New ADODB.Connection
c.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\1.mdb;Persist Security Info=False"
c.Execute "create table a (b longbinary)"
End Sub
Private Sub Command4_Click()
Set b = New ADODB.Recordset
Set c = New ADODB.Stream
b.Close
Set b = New ADODB.Recordset
b.Open "select * from a", "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\1.mdb;Persist Security Info=False", adOpenKeyset, adLockOptimistic
MsgBox b.RecordCount
b.MoveLast
c.Write (b.Fields.Item(0).Value)
c.SaveToFile "c:\aa.bmp", adSaveCreateOverWrite
Picture1.Picture = LoadPicture("c:\aa.bmp")
End Sub
'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"
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