Visual studio 2017 c# BLL& DAL

Asked

Viewed 66 times

1

I have a problem with how to save and take an image from the database. I would like to know a way to enter the image in the database taking into account that the number of contributor is the primary key and that after another form can use code to search for it there datagridview and put the information in a picturebox

public class BLL {
    public class Imagem {
        static public object loadpic(int Numero_de_contribuinte) {
            DAL dal = new DAL();
            SqlParameter[] sqlParams = new SqlParameter[] {
                new SqlParameter("@Numero_de_contribuinte", Numero_de_contribuinte),
            };
            return dal.executarScalar("select Imagem from Imagem where @Numero_de_contribuinte=@Numero_de_contribuinte", sqlParams);

        }
        static public DataTable Load() {
            DAL dal = new DAL();
            return dal.executarReader("select * from Imagem", null);
        }
        //Insere a imagem relativa ao numeroo de contribuinte
        static public int insertImagem(byte[] img, string Numero_de_contribuinte) {
            DAL dal = new DAL();
            SqlParameter[] sqlParams = new SqlParameter[] {
                new SqlParameter("@img", img),
                new SqlParameter("@Numero_de_contribuinte", Numero_de_contribuinte)

            };

            return dal.executarNonQuery("INSERT into Imagem (Imagem,Numero_de_contribuinte) VALUES(@img,@Numero_de_contribuinte)", sqlParams);
        }
    }   

    private void button2_Click(object sender, EventArgs e) {

        // Code Snippet
        MemoryStream ms = new MemoryStream();

        pictureBox1.Image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);

        byte[] buff = ms.GetBuffer();
        int g = BLL.Imagem.insertImagem(buff, textBox2.Text);

        /*  OpenFileDialog open = new OpenFileDialog();
              open.Filter = "Image Files(*.jpg; *.jpeg; *.gif; *.bmp)|*.jpg; *.jpeg; *.gif; *.bmp";
              if (open.ShowDialog() == DialogResult.OK)
              {
                  Bitmap bit = new Bitmap(open.FileName);
                  pictureBox1.Image = bit;
                  int a = BLL.Imagem.insertImagem(bit, textBox1.Text);


              }*/

    }
    private void button3_Click(object sender, EventArgs e) {
        int o = Convert.ToInt16(textBox2.Text);

        BLL.Imagem.loadpic(o);

    }

inserir a descrição da imagem aqui

  • Hello Pedro, do not post images of your code, put the text of your code in the question.

  • I’m new to this,

  • No need to wait for the next one just click on the "edit" link at the end of the question and change :)

  • 1

    thanks ja esta I’ll only put at least the design image to get an idea :)

  • Take a look at this link to get an idea of how to do: https://www.c-sharpcorner.com/UploadFile/deepak.sharma00/how-to-save-images-in-mysql-database-using-C-Sharp/

  • I’ll take a look around

  • What exactly is your doubt?

  • I would like to be able to save the image in the database . , and then be able to demonstrate the image in a picturebox by clicking on the datagridview. So far I’ve only been able to put the data that takes letters and numbers to be saved.

Show 3 more comments

1 answer

1


I would suggest an easy and quick approach to doing that is to guard the URL image in the Database. You don’t need to save the image itself, you just need to make sure it exists on the path you put in the DB. Then next to C#, in Picturebox you only need to put the URL in the method Picturebox.Load(string URL) Method.

The only thing you need to be aware of is if the location of the image is not the same. Then absolute paths will not work. You have to use a relative path.

Browser other questions tagged

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