图片框和访问 [英] Picturebox and Access

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

问题描述


我有一个带有图片框的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 generating 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"]);

推荐答案



请改用命令参数,
Hi,

use command parameter instead,
cmd.CommandText = "select M_Photo from MemberPhoto where M_id=@MID";
cmd.CommandParameter.Add("MID").Value=mSearch.Text.Trim();
..........
..........
..........
System.Text.ASCIIEncoding  encoding=new System.Text.ASCIIEncoding();
pictureBox1.Image = GetPhoto(encoding.GetBytes(dd.Tables[0].Rows[0]["M_Photo"].ToString()));



希望这会有所帮助

谢谢



Hope this will help

Thanks


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

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