从mysql数据库将图像加载到picturebox时出错。的NullReferenceException

时间:2016-02-15 22:30:03

标签: c# mysql picturebox

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

之下
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;
}

1 个答案:

答案 0 :(得分:1)

管理解决问题,不得不改变

  

图片

  

图像

for( int line_count = 0;
     line_count < 20;
     line_count++ )
{
    double line_temp_celsius =
          start_celsius + line_count * celsius_increment;
    // calculate the other values based line_temp_celsius
    ...
}