如何将varbinary数据类型绑定到winforms中的datagridview [英] How to bind a varbinary datatype to datagridview in winforms

查看:80
本文介绍了如何将varbinary数据类型绑定到winforms中的datagridview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个问题,并没有从我身上解开,我在MS Sqlserver-2012的桌子上有一列 person_image varbinary(max),现在问题是我必须将此列绑定到我在windows窗体中的datagridview,实际上在将此二进制信息绑定到Datagridview之前,需要将其转换为图像,然后,我必须将它们绑定到datagridview,如同在其列中的图像所示,我也尝试使用datagridviewimagecolumn ...但它与破碎的图像绑定,实际上并没有真实的图像..还有另外一点要记住..我没有图像的路径,我在我的桌子上只有二进制图像在Back-结束..实际上我可以得到图像的路径...但这是唯一要做的事情...我在绑定之前无法将它们(二进制)转换为图像吗?我试图解决这个问题长达2天..但是在不同网站上找到的代码在修改我的整个应用程序代码方面变得越来越复杂......

这是我的代码ows并且还抛出一个异常,提到对象引用没有设置为实例在试用块上



Here is a problem which doesn't un-sticks from me ,,i have a column of person_image varbinary(max)at my table in MS Sqlserver-2012,, now the thing is i have to bind this column to my datagridview in windows forms, actually while before binding this binary information to Datagridview ,It's needs to be converted into images and then, i have to bind them to datagridview as shows as images in it's column,,i also tried with datagridviewimagecolumn...but it was binding with broken images,not actually with real images..here is another point to remember..i don't have the paths of images,i have only binary images at my table in Back-End..Infact i could get the paths of images...but is that is the only thing to do...did i can't convert them(binary) in to images before binding..?I am trying to fix this for long 2 days..but what the code found in different sites was getting complicated in the way of modifying my whole application code...
Here is the code which i follows and also it was throwing an exception that mentioning an object reference was not set to an instance at try block

public void BindDataGrid()
       {
           string bindQuery = "select * from employee";
           _cmd = new SqlCommand();
           _cmd.CommandText = bindQuery;
           _cmd.Connection = _con;
           _con.Open();
           _da = new SqlDataAdapter(_cmd);

           _ds = new DataSet();

           DataGridViewImageColumn dgi = new DataGridViewImageColumn();
           dgi.Name = "image";
           dgi.HeaderText = "Person_image";
           dgi.ImageLayout = DataGridViewImageCellLayout.Zoom;
           try
           {
               dgi.Image = bytearraytoimage((byte[])_ds.Tables["employee"].Rows[1]["image"]);
           }
           catch (Exception x)
           {

               MessageBox.Show(x.Message);
           }
           dataGridView1.Columns.Insert(0,dgi);
           _con.Close();
           _da.Fill(_ds);
           dataGridView1.DataSource = _ds.Tables[0];
       }

       private Image bytearraytoimage(byte[] b)
       {
          MemoryStream ms=new MemoryStream(b);
           Image img=Image.FromStream(ms);
           return img;
       }
       private void Form1_Load(object sender, EventArgs e)
       {
           BindDataGrid();
       }







我会如此感恩,如果我有一个聪明的代码来实现这个任务....任何建议..请... ....继续...




I will be so thankful,, if i have a smart code to achieve this task....any suggestions..please....go-on...

推荐答案

如果您的列是VARBINARY(50 )我不喜欢任何东西,图像相关。

即使作为原始数据图像,没有任何通常(和必需的)文件格式标题,这只是一个最多单色20像素乘20像素:比标准图标小!

我的.ICO文件不适合:它们都在2.5Kb左右



所以......很有可能你需要做的第一件事就是看你的数据库设计,以及如何将图像加载到数据库中 - 因为它非常非常错!

将其排序,然后我们可以查看显示图片......
If your column is VARBINARY(50) that I don;t hold out much hope of anything, image related.
even as a "raw data" image without any of the usual (and required) file format headers, that would only be an image 20 pixels by 20 pixels in monochrome at most: that's smaller than a standard icon!
And my .ICO files wouldn't fit: they are all around 2.5Kb

So...there is a good chance that the first thing you need to do is look at your database design, and how you are loading images into the DB - because it is very, very wrong!
Sort that out, and then we can look at displaying pictures...


这篇关于如何将varbinary数据类型绑定到winforms中的datagridview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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