vba 如何在VB中将Access数据库中的图像显示/获取到PictureBox

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25949822/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-12 04:36:33  来源:igfitidea点击:

How to Display/Get Image from Access Database to PictureBox in VB

vb.netvbavisual-studio-2013

提问by Gherzone Noah Trinidad Pasion

Private Sub Form2_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    GetPicture()
End Sub


Public Sub GetPicture()

    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
        PictureBox1.Image = FixNull(DataGridView1.Item(6, i).Value)


    End If
______________________________

I got this Error on the line: PictureBox1.Image = FixNull(DataGridView1.Item(6, i).Value)

我在线上收到此错误: PictureBox1.Image = FixNull(DataGridView1.Item(6, i).Value)

-> Unable to cast object of type 'System.Byte[]' to type 'System.Drawing.Image'.

-> Unable to cast object of type 'System.Byte[]' to type 'System.Drawing.Image'.



Screenshot:

截屏:

回答by Gherzone Noah Trinidad Pasion

I just figgured 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()

公共子 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

结束类