vb.net 从数据库中检索图像

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14576935/
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-17 12:04:56  来源:igfitidea点击:

retrieving image from database

vb.netpicturebox

提问by VBeginner

I'm working on my project which displays a list of employee. here, the information and picture of the employee will be shown. my project can now show the list of employees in the listbox and when I double click on an employee, his/her profile will be shown on a textbox. my problem is that i can't make their pictures to show in the picturebox. I already stored their picture on a table in my database along with their id, name, and profile. It only shows the picture of the first employee on the table. can anybody help me?

我正在处理我的项目,该项目显示员工列表。在这里,将显示该员工的信息和图片。我的项目现在可以在列表框中显示员工列表,当我双击员工时,他/她的个人资料将显示在文本框中。我的问题是我无法让他们的照片显示在picturebox. 我已经将他们的照片连同他们的 ID、姓名和个人资料一起存储在我的数据库中的一个表中。It only shows the picture of the first employee on the table. 有谁能够帮助我?

here's what I already done:

这是我已经完成的:

I populated the listbox:

我填充了列表框:

Call Connect() 
    With Me
        STRSQL = "Select employee_name from Employees"
        Try
            myCmd.Connection = myConn
            myCmd.CommandText = STRSQL
            reader = myCmd.ExecuteReader
            If (reader.Read()) Then
                reader.Close()
                adptr.SelectCommand = myCmd
                adptr.Fill(dt)
                lstEmployee.DisplayMember = "employee_name"
                lstEmployee.ValueMember = "employee_id"
                If dt.Rows.Count > 0 Then
                    For i As Integer = 0 To dt.Rows.Count - 1
                        lstEmployee.Items.Add(dt.Rows(i)("employee_name"))
                    Next
                End If
            End If
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End With

here's how I show the info on textbox

这是我在文本框上显示信息的方式

Dim FileSize As UInt32
    Dim mStream As New System.IO.MemoryStream()
    Dim arrImage() As Byte = mStream.GetBuffer()
    FileSize = mStream.Length
    Dim cmd As New MySqlCommandBuilder
    Call Connect()
    With Me
        STRSQL = "select employee_name, profile from Employees where employee_id = " & lstEmployee.SelectedIndex
        Try
            myCmd.Connection = myConn
            myCmd.CommandText = STRSQL
            reader = myCmd.ExecuteReader

            If (reader.Read()) Then
                txtName.Text = reader("employee_name")
                txtProfile.Text = reader("profile")
            End If
        Catch ex As Exception
            MsgBox(ex.Message)
        Finally
            myConn.Close()
        End Try
        adptr.SelectCommand = myCmd
        dt = New DataTable
        adptr = New MySqlDataAdapter("select picture from Employees", myConn)
        cmd = New MySqlCommandBuilder
        adptr.Fill(dt)
        Dim lb() As Byte = dt.Rows(0).Item("picture")
        Dim lstr As New System.IO.MemoryStream(lb)
        pix.Image = Image.FromStream(lstr)
        pix.SizeMode = PictureBoxSizeMode.StretchImage
        lstr.Close()

回答by spajce

You miss the where clauseis must be like to search a one employee to view there image.

你想念的where clause是必须喜欢搜索一个员工来查看那里的图像。

adptr = _
      New MySqlDataAdapter("select picture from Employees " + _
      "where employee_id = " & lstEmployee.SelectedIndex, myConn)
cmd = New MySqlCommandBuilder
adptr.Fill(dt)
Dim lb() As Byte = dt.Rows(0).Item("picture")
Dim lstr As New System.IO.MemoryStream(lb)
pix.Image = Image.FromStream(lstr)
pix.SizeMode = PictureBoxSizeMode.StretchImage
lstr.Close()

P.S.: You must study the LINQ (Language-Integrated Query)for better approach.

PS:您必须学习LINQ(语言集成查询)以获得更好的方法。