2013-05-19 18 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等於我

相關問題