retrieving image from database

2020-05-09 19:11发布

问题:

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?

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()

回答1:

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

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.