Insert image into SQL database SERVER2008 using image path

Asked

Viewed 1,179 times

6

I’m starting in C# and I’m having difficulty inserting in a picturebox an image as in the image below:

inserir a descrição da imagem aqui

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

And also, how to bring this image to consult the register.

As for the database, I’m with Sql2008.

2 answers

2

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

Sending:

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 bank: ~/files/24054_04.doc

It is lighter, less work and more practical than recording the image in the bank.

1

So I suggest you use a variable in your form to save the image path, in my idea would be something from 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 variable caminhoImage in your saving method.

Browser other questions tagged

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