Show the picture from database to each usercontrol's picturebox?

此生再无相见时 提交于 2019-12-08 12:18:45

问题


Usercontrol Code:

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

Form Code:

        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);
                }


            }
        }

With the code above i can show each LastName from database in each usercontrol's textbox. How to show the picture from database to each usercontrol's picturebox?

This is how I display a single image from database:

        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);
                }

            }
        }  

回答1:


To access multiple controls at once (PictureBoxes in your case) use foreach loop.

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
    }
}



回答2:


Make an overload for your UserControl constructor to receive both the last name and the image bytes. Then you can do this in your loop:

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

Here's the updated UserControl (with the InvokeOnClick() code from your other question). Note in the new constructor how we are creating an image from the bytes and assigning it to the PictureBox:

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); 
    }

}



回答3:


Usercontrol Code:

    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);
                    }


                }
            }
        }
    }

Form

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


来源:https://stackoverflow.com/questions/16935334/show-the-picture-from-database-to-each-usercontrols-picturebox

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!