如何在MySQL数据库longblob中检索图像? [英] how to retrieve image in MySQL database longblob?

查看:197
本文介绍了如何在MySQL数据库longblob中检索图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在项目中使用Windows Form和MySQL.在这种情况下,我想保存图像并进行检索.

I am using Windows Form and MySQL for my project. In that i want to save a image and retrieve that.

我在其中创建了一个名为"image"的表,

I have created a table named 'image' in that,

CREATE TABLE `image` (
    `id` INT(15) NOT NULL AUTO_INCREMENT,
    `extension` VARCHAR(50) NOT NULL,
    `image` LONGBLOB NOT NULL,
    PRIMARY KEY (`id`)
)
ENGINE=MyISAM
ROW_FORMAT=DEFAULT
AUTO_INCREMENT=2

还有

OpenFileDialog open = new OpenFileDialog();
            // image filters
            open.Filter = "Image Files(*.jpg; *.jpeg; *.gif; *.bmp; *.png)|*.jpg; *.jpeg; *.gif; *.bmp; *.png";
            if (open.ShowDialog() == DialogResult.OK)
            {                
                txt_imagePath.Text = open.FileName;
            }

            hp.getConnStr();
            try
            {
                MySqlConnection connection = new MySqlConnection(hp.myConnStr);
                MySqlCommand command = connection.CreateCommand();                
                command.CommandText = "insert into image (image) values ('"+txt_imagePath.Text +"')";
                command.Connection.Open();
                command.ExecuteNonQuery();
                command.Connection.Close();
            }

在浏览"按钮中单击事件,..

in Browse button click event,..

文件已成功保存.现在,我想检索该图片并显示在图片框中.这样我尝试下面的代码,..

The file is successfully saved. Now i want to retrieve that picture and show in picture box. So that i try this bellow code,..

 MySqlConnection connection = new MySqlConnection(hp.myConnStr);
                MySqlCommand command = connection.CreateCommand();
                MySqlDataReader Reader;
                command.CommandText = "select image from image";
                connection.Open();
                Reader = command.ExecuteReader();
                while (Reader.Read())
                {
                   pictureBox1.Image = new Bitmap(Reader[0].ToString()); 
                }
                connection.Close(); 

但没有用.

请帮助我.

推荐答案

您应将blob字段读取为MemoryStream,并使用Image.FromStream()设置为控件的Image属性.

You should read the blob field as a MemoryStream and set to Image property of the control using Image.FromStream().

这篇关于如何在MySQL数据库longblob中检索图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆