用二进制存取: Option Explicit Private mycn As ADODB.Connection Private rs As ADODB.Recordset Private stream1 As StreamPrivate Sub Form_Load() Set mycn = New ADODB.Connection mycn.Open "Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=image;Data Source=lover" Set rs = New ADODB.Recordset rs.Open "picture", mycn, adOpenKeyset, adLockOptimistic Set stream1 = New ADODB.Stream stream1.Type = adTypeBinary stream1.Open End Sub Private Sub Command2_Click() Dim filepath As String 'Me.CommonDialog1.FileTitle = "OPEN" Me.CommonDialog1.Filter = "JPG文件(*.jpg)|*.jpg" Me.CommonDialog1.ShowOpen filepath = Me.CommonDialog1.FileName If filepath <> "" Then stream1.LoadFromFile filepath rs.AddNew rs.Fields(0).Value = stream1.Read rs.Update If Err.Number <> 0 Then MsgBox "successful to save image!" Else MsgBox Err.Number & ":" & Err.Description End If End If rs.MoveFirst stream1.Write rs.Fields(0).Value stream1.SaveToFile App.Path & "\210.jpg", adSaveCreateOverWrite Me.Image1.Picture = LoadPicture(App.Path & "\210.jpg") End Sub Private Sub Form_Unload(Cancel As Integer) stream1.Close rs.Close mycn.Close Set stream1 = Nothing Set rs = Nothing Set mycn = Nothing End Sub
你用的是data还是ado 在vb5里存取图片很简单 但到了vb6的ado存取图片却很难需要api
参考以下 '******************将图片文件保存到数据库中************************* Sub SavePicToDb(cn As ADODB.Connection, table1 As String, field1 As String, file1 As String, id1 As String) On Error Resume Next Dim stm As ADODB.Stream Set stm = New ADODB.Stream Set rs1 = New ADODB.Recordset rs1.Open "select * from " & table1 & " where id = " & id1, cn, adOpenKeyset, adLockOptimistic With stm .Type = adTypeBinary .Open .LoadFromFile file1 'DLG.FileName End With With rs1 .Fields(field1) = stm.Read .Update End With rs1.Close Set rs1 = Nothing End Sub
'*************************************************************** '*从数据库中相应的表里边读取该合同的所有图片 '*并将他们保存到当前路径下面的temp目录下边 '*文件名用在表中保存的文件名 '**************************************************************** Sub GetPicFromDB(cn As ADODB.Connection) On Error Resume Next Dim fld As Field Dim strTemp As String Dim stm As ADODB.Stream Set stm = New ADODB.Stream 'strTemp = "c:\temp.bmp" Set rs1 = New ADODB.Recordset rs1.Open "select * from rs_http where htbh='" & frm_manage.Grid2.TextMatrix(frm_manage.Grid2.RowSel, 0) & "'", cn, , , adCmdText While Not rs1.EOF '*********将数据库中的文件读到硬盘上************************* ' strTemp = App.Path + "\temp\" + rs1!Name '`临时文件,用来保存读出的图片 With stm .Type = adTypeBinary .Open .Write rs1("tp").value strTemp = App.Path & "\temp1\" & rs1!Name .SaveToFile strTemp, adSaveCreateOverWrite .Close End With Set itemX = lvwPic.ListItems.add(, App.Path & "\temp1\" & rs1!Name, rs1!Name, 1, 1) itemX.SubItems(1) = rs1!bz rs1.MoveNext Wend Set stm = Nothing rs1.Close Set rs1 = Nothing End Sub
用AppendChunk 和 GetChunk 临时写的例子:(同样的方法我在sql和access中都成功作过) 用同样的方法可以把任何文件存到数据库里(取出的时候不一样,可以根据不同的文件格式打开) 有问题mailto: [email protected]''''''''''''''''''''''''''''''''''''' '取出(可以放在一个按钮的事件里面) Dim image_data_out() As Byte Dim db As Connection Set db = New Connection '连接一个数据库,换成你自己的库 db.Open "Provider=sqloledb;Initial Catalog=kyp;User Id=sa;Password=1111; " Dim sql As String '选择一个带图片的表格,换成你自己的表格 sql = "select * from linkman" Dim rs As Recordset Set rs = New Recordset rs.Open sql, db, adOpenStatic '取出图片子段的内容, '我的表格中"photo"存储图片, '"photoformat"存储图片的扩展名 '"photosize"存储图片的大小 image_data_out = rs.Fields("photo").GetChunk(rs.Fields("photosize")) Dim photoformat As String photoformat = rs.Fields("photoformat") rs.Close Set rs = Nothing db.Close Set db = Nothing 'img2是一个image控件 '将数据保存维一个临时的图片,用image显示,然后删除临时图片 Open App.Path + "\temp." + photoformat For Binary As #1 Put #1, , image_data_out() Close #1 img2.Picture = LoadPicture(App.Path + "\temp." + photoformat) Kill App.Path + "\temp." + photoformat ''''''''''''''''''''''''''''''''''''''存入 Dim image_data_in() As Byte Dim i As Long Open 图片文件 For Binary As #1 i = LOF(1) If i > 0 Then ReDim image_data_in(i - 1) Else Close #1 Exit Sub End If Get #1, , image_data_in() Close #1 Dim ExtensionName As String Dim fs Set fs = CreateObject("Scripting.FileSystemObject") ExtensionName = fs.GetExtensionName(txtpath) Set fs = NothingDim db As Connection Set db = New Connection db.Open "Provider=sqloledb;Initial Catalog=kyp;User Id=sa;Password=1111; " Dim sql As String Dim rs As Recordset Set rs = New Recordset rs.Open "linkman", db, , adLockOptimisticrs.Fields("photo").AppendChunk image_data_in rs.Fields("photoformat") = ExtensionName rs.Fields("photosize") = i rs.Update rs.Close Set rs = Nothing db.Close Set db = Nothing MsgBox "OK!", vbInformation, "提示"
Option Explicit
Private mycn As ADODB.Connection
Private rs As ADODB.Recordset
Private stream1 As StreamPrivate Sub Form_Load()
Set mycn = New ADODB.Connection
mycn.Open "Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=image;Data Source=lover"
Set rs = New ADODB.Recordset
rs.Open "picture", mycn, adOpenKeyset, adLockOptimistic
Set stream1 = New ADODB.Stream
stream1.Type = adTypeBinary
stream1.Open
End Sub
Private Sub Command2_Click()
Dim filepath As String
'Me.CommonDialog1.FileTitle = "OPEN"
Me.CommonDialog1.Filter = "JPG文件(*.jpg)|*.jpg"
Me.CommonDialog1.ShowOpen
filepath = Me.CommonDialog1.FileName
If filepath <> "" Then
stream1.LoadFromFile filepath
rs.AddNew
rs.Fields(0).Value = stream1.Read
rs.Update
If Err.Number <> 0 Then
MsgBox "successful to save image!"
Else
MsgBox Err.Number & ":" & Err.Description
End If
End If
rs.MoveFirst
stream1.Write rs.Fields(0).Value
stream1.SaveToFile App.Path & "\210.jpg", adSaveCreateOverWrite
Me.Image1.Picture = LoadPicture(App.Path & "\210.jpg")
End Sub
Private Sub Form_Unload(Cancel As Integer)
stream1.Close
rs.Close
mycn.Close
Set stream1 = Nothing
Set rs = Nothing
Set mycn = Nothing
End Sub
在vb5里存取图片很简单
但到了vb6的ado存取图片却很难需要api
'******************将图片文件保存到数据库中*************************
Sub SavePicToDb(cn As ADODB.Connection, table1 As String, field1 As String, file1 As String, id1 As String)
On Error Resume Next
Dim stm As ADODB.Stream
Set stm = New ADODB.Stream
Set rs1 = New ADODB.Recordset
rs1.Open "select * from " & table1 & " where id = " & id1, cn, adOpenKeyset, adLockOptimistic
With stm
.Type = adTypeBinary
.Open
.LoadFromFile file1 'DLG.FileName
End With
With rs1
.Fields(field1) = stm.Read
.Update
End With
rs1.Close
Set rs1 = Nothing
End Sub
'*从数据库中相应的表里边读取该合同的所有图片
'*并将他们保存到当前路径下面的temp目录下边
'*文件名用在表中保存的文件名
'****************************************************************
Sub GetPicFromDB(cn As ADODB.Connection)
On Error Resume Next
Dim fld As Field
Dim strTemp As String
Dim stm As ADODB.Stream
Set stm = New ADODB.Stream
'strTemp = "c:\temp.bmp"
Set rs1 = New ADODB.Recordset
rs1.Open "select * from rs_http where htbh='" & frm_manage.Grid2.TextMatrix(frm_manage.Grid2.RowSel, 0) & "'", cn, , , adCmdText
While Not rs1.EOF
'*********将数据库中的文件读到硬盘上*************************
' strTemp = App.Path + "\temp\" + rs1!Name '`临时文件,用来保存读出的图片 With stm
.Type = adTypeBinary
.Open
.Write rs1("tp").value
strTemp = App.Path & "\temp1\" & rs1!Name
.SaveToFile strTemp, adSaveCreateOverWrite
.Close
End With
Set itemX = lvwPic.ListItems.add(, App.Path & "\temp1\" & rs1!Name, rs1!Name, 1, 1)
itemX.SubItems(1) = rs1!bz
rs1.MoveNext
Wend
Set stm = Nothing
rs1.Close
Set rs1 = Nothing
End Sub
临时写的例子:(同样的方法我在sql和access中都成功作过)
用同样的方法可以把任何文件存到数据库里(取出的时候不一样,可以根据不同的文件格式打开)
有问题mailto: [email protected]'''''''''''''''''''''''''''''''''''''
'取出(可以放在一个按钮的事件里面)
Dim image_data_out() As Byte
Dim db As Connection
Set db = New Connection
'连接一个数据库,换成你自己的库
db.Open "Provider=sqloledb;Initial Catalog=kyp;User Id=sa;Password=1111; "
Dim sql As String
'选择一个带图片的表格,换成你自己的表格
sql = "select * from linkman"
Dim rs As Recordset
Set rs = New Recordset
rs.Open sql, db, adOpenStatic
'取出图片子段的内容,
'我的表格中"photo"存储图片,
'"photoformat"存储图片的扩展名
'"photosize"存储图片的大小
image_data_out = rs.Fields("photo").GetChunk(rs.Fields("photosize"))
Dim photoformat As String
photoformat = rs.Fields("photoformat")
rs.Close
Set rs = Nothing
db.Close
Set db = Nothing
'img2是一个image控件
'将数据保存维一个临时的图片,用image显示,然后删除临时图片
Open App.Path + "\temp." + photoformat For Binary As #1
Put #1, , image_data_out()
Close #1
img2.Picture = LoadPicture(App.Path + "\temp." + photoformat)
Kill App.Path + "\temp." + photoformat
''''''''''''''''''''''''''''''''''''''存入
Dim image_data_in() As Byte
Dim i As Long
Open 图片文件 For Binary As #1
i = LOF(1)
If i > 0 Then
ReDim image_data_in(i - 1)
Else
Close #1
Exit Sub
End If
Get #1, , image_data_in()
Close #1
Dim ExtensionName As String
Dim fs
Set fs = CreateObject("Scripting.FileSystemObject")
ExtensionName = fs.GetExtensionName(txtpath)
Set fs = NothingDim db As Connection
Set db = New Connection
db.Open "Provider=sqloledb;Initial Catalog=kyp;User Id=sa;Password=1111; "
Dim sql As String
Dim rs As Recordset
Set rs = New Recordset
rs.Open "linkman", db, , adLockOptimisticrs.Fields("photo").AppendChunk image_data_in
rs.Fields("photoformat") = ExtensionName
rs.Fields("photosize") = i
rs.Update
rs.Close
Set rs = Nothing
db.Close
Set db = Nothing
MsgBox "OK!", vbInformation, "提示"