如何从 Access 数据库显示/检索或获取图像到 PictureBox? [英] How to Show/Retrieve or Get the Image to PictureBox from Access Database?

查看:27
本文介绍了如何从 Access 数据库显示/检索或获取图像到 PictureBox?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Private Sub UpdatePicture()

    Dim str As String
    str = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=UsersDB.accdb"
    cn = New OleDbConnection(str)
    cn.Open()

    Dim ms As New MemoryStream()
    Dim arrimage() As Byte
    If (PictureBox1.Image IsNot Nothing) Then
        PictureBox1.Image.Save(ms, PictureBox1.Image.RawFormat)
        arrimage = ms.GetBuffer
        ms.Close()
    End If

    With cmd
        .Connection = cn
        .CommandText = "UPDATE  Users set Picture = @img where StudentNumber " & TextBox1.Text & ""
        .Parameters.Add("@img", OleDbType.Binary).Value = IIf(PictureBox1.Image IsNot Nothing, arrimage, DBNull.Value)
        'con.Open()
        i = .ExecuteNonQuery()
        .Dispose()
        cn.Close()
        If (i > 0) Then
            MsgBox("Save Successs!")
        End If
    End With
    con.Close()

End Sub

有人可以根据此代码给我显示/检索或从 Access 数据库中获取图像到我的 PictureBox1.Image 的代码吗?

Can someone give me the code to Show/Retrieve or Get the Image to my PictureBox1.Image from Access Database based on this code?

推荐答案

我刚刚想通了,这就是解决方案:

I just figured it out, this is the solution:

Public Class Form

    Dim con As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\UsersDB.accdb")
    Dim cmd As New OleDbCommand("", con)
    Dim Reader As OleDb.OleDbDataReader
    Dim cn As New OleDbConnection
    Dim i As Integer

  Public Sub GetData()

        con.Open()
        Dim dt As New DataTable("Users")
        Dim rs As New OleDb.OleDbDataAdapter("Select * from Users where StudentNumber='" & TextBox1.Text & "' ", con)
        rs.Fill(dt)
        DataGridView1.DataSource = dt
        DataGridView1.Refresh()
        Label1.Text = dt.Rows.Count
        rs.Dispose()
        con.Close()

        If Val(Label1.Text) = 1 Then
            Dim i As Integer
            i = DataGridView1.CurrentRow.Index
            'Image
            Dim bytes As [Byte]() = (DataGridView1.Item(6, i).Value)
            Dim ms As New MemoryStream(bytes)
            PictureBox1.Image = Image.FromStream(ms)

        End If
  End Sub

End Class

这篇关于如何从 Access 数据库显示/检索或获取图像到 PictureBox?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆