如何将图像从数据库映射到图片框 [英] how can fatch image from database to picture box

查看:94
本文介绍了如何将图像从数据库映射到图片框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好

我在sql server中插入一个图像并将这个图像传送到另一个web表单。

但显示和错误我的代码是

此代码用于将图像插入到sqldatabase列中imgdata是varchar类型:

hello
i am inserting an image in sql server and fatch this image to another web form.
but is show and error my code is
this code is use for insert image into sqldatabase column imgdata is varchar type:

OpenFileDialog dlg = new OpenFileDialog();
            DialogResult dlgRes = dlg.ShowDialog();
            if (dlgRes != DialogResult.Cancel)
            {
                //Set image in picture box
                pictureBox1.ImageLocation = dlg.FileName;
                //Provide file path in txtImagePath text box.
                txtimagepath.Text = dlg.FileName;
            }
  MemoryStream stream = new MemoryStream();
                pictureBox1.Image.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg);
                byte[] imageData = stream.ToArray();    

//and below code use for fatch image :
 SqlDataAdapter dataAdapter = new SqlDataAdapter(new SqlCommand("select imgdata from Joiningform where regno='" + regno + "'",con));
           DataSet dataSet = new DataSet();
            dataAdapter.Fill(dataSet);
            if (dataSet.Tables[0].Rows.Count == 1)
            {
                Byte[] data = new Byte[0];
                data = (Byte[])(dataSet.Tables[0].Rows[0]["imgdata"]);
                MemoryStream mem = new MemoryStream(data);
                pictureBox1.Image = Image.FromStream(mem);
                
            }  



但在该行上方显示异常:DataSet dataSet = new DataSet(); System.InvalidOperationException未处理

消息=填充:SelectCommand.Connection属性尚未初始化。


but below exception is display on that line: DataSet dataSet = new DataSet(); System.InvalidOperationException was unhandled
Message=Fill: SelectCommand.Connection property has not been initialized.

推荐答案

你的代码看起来不错。

但是,在将它用于SqlAdapter之前,请尝试打开连接

You're code just looks good.
But still, try to Open the connection before using it for SqlAdapter.
con.Open();
SqlDataAdapter dataAdapter = new SqlDataAdapter(new SqlCommand("select imgdata from Joiningform where regno='" + regno + "'",con));





-KR



-KR


看来你没有初始化SQL连接和连接字符串,

首先用连接字符串创建SQL连接。
It seems you doesn't initilise SQL connection and connection string,
First create SQL connection with connection string.


这篇关于如何将图像从数据库映射到图片框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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