Insert image into SQL SERVER2008 database using image path

6

I'm starting in C # and I'm having trouble inserting into a picturebox an image as in the image below:

The above screen is a photo record, and the browse image button works, but I do not know how to capture the path of the image to write to the bank, which is what I want to record.

And also, how to bring this image to query the registry.

As for the database, I'm with Sql2008.

    
asked by anonymous 13.12.2014 / 19:26

2 answers

2

I recommend sending the image to a folder on the server, and in the database only save the address of that folder.

Shipping:

if (Arquivo.HasFile)
{
     if (File.Exists(HttpContext.Current.Server.MapPath("../files/" + Arquivo.FileName)))
         Arquivo.SaveAs(Server.MapPath("../files/" + Arquivo.FileName));

}

Example of address saved in the database: ~/files/24054_04.doc

It's lighter, less work, and more practical than burning the image on the bench.

    
17.12.2014 / 19:13
1

So I suggest you use a variable in your form to save the image path, my idea would be something of the tip

 string caminhoImagem = "";

    private void button1_Click(object sender, EventArgs e)
    {
        if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
        {
            Image image = Image.FromFile(openFileDialog1.FileName);
            caminhoImagem = openFileDialog1.FileName;

            pictureBox1.Image = image;
            pictureBox1.Height = image.Height;
            pictureBox1.Width = image.Width;
        }
    }

Then, just use the caminhoImage variable in your save method.

    
13.12.2014 / 20:02