Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

mysql - How to retrieve image from database in c#

an error shows saying that I have invalid parameters for this code... can anyone tell me whats wrong? Im supposed to get the image assigned to the clientID.

private void button1_Click(object sender, EventArgs e)
{
        MySqlConnection conn = new MySqlConnection(mycon);
        MySqlCommand cmd = new MySqlCommand("SELECT clientImage FROM client WHERE clientID='" + label2.Text + "'", conn);

        conn.Open();
        MySqlDataReader myReader = null;
        myReader = cmd.ExecuteReader();

        while (myReader.Read())
        {
            byte[] imgg = (byte[])(myReader["clientImage"]);
            if (imgg == null)
            {
                pictureBox1.Image = null;
            }
            else
            {
                MemoryStream mstream = new MemoryStream(imgg);
                pictureBox1.Image = System.Drawing.Image.FromStream(mstream);
            }
        }
        conn.Close();
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

This piece of code might come in handy. I have tried it.

byte[] imagedata = (byte [])dataGridView1[4, dataGridView1.SelectedRows[0].Index].Value;
            using (System.IO.MemoryStream ms = new System.IO.MemoryStream(imagedata, 0, imagedata.Length))
            {
                ms.Write(imagedata, 0, imagedata.Length);
                //Set image variable value using memory stream.
                image = Image.FromStream(ms, true );
            }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...