【问题标题】:Error loading image to picturebox from mysql database. NullReferenceException将图像从 mysql 数据库加载到图片框时出错。空引用异常
【发布时间】:2016-02-15 22:30:03
【问题描述】:

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

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

【问题讨论】:

  • But I am getting NullReferenceException error。在哪条线上?
  • 为什么要将图像存储在数据库中?数据库不应该存储文件。您应该在数据库中存储图像的链接或引用,然后根据该链接从文件服务器或网址中提取图像。
  • 您填写一个名为“Image”的表格,然后引用“Images”。您不需要数据适配器、数据集和数据表来从数据库中读取一个字段。有几件事需要处理
  • @Plutonix 你能解释一下我需要做些什么来解决这个问题吗?
  • @Necoras 我知道我不应该将文件存储在数据库中,但我正在学习 C# 并想了解它是如何工作的

标签: c# mysql picturebox


【解决方案1】:

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

图片

到

图片

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);
                }
            }

        }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多