Adding images to bd using classes in C #

0

I'm doing a project that has classes, for example, a user class, where you have: code, name, password and photo and if you have the method of user input:

    public string Inserir()
    {
        return "insert into Usuario(Username,Senha,Foto) values ('" + _username + "','" + _senha + "','" + _vetorImagens + "')";
    }

}

}

In the registration form, I have a function that takes the chosen image in an open dialog and fills in the picture box:

private void CarregaImagem()
    {

        try
        {
            this.Fd1.ShowDialog(this);
            string strFn = this.Fd1.FileName;

            if (string.IsNullOrEmpty(strFn))
                return;

            this.pbImagem.Image = Image.FromFile(strFn);


        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }

and after that click on the save button if you have another function that should take the information, save in the attributes of my User class and finally save in the BD:

private void btCadastrar_Click(object sender, EventArgs e)
    {
        if (tbCsenha.Text != tbSenha.Text)
        {
            MessageBox.Show("As senhas não batem ");
            tbSenha.Clear();
            tbCsenha.Clear();
            tbSenha.Focus();
        }
        else
        {
            cUsuario cUs = new cUsuario();
            MemoryStream ms = new MemoryStream();
            cUs.VetorImagens = null;
            pbImagem.Image.Save(ms, ImageFormat.Jpeg);
            cUs.VetorImagens = ms.ToArray();



            cUs.Username = tbUser.Text;
      cUs.Senha = tbSenha.Text ;





            if (con.Cadastro(cUs.Inserir()) == true)
            {
                MessageBox.Show("Cadastrado com sucesso");
                tbUser.Clear();
                pbImagem.Image = null;
                tbSenha.Clear();
                tbCsenha.Clear();
                tbUser.Focus();
            }







        }
    }

This function saves the password and name correctly, however the photos always have the value 0x53797374656D2E427974655B5D. If anyone can help me, I've been breaking my head with this for some time, I've never manipulated images in a DB, even with classes.

    
asked by anonymous 16.11.2016 / 22:28

1 answer

0

Gabriel, to make the inserts, use data access libraries like Microsoft Enterprise Library , Microsoft Entity Framework , Dapper or simply the < strong> ADO pure. In the database, for the field that holds the picture, use VARBINARY type and the property of its class an byte byte [] .

And to read the information from the table, an array of bytes will also be returned to the photo field.

I hope you have a way forward.

    
17.11.2016 / 10:51