2013-06-05 24 views
0

用戶控件代碼:將數據庫中的圖片顯示到每個usercontrol的picturebox?

private string lastName; 
    public string LastName 
    { 
     get { return lastName; } 
     set 
     { 
      lastName = value; 
      textBox1.Text = value; 
     } 
    } 

表格代號:

 using (SqlConnection myDatabaseConnection = new SqlConnection(myConnectionString.ConnectionString)) 
     { 
      myDatabaseConnection.Open(); 
      using (SqlCommand SqlCommand = new SqlCommand("Select LastName, Image from Employee", myDatabaseConnection)) 

      { 



       int i = 0; 
       SqlDataReader DR1 = SqlCommand.ExecuteReader(); 
       while (DR1.Read()) 
       { 
        i++; 
        UserControl2 usercontrol = new UserControl2(); 
        usercontrol.Tag = i; 
        usercontrol.LastName = (string)DR1["LastName"]; 
        flowLayoutPanel1.Controls.Add(usercontrol); 
       } 


      } 
     } 

有了上面的代碼,我可以從每個用戶控件的文本框數據庫顯示每個姓氏。如何將數據庫中的圖片顯示到每個usercontrol的picturebox?

這是我從數據庫中顯示單個圖像:

 using (SqlConnection myDatabaseConnection = new SqlConnection(myConnectionString.ConnectionString)) 
     { 
      myDatabaseConnection.Open(); 
      using (SqlCommand SqlCommand = new SqlCommand("Select LastName, Image from Employee where ID = @a", myDatabaseConnection)) 
      { 
       SqlCommand.Parameters.AddWithValue("@a", textBox1.Text); 
       DataSet DS = new DataSet(); 
       SqlDataAdapter da = new SqlDataAdapter(SqlCommand); 
       da.Fill(DS, "Images"); 
       var imagesTable = DS.Tables["Images"]; 
       var imagesRows = imagesTable.Rows; 
       var count = imagesRows.Count; 

       if (count <= 0) 
        return; 
       var imageColumnValue = 
        imagesRows[count - 1]["Image"]; 
       if (imageColumnValue == DBNull.Value) 
        return; 

       var data = (Byte[])imageColumnValue; 
       using (var stream = new MemoryStream(data)) 
       { 
        pictureBox1.Image = Image.FromStream(stream); 
       } 

      } 
     } 

回答

0

用戶控件代碼:

public void showpictures() 
    { 
     { 
      using (SqlConnection myDatabaseConnection = new SqlConnection(myConnectionString.ConnectionString)) 
      { 
       myDatabaseConnection.Open(); 
       using (SqlCommand SqlCommand = new SqlCommand("Select LastName, Image from Employee where LastName = @a", myDatabaseConnection)) 
       { 

        SqlCommand.Parameters.AddWithValue("@a", textBox1.Text); 
        DataSet DS = new DataSet(); 
        SqlDataAdapter da = new SqlDataAdapter(SqlCommand); 
        da.Fill(DS, "Images"); 
        var imagesTable = DS.Tables["Images"]; 
        var imagesRows = imagesTable.Rows; 
        var count = imagesRows.Count; 

        if (count <= 0) 
         return; 
        var imageColumnValue = 
         imagesRows[count - 1]["Image"]; 
        if (imageColumnValue == DBNull.Value) 
         return; 

        var data = (Byte[])imageColumnValue; 
        using (var stream = new MemoryStream(data)) 
        { 
         pictureBox1.Image = Image.FromStream(stream); 
        } 


       } 
      } 
     } 
    } 

形式

  while (DR1.Read()) 
      { 
       i++; 
       UserControl2 usercontrol = new UserControl2(); 
       usercontrol.Tag = i; 
       usercontrol.LastName = (string)DR1["LastName"]; 
       usercontrol.showpictures(); 
       flowLayoutPanel1.Controls.Add(usercontrol); 
      } 
1

要一次(在你的情況PictureBoxes)訪問多個控件使用foreach循環。

foreach (Control control in this.Controls) 
{ 
    if (control is PictureBox) 
    { 
     PictureBox pic = (PictureBox)control; 
     pic.Image = Image.FromStream(stream); //something similar, this will only load the same image to every PictureBox 
    } 
} 
1

爲您的UserControl構造函數重載以接收姓氏和圖像字節。然後你就可以在你的循環做到這一點:

  while (DR1.Read()) 
      { 
       i++; 
       UserControl2 usercontrol = new UserControl2((string)DR1["LastName"], (Byte[])DR1["Image"]); 
       usercontrol.Tag = i; 
       flowLayoutPanel1.Controls.Add(usercontrol); 
      } 

下面是更新用戶控件(與你的其他問題InvokeOnClick()的代碼)。請注意,在新的構造函數,我們如何創建從字節的圖像,並將其分配給圖片框:

public partial class UserControl2 : UserControl 
{ 

    private string lastName; 
    public string LastName 
    { 
     get { return lastName; } 
     set 
     { 
      lastName = value; 
      textBox1.Text = value; 
     } 
    } 

    public UserControl2(string LastName, byte[] data) 
    { 
     InitializeComponent(); 
     WireAllControls(this); 

     this.LastName = LastName; 
     try 
     { 
      using (var stream = new System.IO.MemoryStream(data)) 
      { 
       pictureBox1.Image = Image.FromStream(stream); 
      } 
     } 
     catch (Exception) 
     { 
      MessageBox.Show("Error Loading Image for " + LastName); 
     } 
    } 

    public UserControl2() 
    { 
     InitializeComponent(); 
     WireAllControls(this); 
    } 

    private void WireAllControls(Control cont) 
    { 
     foreach (Control ctl in cont.Controls) 
     { 
      ctl.Click += ctl_Click; 
      if (ctl.HasChildren) 
      { 
       WireAllControls(ctl); 
      } 
     } 
    } 

    private void ctl_Click(object sender, EventArgs e) 
    { 
     this.InvokeOnClick(this, EventArgs.Empty); 
    } 

} 
+0

Did'nt處理空值。但是還是特別感謝你爲wirecontrols。 :) –

+0

很容易事先檢查null。如果沒有關聯的圖像,您可以創建一個只接受姓氏的不同重載.... –