2013-05-19 17 views
1

这是我遇到的问题。我有一个包含几列的SQL Server表。其中之一是image数据类型。图像保存为二进制数据。如何在c中的datagridview中选择记录时显示存储在数据库表中的图像#

在窗体上,我有一个dataGridView,显示表中的3列。他们都没有包含图像。当我点击dataGridView中的一条记录时,我希望图像显示在同一个表单中。

这是我到目前为止的代码:

public void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e) 
{ 
     SqlConnection cn = new SqlConnection(@" Data Source=HOME-D2CADC8D4F\SQL;Initial Catalog=motociclete;Integrated Security=True"); 

     if (e.RowIndex >= 0) { 
      DataGridViewRow row = dataGridView1.SelectedRows[0]; 
      textBox1.Text = row.Cells["anf"].Value.ToString(); 
      textBox2.Text = row.Cells["putere"].Value.ToString(); 
      textBox3.Text = row.Cells["caprez"].Value.ToString(); 
      textBox4.Text = row.Cells["greutate"].Value.ToString(); 
      textBox5.Text = row.Cells["stoc"].Value.ToString(); 
      textBox6.Text = row.Cells["pret"].Value.ToString(); 
      textBox7.Text = row.Cells["garantie"].Value.ToString(); 

      SqlCommand cmd = new SqlCommand("select poza from motociclete where codm '" + dataGridView1.SelectedRows + "'", cn); 

      cn.Open(); 

      try 
      { 
       SqlDataReader dr = cmd.ExecuteReader(); 

       if (dr.Read()) 
       { 
        byte[] picarr = (byte[])dr["poza"]; 
        MemoryStream ms = new MemoryStream(picarr); 
        ms.Seek(0, SeekOrigin.Begin); 
        pictureBox1.Image = Image.FromStream(ms); 
       } 
      } 
      catch (Exception ex) 
      { 
       MessageBox.Show(ex.Message); 
      } 
      finally { 
       cn.Close(); 
      } 
     } 
    } 

我认为这是一个语法错误,但我不知道在哪里或如何解决它。

回答

0

您在内存流中丢失了字节数组。因此,通过一些填充字节数组的内存流这样

byte[] picarr = (byte[])dr["poza"]; 
MemoryStream ms = new MemoryStream(picarr); 
pictureBox1.Image = Image.FromStream(ms); 
+0

我做了,但它仍然给我同样的错误:附近'不正确的语法附近'system.windows.forms.datagridviewselectedcellcollection' – stefan9976

+0

'但我不我认为这个错误与这个图片问题有关。看起来你可能在别的地方有错误。 – Sachin

+0

你有什么想法在哪里?文本框正在工作,所以我认为它从sql表中读取的很好。 – stefan9976

0

从下面这段代码的:

 SqlDataReader dr; 
     try 
     { 
      dr = cmd.ExecuteReader(); 
      if (dr.Read()) 
      { 
       byte[] picarr = (byte[])dr["poza"]; 
       MemoryStream ms = new MemoryStream(); 
       ms.Seek(0, SeekOrigin.Begin); 
       pictureBox1.Image = Image.FromStream(ms); 
      } 
     } 

注意到,您的MemoryStream毫秒=新的MemoryStream()提供了一个MemoryStream没有数据。然后,使用空白的MemoryStream作为图像的来源。你应该用刚刚在前一行读到的字节填充内存流。

+0

我做了,但它仍然给出错误:附近'system.windows.forms.datagridviewselectedcellcollection'附近的语法不正确' – stefan9976

+0

在你原来的文章中,你说你认为这是一个语法错误,但没有指定一个。现在你正在指定一个语法错误。除了您使用空白MemoryStream的事实之外,该语法错误。在语法错误中引用了哪些行号,以及您的发布代码是与哪些行对应的? – 2013-05-19 15:46:49

+0

这不是一个语法错误。我错了。它是一个异常的消息框。 – stefan9976

1

我建议你将图像保存在一些临时文件夹中,然后使用ImageLocation属性显示它。这种方法的

if (dr.Read()) 
{ 
     byte[] picarr = (byte[])dr["poza"]; 
     string imagePath = "path to temp folder/image123.jpg"; 
     File.WriteAllBytes(imagePath ,picarr); 
     pictureBox1.ImageLocation = imagePath; 
} 

主要优点是可以轻松实现某种形式的缓存,这样,如果他们最近已经选择你不必从数据库中每一次检索图像。如果你的应用程序很忙,这会对应用程序的性能产生很大的影响。

0

这是一个OLEDB连接代码,但您可以从中获得一些想法。图像是以byte []形式保存的位图。

int idImagen = Convert.ToInt32(listImagenes.Text.Split(':')[0]); 
      ImageConverter ic = new ImageConverter();  
OleDbConnection cnx = new OleDbConnection(); 
      cnx.ConnectionString = "Your Conection String"; 
      OleDbCommand cmdx = new OleDbCommand(); 
      cmdx.Connection = cnx; 
      cmdx.CommandText = "SELECT IC.Foto FROM ImageCriminal IC WHERE IC.IdImage=" + idImag; 
      cnx.Open(); 
      byte[] imgRaw = (byte[])cmdx.ExecuteScalar(); //<- Here the byte[] stored in the database gets recovered and stored in imgRaw variable. 
      cnx.Close(); 
      Image imagen = (Image)ic.ConvertFrom((byte[])imgRaw); // This has been working for me from weeks ago!! It's very important to save the files as byte[] inside the DB. 
      picBox.Image = imagen; 

此代码是从另一种方法调用的。的行动是: - 搜索每张图片哪个PictureId等于我

相关问题