Skip to content

Upload image to SQL table

Lynn Emery

Asked Oct 14, 2013, 6:55 AM EDT3 answers
Share
LinkedInX
Hi, I wish to upload an image to an sql table.
So far I have used an open file dialogue and a picturebox where the user can browse for images and preview it. The code I have used for this is ;

Java
//open the BtnBrowse dialog
                    OpenFileDialog BrowseDialogue = new OpenFileDialog();

            //Select filters on which file types to upload
            BrowseDialogue.Filter = "Images Files (*.jpg; *.jpeg; *.gif; *.bmp; *.png:)|*.jpg; *.jpeg; *.gif; *.bmp; *png;";
                    if (BrowseDialogue.ShowDialog() == DialogResult.OK)
                    {
            //Preview the image in a picturebox
                        pictureBox1.Image = new Bitmap(BrowseDialogue.FileName);
            //Display image path in textbox
                        textBox1.Text = BrowseDialogue.FileName;

The above code all works fine but I now want  to have an upload button where the image in the picturebox is uploaded to the database, I understand that there are two ways of doing this via file stream or memory stream.
The table name I wish to upload to is contents and the field name is image.
Could someone provide me with some information of how to go about this?
Many thanks for your help.

2 Answers

Know the answer? Post it. Somebody with the same question will find it here.

  • 0
    Hi

    Open an Image : 

    TypeScript
     private void button1_Click(object sender, EventArgs e)
            {
                OpenFileDialog openFD = new OpenFileDialog();
                openFD.Filter = "All Files | *.jpg;";
                DialogResult dr = openFD.ShowDialog();
                pictureBox1.Image = Image.FromFile(openFD.FileName);
                pictureBox1.LoadAsync(openFD.FileName);
            }

    Button Save :

    Java
     MemoryStream ms = new MemoryStream();
                byte[] pic = new byte[ms.Length];
                ms.Position = 0;
                ms.Read(pic, 0, pic.Length);
    
    
     SqlConnection sqlcon = new SqlConnection();
                SqlCommand sqlcmd = new SqlCommand();
                sqlcon.ConnectionString = "Data Source=sakthi\\sqlexpress; Initial Catalog=LoginDetails; Integrated Security=True";
                sqlcon.Open();
                sqlcmd.Connection = sqlcon;
                sqlcmd.CommandText = "insert into emp(empcode, name, image) values (@empcode, @name, @image)";
                sqlcmd.Parameters.AddWithValue("@empcode", txtEmpCode.Text);
                sqlcmd.Parameters.AddWithValue("@name", txtName.Text);
                sqlcmd.Parameters.AddWithValue("@image", pic);
                sqlcmd.ExecuteNonQuery();
                sqlcon.Close();
                MessageBox.Show("Saved");
                Close();
    0
  • 0
    Hi, thank you for your reply.

    For the code which is related to the SQL connection, i am a little confused. Do I need to define the security etc?

    sqlcon.ConnectionString = "Data Source=sakthi\\sqlexpress; Initial Catalog=LoginDetails; Integrated Security=True";

    Could please explain further?

    I have a data source called ProductImages and are using SQL server 2003.

    Thank you once again.
    0
  • 1 short reply
    • 0
      yes..
      0