加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

VB 利用ADO的Stream对象在数据库进行写入与读出

发布时间:2020-12-17 08:16:19 所属栏目:百科 来源:网络整理
导读:1、stream只是一个媒介,它在数据库字段与实际的图片间充当一个搬运工作用。 当读出时,图片字段写入stream中,,然后stream导出成一个实际图片 当写入时,实际图片装入stream中,,然后字段再从steam中读取 注意:这里写和读,是字段对stream的操作。从数据

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读