Add images in the bd using classes in c#

Asked

Viewed 108 times

0

i am doing a project that has classes, for example, a User class, where you have :code, name, password and photo and you have the user input method:

    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 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 by clicking the save button if there is 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 the name correctly, however the photos always get the value 0x53797374656D2E427974655B5D. If anyone can help me, I’ve been racking my brain with this for a while, I’ve never manipulated images in a comic book, even with classes.

  • You have to necessarily save the images in the bank ? could not save only the way ?

1 answer

1


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

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

I hope you have a way to go.

Browser other questions tagged

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