2015-04-05 73 views
0

我試圖用列表框中選擇事件來檢索訪問數據庫映像檢索圖像到一個PictureBox

我用下面的代碼將數據保存到數據庫中:

command.CommandText = "insert into EmployeeInfo (FirstName,LastName,Pay,Pic) values ('" + txt_fname.Text + "' , '" + txt_lname.Text + "' , '" + txt_pay.Text + "' , @productpic)"; 
      MemoryStream stream = new MemoryStream(); 
      pb1.Image.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg); 
      byte[] pic = stream.ToArray(); 
      command.Parameters.AddWithValue("@productpic", pic); 

我使用下面的代碼檢索數據是:

private void listBox1_SelectedIndexChanged(object sender, EventArgs e) 
     { 
      connection.Open(); 

      OleDbCommand command = new OleDbCommand(); 
      command.Connection = connection; 
      string query = "select * from EmployeeInfo where FirstName = '" + listBox1.Text +"'"; 
      command.CommandText = query; 
      OleDbDataReader reader = command.ExecuteReader(); 
      while (reader.Read()) { 
       list_fname.Text = reader["FirstName"].ToString(); 
       list_lname.Text = reader["LastName"].ToString(); 
       list_dob.Text = reader["DoB"].ToString(); 
      } 
      connection.Close(); 
     } 

現在我所要做的是,檢索與每一行相關聯的圖片,字段名是產品圖成pictureb並用其他數據在while循環中顯示它。我看到了其他人提出的幾個問題,但每個人都使用datagridview來檢索數據,在我的情況下,我正在嘗試在listbox select事件中執行此操作。

任何形式的幫助將不勝感激。提前致謝 。

+0

請參閱本http://stackoverflow.com/a/2217617/4059942 – 2015-04-05 19:19:40

+0

我已經找到了解決方案。謝謝 。 – 2015-04-05 19:23:57

+0

然後請在此處添加您的解決方案作爲答案,以便其他用戶稍後可以找到答案。 – Marco 2015-04-05 19:26:51

回答

0

調用此函數在while循環列表框中選擇事件:

void loadpicture() 
      { 


       OleDbCommand command = new OleDbCommand(); 
       command.Connection = connection; 
       command.CommandText = "select pic from EmployeeInfo where FirstName = '" + listBox1.Text + "'"; 
       OleDbDataAdapter da = new OleDbDataAdapter(command); 
       DataSet ds = new DataSet(); 
       da.Fill(ds); 
       byte[] content = (byte[])ds.Tables[0].Rows[0].ItemArray[0]; 
       MemoryStream stream = new MemoryStream(content); 
       pb1.Image = Image.FromStream(stream); 

      }