guardar imagenes en productos
This commit is contained in:
+26
-2
@@ -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();
|
||||
|
||||
Reference in New Issue
Block a user