绑定图片框 [英] Binding PictureBox

查看:65
本文介绍了绑定图片框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我有一个带有图片框的Windows窗体,并且我有一个使用表适配器链接到该窗体的访问数据库.该数据库具有OLE对象类型的字段,并存储位图图像.
我需要能够从数据库检索图像到picturebox控件,但是我的代码显示错误-参数无效.请帮助我

Hi
I have a windows form with a picturebox and i have an access database that is linked to the form with a table adapter. The database has a field that is of type OLE Object and stores bitmap images.
I need to be able to retrieve the image from the database to the picturebox control but the code i have is presenting an error- Parameter is not valid. Please Help Me

//Method
public Bitmap GetPhoto(byte[] photoBits)
        {
            if (photoBits != null && photoBits.Length > 0)
            {
                using (MemoryStream stream = new MemoryStream(photoBits))
                {
                    return new Bitmap(stream);
                }
            }
            else
            {
                return null;
            }
        }
cmd = new OleDbCommand();
cmd.Connection = con;
cmd.CommandText = "select M_Photo from MemberPhoto where M_id=''" + mSearch.Text + ''";
DataSet ds = new DataSet();                    
OleDbDataAdapter da = new OleDbDataAdapter();
da.SelectCommand = cm;
da.Fill(ds, "MemberPhoto");                 
pictureBox1.Image = GetPhoto((byte[])dd.Tables[0].Rows[0]["M_Photo"]);

推荐答案

这可能是不正确的,因为我只是在判断代码项目< pre></pre>所应用的语法颜色.标签,顺便说一句,我不得不代表您申请).

该部分的格式似乎不正确(mSearch.Text +之后的所有内容都被视为一个字符串.
This may not be correct since I am judging simply on the syntax colouring applied by the Code Project <pre></pre> tags, which incidentally I have had to apply on your behalf).

This part appears not to be correctly formatted (everything after mSearch.Text + is considered to be one string.
cmd.CommandText = "select M_Photo from MemberPhoto where M_id=''" + mSearch.Text + ''";
DataSet ds = new DataSet();                    
OleDbDataAdapter da = new OleDbDataAdapter();
da.SelectCommand = cm;
da.Fill(ds, "MemberPhoto");                 
pictureBox1.Image = GetPhoto((byte[])dd.Tables[0].Rows[0]["M_Photo"]);






whereas

cmd.CommandText = "select M_Photo from MemberPhoto where M_id=''" + mSearch.Text + "''"; 
DataSet ds = new DataSet();                    
OleDbDataAdapter da = new OleDbDataAdapter();
da.SelectCommand = cm;
da.Fill(ds, "MemberPhoto");                 
pictureBox1.Image = GetPhoto((byte[])dd.Tables[0].Rows[0]["M_Photo"]);



格式正确.

您可以使用参数化查询"(例如Google)来避免所有这些情况.这样做还有助于避免SQLInjection攻击.



appears correctly formatted.

All of this could have been avoided by your using a ''parameterized query'' (just Google that for examples). Doing so will also help to avoid SQLInjection attacks.


这篇关于绑定图片框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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