1、stream只是一个媒介,它在数据库字段与实际的图片间充当一个搬运工作用。
当读出时,图片字段写入stream中,,然后stream导出成一个实际图片
当写入时,实际图片装入stream中,,然后字段再从steam中读取
注意:这里写和读,是字段对stream的操作。从数据库中取出数据,相当于字段先写到stream中。
从外面写数据到数据库,相当于字段从stream中读取存盘。
2、利用rs.addnew和rs.update进行记录的新增和更新。
3、引用ADO 2.5或以上
Dim cn As New ADODB.Connection Dim rs As New ADODB.Recordset Dim stm As New ADODB.Stream Private Sub Command1_Click() '读取 cn.Open rs.Open "select * from 图书销售员 where 员工号='" & MSHFlexGrid1.TextMatrix(MSHFlexGrid1.RowSel,1) & "'",cn,adOpenKeyset,adLockOptimistic If Not rs.EOF Then If IsNull(rs.Fields("照片")) Then Image1.Picture = LoadPicture("") Else stm.Open stm.Type = adTypeBinary stm.Write rs.Fields("照片") '写进stream对象中 stm.SaveToFile App.Path & "\22.gif",adSaveCreateOverWrite '由stream导出成文件 Image1.Picture = LoadPicture(App.Path & "\22.gif") stm.Close End If End If rs.Close cn.Close End Sub Private Sub Command2_Click() '写入 cn.Open rs.Open "select * from 图书销售员 where 员工号='" & MSHFlexGrid1.TextMatrix(MSHFlexGrid1.RowSel,adLockOptimistic If Not rs.EOF Then If IsNull(rs.Fields(3)) Then Image1.Picture = LoadPicture("") stm.Open stm.Type = adTypeBinary stm.LoadFromFile "D:\a.gif" '实际文件装进stream rs.Fields(3).Value = stm.Read rs.Update stm.Close End If End If rs.Close cn.Close End Sub Private Sub Form_Load() cn.Open "Provider=sqlOLEDB.1;Password=123;Persist Security Info=True;User ID=sa;Initial Catalog=book;Data Source=ZHENG" Set MSHFlexGrid1.DataSource = cn.Execute("select * from 图书销售员") cn.Close Text1.Text = MSHFlexGrid1.RowSel End Sub Private Sub MSHFlexGrid1_SelChange() Text1.Text = MSHFlexGrid1.RowSel End Sub @H_301_27@