How can I load the path of an image to the sql server from visual studio using c #?

0

I'm doing a Windows Forms project in Visual Studio 2015, with a database in SQL Server Management Studio 2013 and I want to be able to add an image from a project form and keep the same route in my database. data.

if (ValidarDatos())
        {
            if (imagen==null)
            {
                imagen = new Imagen();
            }
            string imagenNoDisponible = Application.StartupPath + "\Imágenes\" + "imagenNoDisponible.jpg";
            if (imagen.RutaImagen != null || imagen.RutaImagen != string.Empty)
            {
                archivoNombre = imagen.RutaImagen;
                string archivoNombreConRuta = Application.StartupPath + "\Imágenes\" + archivoNombre;
                if (File.Exists(archivoNombreConRuta))
                {
                    picImagenes.Image = Image.FromFile(@archivoNombreConRuta);
                }
                else
                {
                    if (File.Exists(imagenNoDisponible))
                        picImagenes.Image = Image.FromFile(@imagenNoDisponible);
                }
            }
            else
            {
                if (File.Exists(imagenNoDisponible))
                    picImagenes.Image = Image.FromFile(@imagenNoDisponible);
            }
            if (!Editar)
            {
                try
                {
                    ImagenBD.Agregar(imagen);
                    MessageBox.Show("Imagen agregada", "Mensaje", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    DialogResult dr = MessageBox.Show("¿Desea agregar otra Imagen?", "Continuar",
                        MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1);
                    if (dr == DialogResult.Yes)
                    {
                        InicializarControles();
                    }
                    else
                    {
                        this.DialogResult = DialogResult.OK;
                    }
                }
                catch (Exception ex)
                {

                    MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }

            }
            else
            {
                this.DialogResult = DialogResult.OK;
            }
        }
    
asked by Ignacio Pouysegu 09.10.2017 в 20:05
source

2 answers

0

Let's start from a base, when you arm a route you should use

string imagenNoDisponible = Path.Combine(Application.StartupPath, @"Imágenes\imagenNoDisponible.jpg");

This way you make the creation of the route safer

If the db you use is Sql Server should be in a database server, or be separated to the client application running on a pc, therefore the copy of the selected image should be made to a shared folder , this folder could be defined by configuration in case it should change, you do not have to recompile.

Now, when you select a file, it is clear that when you assign it to a PictureBox it does not return the route, but you must conserve it in a separate variable, or if you prefer it in the property Tag of the image control

picImagenes.Tag = imagenNoDisponible;

if you need to recover the route you can do it without problems and proceed to the copy

string rutaImagen = picImagenes.Tag.ToString();
File.Copy(rutaImagen, rutaDestino);

but I stress that the db should not be in the same PC as the application

in the database you will only persist the name of the image, for that you use

string nombreImagen = Path.GetFileName(rutaImagen);

that is what you register in the table, since to recover it arms the complete route uniting the folder that you define by configuration where they will be als images + the name of the image that you get from the field in the table

    
answered by 10.10.2017 в 08:47
0

If you only want to obtain the path where the image is, you can do it when you insert it in PictureBox , look at the following code:

if (openFileDialog1.ShowDialog() == DialogResult.OK)
        {
            pictureBox1.Image = Image.FromFile(openFileDialog1.FileName); //La propiedad FileName de un openFileDialog guarda la ruta donde se almacena el archivo seleccionado. 
        }

The format of the route obtained with the Filename property is:

"C: \\ New folder \\ image.jpg"

Once the route is obtained, you store it as a string

    
answered by 02.10.2018 в 19:18