图像从数据库到datagridview错误 [英] Image from Database To datagridview error

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

问题描述

你好,我有这个代码

//获取图像字节数组的部分

Hello, i have this code

//the part for getting the byte array of image

MemoryStream image=new MemoryStream() ;
Bitmap  imgO = new Bitmap(open.FileName);
imgO.Save(image, ImageFormat.Bmp);


//填充数据库部分


//the fill db part

sqlconn.Open();
           String sSQLCommand = "INSERT INTO ImageTable (CodImage,NameImage,Image,Distance,Split,Energy,Contrast,Entropy,Omogenity,Variance)" + "VALUES('"+Bob+"', '" + name + "','" + image.ToArray() + "','5','1','" + ai.Get_energy() + "','" + ai.Get_contrast() + "','" + ai.Get_entropy() + "','" + ai.Get_omogenity() + "','" + ai.Get_variance() + "' );";

           SqlCommand sqlc = new SqlCommand(sSQLCommand, sqlconn);
           sqlc.ExecuteNonQuery();
           sqlconn.Close();


//对于视图部分,我有这个(在这一部分中,我认为我有一个错误)


//and for view part i have this(in this part i think i have an error)

DataTable dt = new DataTable();
string sqlSelect = "select * from ImageTable";
SqlCommand cmd = new SqlCommand(sqlSelect, sqlconn);
  using (cmd)
           {
               SqlDataAdapter adp = new SqlDataAdapter(cmd);
               adp.Fill(dt);

           }
dataGridView1.DataSource = dt;


//sqlconn其连接字符串;

我认为它丢失了一些东西,我找不到它,在ImageTable(数据库表)中,我有一个列,用于存储图像类型的数据,我将图像转换为字节数组,并成功地将其存储到数据库中,但是当我想查看图像时在datagridview中从数据库中抛出错误(类似参数无效).有人可以帮我弄清楚我哪里出错了,或者有另一种方法可以做同样的事情?


//sqlconn its the connection string;

i think its missing something and i cant find what, in ImageTable (the table of database) i have a collumn that stores data of type image, i converted the image to bytearray and succesfully stored it into database but when i want to view the images from database in a datagridview it throws me an error (something like parameter not valid). Can someone help me figureout where i made the mistake or there is another way to do the same thing?

推荐答案

请参阅此链接

http://stackoverflow.com/questions/9069742/store-image-in- database-and-retrieve-it [ ^ ]

给出类似问题的解决方法

但是我有一个建议.您可以尝试使用Visual Studio的AddNewDataSource 向导尝试键入DataSet ,而不是手动创建DataTableSqlCommandSqlDataAdapter .

而这篇文章
用于从DataBase和使用反射将DataTable保存到数据库中 [
Please see this link

http://stackoverflow.com/questions/9069742/store-image-in-database-and-retrieve-it[^]

at which, solution to similar problem is given

But I have a suggestion. Instead of creating DataTable, SqlCommand, SqlDataAdapter manually, you can try Typed DataSet using the AddNewDataSource wizard of Visual Studio.

And this article
General purpose class to fill DataTable(s) from DataBase and to save DataTable(s) to DataBase using reflection[^]

may be helpful to easily read data from database and save data to database.
In the example given in this article, in the Categories table of Northwind database, there is Picture field storing the image. I think it is relevant to your question.


String InsertImgTableCommand = "INSERT INTO ImageTable (CodImage,NameImage,Image,Distance,Split,Energy,Contrast,Entropy,Omogenity,Variance) VALUES(@CodImage, @NameImage,@Image,@Distance,@Split,@Energy,@Contrast,@Entropy,@Omogenity,@Variance )";
           SqlCommand sqlc = new SqlCommand(InsertImgTableCommand, sqlconn);
           sqlc.Parameters.AddWithValue("@CodImage", codimg.ToString());
           sqlc.Parameters.AddWithValue("@NumeImage", name);
           sqlc.Parameters.AddWithValue("@Image", image.ToArray());
           sqlc.Parameters.AddWithValue("@Distance", distance);
           sqlc.Parameters.AddWithValue("@Split", 1);
           sqlc.Parameters.AddWithValue("@Energy", ai.Get_energy());
           sqlc.Parameters.AddWithValue("@Contrast", ai.Get_contrast());
           sqlc.Parameters.AddWithValue("@Entropy", ai.Get_entropy());
           sqlc.Parameters.AddWithValue("@Omogenity", ai.Get_omogenity());
           sqlc.Parameters.AddWithValue("@Variance", ai.Get_variance());

           sqlconn.Open();
           try
           {
               sqlc.ExecuteNonQuery();
           }
           catch (Exception ex)
           {
               MessageBox.Show(ex.Message);
           }
           finally
           {

               sqlconn.Close();
           }




我设法在插入命令中找到该错误及其错误.而且应该是这样的




I managed to find the error and its in the insert command. And is should`ve been like this


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

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