Upload image to picturebox and write to database

2

I'm now in the part of the movies of the video club, where I have these fields.

AndIwantedtouploadtheimagetothepicturabox(it'salreadyworking)andwhenImakeitnewitwillbesavedinthedatabase(Icreatedanimagefield).Isitpossible?

ItriedtodoasIdofortxtboxs:

cmd.Parameters.AddWithValue("@classimdb", txtClassIMDB.Text);

Placing instead of txtClassIMDB.text' tentei picturebox1.image '. But of course it does not work.

    
asked by anonymous 21.05.2017 / 22:45

3 answers

1

Code I used and worked:

                FileStream Stream = new FileStream(imgLocation, FileMode.Open, FileAccess.Read);
            BinaryReader binary = new BinaryReader(Stream);
            img = binary.ReadBytes((int)Stream.Length);
            if (txtNameMovie.Text != "" && txtIDMovie.Text != "")
            {
                sqlCon.Open();
                SqlCommand cmd = sqlCon.CreateCommand();
                cmd.CommandType = CommandType.Text;
                cmd.CommandText = "INSERT INTO Filmes(IdFilme,Titulo,Realizador,Genero,Ano,ClassifIdade,ClassfImdb,Formato,Capa) values(@id,@titulo,@realizador,@categoria,@ano,@classidade,@classimdb,@formato,@capa)";
                cmd.Parameters.AddWithValue("@id", txtIDMovie.Text);
                cmd.Parameters.AddWithValue("@titulo", txtNameMovie.Text);
                cmd.Parameters.AddWithValue("@realizador", txtRealizador.Text);
                cmd.Parameters.AddWithValue("@categoria", txtCatMovies.Text);
                cmd.Parameters.AddWithValue("@ano", txtAnoMovie.Text);
                cmd.Parameters.AddWithValue("@classidade", txtClassIdade.Text);
                cmd.Parameters.AddWithValue("@classimdb", txtClassIMDB.Text);
                cmd.Parameters.AddWithValue("@formato", cbFormato.Text);
                cmd.Parameters.Add(new SqlParameter("@capa", img));
                int x = cmd.ExecuteNonQuery();
                sqlCon.Close();
                disp_data();
                ClearData();
                MessageBox.Show("Filme gravado com sucesso!", "Operação Realizada com Sucesso", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }
    
24.05.2017 / 22:38
4

To save a photo in BD, one of the means I know is to transform the photo into an Array of byte , for that I use the method below:

          if (Pic.Image != null)
          {
               using (MemoryStream stream = new MemoryStream())
               {
                    Pic.Image.Save(stream, ImageFormat.Jpeg);                                        

                    byte[] Bfoto = stream.ToArray();

                    Classes.Cadastro.Crm.Analise_CRM Cad_Foto = new Classes.Cadastro.Crm.Analise_CRM();

                    Cad_Foto.Cad_Foto_Anal_Crm(textEdit8.Text, Bfoto, Pic.Name, Pic.Properties.ZoomPercent);
                }

                 Pic.Image.Dispose();
                 Pic.Image = null;
          }

Now, in the database, use [varbinary] in the column where you will save the photos:

CREATE TABLE [dbo].[Crm_Fotos]
(
[id] [int] IDENTITY(1,1) NOT NULL,
[Bfoto] [varbinary](max) NULL,
[item] [varchar](150) NULL,
[foto_seq] [varchar](200) NULL
(

Just pass the parameter like any other data, only difference is using:

cmd.Parameters.Add("@Bfoto", SqlDbType.VarBinary).Value = Bfoto;

Questions with answers that can help:

How to return multiple rows with byte array of sql Server

Convert and save photo to BD

It is wrong to save byte of images in the database?

    
21.05.2017 / 23:24
0
   if (Pic.Image != null)
      {
           using (MemoryStream stream = new MemoryStream())
           {
                Pic.Image.Save(stream, ImageFormat.Jpeg);                                        

                byte[] Bfoto = stream.ToArray();

                Classes.Cadastro.Crm.Analise_CRM Cad_Foto = new Classes.Cadastro.Crm.Analise_CRM();

                Cad_Foto.Cad_Foto_Anal_Crm(textEdit8.Text, Bfoto, Pic.Name, Pic.Properties.ZoomPercent);
            }

             Pic.Image.Dispose();
             Pic.Image = null;
      }

This part goes to:

            sqlCon.Open();
            SqlCommand cmd = sqlCon.CreateCommand();
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = "INSERT INTO Filmes(IdFilme,Titulo,Realizador,Genero,Ano,ClassifIdade,ClassfImdb,Capa) values(@id,@nome,@realizador,@categoria,@ano,@classidade,@classimdb,@capa)";
            cmd.Parameters.AddWithValue("@id", txtIDMovie.Text);
            cmd.Parameters.AddWithValue("@nome", txtNameMovie.Text);
            cmd.Parameters.AddWithValue("@realizador", txtRealizador.Text);
            cmd.Parameters.AddWithValue("@categoria", txtCatMovies.Text);
            cmd.Parameters.AddWithValue("@ano", txtAnoMovie.Text);
            cmd.Parameters.AddWithValue("@classidade", txtClassIdade.Text);
            cmd.Parameters.AddWithValue("@classimdb", txtClassIMDB.Text);
    
23.05.2017 / 21:16