image and write to the sql database in the image field

Asked

Viewed 67 times

1

Good afternoon. I have a little problem with my code, and I need some help. I am inserting an image in my form in an image field, now I need to take this image and write an image field in a field in sql server. How can I make this recording.

below my code where I search the image on pc and play in Form.

 private void btn_imagem_Click(object sender, EventArgs e)
    {
        open_imagem.FileName = "";
        open_imagem.Title = "Imagem do Chamdo";
        open_imagem.Filter = "JPEG | *.JPG | PNG | *.PNG";
        open_imagem.ShowDialog();
    }

    private void open_imagem_FileOk(object sender, CancelEventArgs e)
    {
        pcb_imagem.Image = Image.FromFile(open_imagem.FileName);
    }

Now follow the code of Insert.

 public String cadchamado(String usuario, String departamento, String tipo, String comentario, DateTime data)
    {
        if (usuario != "")
        {
            tem = false;
            cmd.CommandText = "insert into tbl_chamados values(@GM_USUARIO, @GM_DEPARTAMENTO, @GM_TIPO, @GM_COMENTARIO, @GM_DTINICIAL)";
            cmd.Parameters.AddWithValue("@GM_USUARIO", usuario);
            cmd.Parameters.AddWithValue("@GM_DEPARTAMENTO", departamento);
            cmd.Parameters.AddWithValue("@GM_TIPO", tipo);
            cmd.Parameters.AddWithValue("@GM_COMENTARIO", comentario);
            cmd.Parameters.AddWithValue("@GM_DTINICIAL", data);
            try
            {
                cmd.Connection = con.conectar();
                cmd.ExecuteNonQuery();
                con.desconectar();
                this.mensagem = "Chamado incluído com sucesso!!";
                tem = true;
            }
            catch (SqlException)
            {
                throw;

                //this.mensagem = "Erro ao inserir chamado verifique!!";
            }
        }
        else
        {
            this.mensagem = "Existem campos em branco por favor preencer, obrigado!!";
        }
        return mensagem;
    }

I thank you in advance

  • The winforms is C#?

  • yes I have a form in c#

  • 1

    I am from the java world and work with sql server database, I believe the concept is the same. This type of data (image) maps itself in its class as byte array (byte[]). Then I believe you will need to transform this file into a byte[] and then save. Check out these examples, I think it will help you: https://www.codeproject.com/Questions/736961/Save-file-in-sql-database-in-csharp-winform

  • Recording images in the database is a bad idea... In your Index I do not see the parameter for the image blob... update your question showing how you are doing and where you are finding the error. [MCVE]

  • OK thanks for the help Adriano Gomes, I managed to make the recording.

  • @So it would be interesting for you to post the answer with your solution or close the question

Show 1 more comment
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.