Save Image in SQL Server using PictureBox

0

Good morning, I am working with an application within C #, which works as an inventory for the teams within the company. I'm working inside Windows Forms.

What I intend to do is keep the records of each computer, with its respective image for easier location.

It is worth mentioning that the field of the database declares it as image:

This is how I am loading the image inside the PictureBox:

 private void button1_Click(object sender, EventArgs e)
        {

            OpenFileDialog getImage = new OpenFileDialog();
            getImage.InitialDirectory = "C:\";
            getImage.Filter = "Archivos de Imagen (*.jpg)(*.jpeg)|*.jpg;*.jpeg|PNG(*.png)|*.png";
            if (getImage.ShowDialog() == DialogResult.OK)
            {
                pictureBox1.ImageLocation = getImage.FileName;
                pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
            }
            else
            {
            }
        }

This is how I intend to put them in the database:

 private void button2_Click(object sender, EventArgs e)
        {
            if (textBox1.Text != "" || comboBox1.Text != "" || comboBox2.Text != "")
            {

                SqlCommand agregar = new SqlCommand("Insert Into Equipos values ((RTRIM(LTRIM(" + textBox1.Text + "))), (RTRIM(LTRIM('" + variable + "'))),  (RTRIM(LTRIM('" + textBox3.Text + "'))) ,(RTRIM(LTRIM('" + textBox2.Text + "'))), (RTRIM(LTRIM('" + comboBox1.Text + "'))), (RTRIM(LTRIM('" + comboBox2.Text + "'))), (RTRIM(LTRIM('" + textBox10.Text + "'))), @Imagen)", cadena);
                try
                {
                    cadena.Open();

                    agregar.ExecuteNonQuery();
                    MessageBox.Show("Agregado");

                    Variables.equipos1 = textBox1.Text;
                    Variables.equipos2 = variable;
                    Variables.equipos3 = textBox2.Text;
                    Variables.equipos4 = comboBox1.Text;
                    Variables.equipos5 = comboBox2.Text;
                    Variables.equipos6 = textBox10.Text;
                    Variables.dialogo = true;
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Error" + ex);
                }
                finally
                {
                    cadena.Close();
                    textBox1.Clear();
                    textBox2.Clear();
                }

            }
            else
            {
                cadena.Close();
                MessageBox.Show("No puede dejar vacio");

                textBox2.Clear();
            }
        }

The error that shows me when executing it like this is the following:

    
asked by Ezequie Lopez 15.10.2018 в 20:12
source

1 answer

1

Usually when I work with winforms I convert the image to an array of bytes with a function as I show you below:

    public byte[] ImageToByteArray(System.Drawing.Image imagen)
    {
        MemoryStream ms = new MemoryStream();
        imagen.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
        return ms.ToArray();
    }

Now to get the value and assign it, you could do so:

  byte[] byteArrayImagen = ImageToByteArray(PictureBox1.Image)
  SqlCommand command = new SqlCommand();
  command.Text="INSERT INTO TABLA (CAMPO) values (@imagen)";
  command.Parameters.AddWithValue("@imagen",byteArrayImagen);
  command.ExecuteNonQuery();

I hope you find it useful.

    
answered by 15.10.2018 / 22:11
source