guardar imagenes en productos

This commit is contained in:
carlos
2023-12-23 00:02:57 -06:00
parent ee731cedf2
commit ef749cc9b1
11 changed files with 27 additions and 3 deletions
Binary file not shown.
Binary file not shown.
Binary file not shown.
BIN
View File
Binary file not shown.
+26 -2
View File
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Data.SQLite;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
@@ -106,14 +107,18 @@ namespace compabetov2.database
{
conexion.Open();
string sql = "INSERT INTO Producto (nombre, descripcion, precio, stock, idcategoria) "+
"VALUES (@nombre, @descripcion, @precio, @stock, @idcategoria)";
string sql = "INSERT INTO Producto (nombre, descripcion, precio, img, stock, idcategoria) "+
"VALUES (@nombre, @descripcion, @precio, @stock, @img, @idcategoria)";
string rutaDestino = guardarImg(producto.img);
using (SQLiteCommand command = new SQLiteCommand(sql, conexion))
{
command.Parameters.AddWithValue("@nombre", producto.nombre);
command.Parameters.AddWithValue("@descripcion", producto.descripcion);
command.Parameters.AddWithValue("@precio", producto.precion);
command.Parameters.AddWithValue("@stock", producto.stock);
command.Parameters.AddWithValue("@img", rutaDestino);
command.Parameters.AddWithValue("@idcategoria", producto.categoria.idCategoria);
int filasAfectadas = command.ExecuteNonQuery();
@@ -140,6 +145,7 @@ namespace compabetov2.database
string sql = "INSERT INTO Categoria (nombre, descripcion) " +
"VALUES (@nombre, @descripcion)";
using (SQLiteCommand command = new SQLiteCommand(sql, conexion))
{
command.Parameters.AddWithValue("@nombre", categoria.nombre);
@@ -158,6 +164,24 @@ namespace compabetov2.database
}
}
private static string guardarImg(String rutaImagen)
{
//consigue la carpeta del proyectro
string carpetaImagenes = Path.Combine(Aplication.StartupPath, "produtos_img");
//crear el directorio sino existe
if (!Directory.Exists(carpetaImagenes))
{
Directory.CreateDirectory(carpetaImagenes);
}
string nombreArchivo = Path.GetFileName(rutaImagen);
string rutaDestino = Path.Combine(carpetaImagenes, nombreArchivo);
File.Copy(rutaImagen, rutaDestino, true);
return rutaDestino;
}
private static void crearDB()
{
var db_conexion = new Conexion();
Binary file not shown.
@@ -1 +1 @@
245f61b4e520a1fb9e0e2f4a53e49df7efd978ab
04ff0eea2625c5f041dc8598762cddc9006f263e8231100b6f207f00fb8b7971
Binary file not shown.
Binary file not shown.
Binary file not shown.