简体   繁体   English

如何从mysql检索数据到包含image列的datagridview?

[英]How to retrieve data from mysql to a datagridview that contains an image column?

I tried retrieving data from MySQL to datagridview. 我尝试将数据从MySQL检索到datagridview。 The table contains an image column. 该表包含一个图像列。 When I remove the image column, the code works fine but it doesn't seem to work with the image column included. 当我删除图像列时,代码工作正常,但似乎不适用于包含的图像列。

Here's my code. 这是我的代码。

Mda= new mysldataadapter ("select * from user_info", con)
Dt = new datatable
Mda.fill(dt)

With datagridview1
. Datasource =dt
End with

I get this error msg 我收到此错误消息

Datagridview default error dialog Datagridview默认错误对话框
System.argumentexception: parameter is not valid at System.argumentException:参数在以下位置无效
system.drawing.image.fromstream(.... system.drawing.image.fromstream(....

Try using MemoryStream to store the image then show it... something like this : 尝试使用MemoryStream来存储图像,然后显示它...像这样:

Dim command As New MySqlCommand("SELECT `id`, `pic` FROM `mytb` WHERE `id` = @ID", connection)

command.Parameters.Add("@ID", MySqlDbType.UInt64).Value = TextBoxID.Text

Dim Adapter As New MySqlDataAdapter(command)
Dim DBTable As New DataTable()

Try
    Adapter.Fill(DBTable)

    Dim imgByte() As Byte

    If DBTable.Rows.Count = 1 Then
        imgByte = DBTable(0)(1)

        Dim MS As New MemoryStream(imgByte)
        PictureBox1.Image = Image.FromStream(MS)
    End If
Catch ex As Exception
    MessageBox.Show("ERROR")
    PictureBox1.Image = Nothing
End Try

NOTE : The data type for the column must be BLOB . 注意:列的数据类型必须为BLOB

Feel free to ask about anything you don't understand. 随意询问任何您不了解的内容。

Hope that was helpful. 希望对您有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM