Visual studio 2017 c # BLL & DAL

1

I have a problem with saving and taking an image of the database. I would like to know a way to enter the image in the database taking into account that the taxpayer number is the primary key and then in another form can use code to search it there is datagridview and for 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);

    }

    
asked by anonymous 10.05.2018 / 12:51

1 answer

1

I suggested a quick and easy approach to keeping the URL of the image in the Database. You do not need to save the image itself, you just need to make sure it exists on the path you put in the DB. After the C # side, in the PictureBox you only need to place 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. So absolute paths will not work. You have to use a relative path.

    
11.05.2018 / 16:22