将图像从 mysql 数据库加载到图片框时出错。空引用异常

Error loading image to picturebox from mysql database. NullReferenceException

我正在尝试从数据库中获取图像并显示到图片框中。我的代码在下面

private void getImage()
{
    using (MySqlConnection conn = new MySqlConnection(connectionManager.connectionString))
    {
        try
        {
            conn.Open();

            string query = "SELECT 'Image' FROM student_img WHERE ID =  @ID";

            MySqlCommand cmd = new MySqlCommand(query, conn);

            int id = 10;
            cmd.Parameters.AddWithValue("@ID", id);

            var da = new MySqlDataAdapter(cmd);
            var ds = new DataSet();
            da.Fill(ds, "Image");
            int count = ds.Tables["Images"].Rows.Count;

            if (count > 0)
            {
                var data = (Byte[])(ds.Tables["Images"].Rows[count - 1]["Image"]);
                var stream = new MemoryStream(data);
                picLogo.Image = Image.FromStream(stream);
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show("Connection Error!\n" + ex.Message, "Error Message",
                    MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
    }

}

但是我收到 NullReferenceException 错误。我还有另一种方法,也许我可以以某种方式使用它,但我不确定如何从数据库中获取数据,因为我是 C#

的新手
public Image byteArrayToImage(byte[] byteArrayIn)
{
    MemoryStream ms = new MemoryStream(byteArrayIn);
    Image returnImage = Image.FromStream(ms);
    return returnImage;
}

设法解决了问题,不得不更改

Images

Image

private void getImage()
        {
            using (MySqlConnection conn = new MySqlConnection(connectionManager.connectionString))
            {
                try
                {
                    conn.Open();

                    string query = "SELECT Image FROM student_img WHERE ID =  @ID";

                    MySqlCommand cmd = new MySqlCommand(query, conn);

                    int id = 10;
                    cmd.Parameters.AddWithValue("@ID", id);

                    var da = new MySqlDataAdapter(cmd);
                    var ds = new DataSet();
                    da.Fill(ds, "Image");
                    int count = ds.Tables["Image"].Rows.Count;

                    if (count > 0)
                    {
                        var data = (Byte[])(ds.Tables["Image"].Rows[count - 1]["Image"]);
                        var stream = new MemoryStream(data);
                        picLogo.Image = Image.FromStream(stream);
                    }

                }
                catch (Exception ex)
                {
                    MessageBox.Show("Connection Error!\n" + ex.Message, "Error Message",
                            MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }

        }