vb.net 使用Access数据库 保存和读取图片文件

不好意思忘记是转帖那位网友的。

 

下面是一个简单的演示

Access数据库
字段名称    数据类型
编号          自动编号
pic             OLE 对象

Public Class Form1
    Dim Conn As New OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:/db.mdb")
    Dim Cmd As New OleDb.OleDbCommand
    Dim DataRd As OleDb.OleDbDataReader

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Cmd.Connection = Conn
        Cmd.Parameters.Add(New OleDb.OleDbParameter)
    End Sub

    '添加图片
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim OpenDialog As New OpenFileDialog
        If OpenDialog.ShowDialog = Windows.Forms.DialogResult.OK Then
            Dim da() As Byte
            da = My.Computer.FileSystem.ReadAllBytes(OpenDialog.FileName)
            Cmd.CommandText = "insert into biao(pic) values(?)"
            Cmd.Parameters(0).Value = da
            Conn.Open()
            If Cmd.ExecuteNonQuery > 0 Then
                MsgBox("添加图片成功")
            End If
            Conn.Close()
        End If
    End Sub

    '读取图片
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Cmd.CommandText = "select pic from biao where 编号=1" '注意选择编号
        Conn.Open()
        DataRd = Cmd.ExecuteReader
        If DataRd.Read Then
            Dim da() As Byte
            da = DataRd(0)
            PictureBox1.Image = Bitmap.FromStream(New IO.MemoryStream(da))
        End If
        DataRd.Close()
        Conn.Close()
    End Sub
End Class

已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页