insert y select de productos agregada
This commit is contained in:
+101
-30
@@ -6,6 +6,7 @@ using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows.Forms;
|
||||
using compabetov2.database;
|
||||
using compabetov2.database.modelos;
|
||||
|
||||
namespace compabetov2.database
|
||||
{
|
||||
@@ -16,38 +17,46 @@ namespace compabetov2.database
|
||||
var db_conexion = new Conexion();
|
||||
try
|
||||
{
|
||||
SQLiteConnection conexion = db_conexion.get_connection();
|
||||
conexion.Open();
|
||||
|
||||
using (SQLiteConnection conexion = db_conexion.get_connection()) {
|
||||
conexion.Open();
|
||||
|
||||
string sql = "SELECT usuario, contra FROM Login WHERE usuario = @param0";
|
||||
SQLiteCommand command = new SQLiteCommand(sql, conexion);
|
||||
command.Parameters.AddWithValue($"@param{0}", usuario);
|
||||
SQLiteDataReader reader = command.ExecuteReader();
|
||||
string sql = "SELECT usuario, contra FROM Login WHERE usuario = @param0";
|
||||
SQLiteCommand command = new SQLiteCommand(sql, conexion);
|
||||
command.Parameters.AddWithValue($"@param{0}", usuario);
|
||||
SQLiteDataReader reader = command.ExecuteReader();
|
||||
|
||||
if (reader.HasRows)
|
||||
{
|
||||
while (reader.Read())
|
||||
if (reader.HasRows)
|
||||
{
|
||||
if (reader["contra"].Equals(contra))
|
||||
while (reader.Read())
|
||||
{
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
MessageBox.Show("Usuario o contraseña incorrectos");
|
||||
return false;
|
||||
if (reader["contra"].Equals(contra))
|
||||
{
|
||||
conexion.Close();
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
MessageBox.Show("Usuario o contraseña incorrectos");
|
||||
conexion.Close();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
MessageBox.Show("Usuario o contraseña incorrectos");
|
||||
conexion.Close();
|
||||
return false;
|
||||
}
|
||||
else
|
||||
{
|
||||
MessageBox.Show("Usuario o contraseña incorrectos");
|
||||
conexion.Close();
|
||||
return false;
|
||||
}
|
||||
MessageBox.Show("Usuario o contraseña incorrectos");
|
||||
return false;
|
||||
}
|
||||
else
|
||||
{
|
||||
MessageBox.Show("Usuario o contraseña incorrectos");
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
catch (SQLiteException ex)
|
||||
{
|
||||
if (ex.Message.Contains("no such table"))
|
||||
@@ -65,6 +74,61 @@ namespace compabetov2.database
|
||||
}
|
||||
}
|
||||
|
||||
public static SQLiteDataReader conseguirProductosDAO()
|
||||
{
|
||||
SQLiteDataReader reader = null;
|
||||
var db_conexion = new Conexion();
|
||||
try
|
||||
{
|
||||
using (SQLiteConnection conexion = db_conexion.get_connection())
|
||||
{
|
||||
conexion.Open();
|
||||
|
||||
string sql = "SELECT * FROM Producto INNER JOIN Categoria ON Producto.fk_idcategoria = Categoria.idcategoria";
|
||||
SQLiteCommand command = new SQLiteCommand(sql, conexion);
|
||||
reader = command.ExecuteReader();
|
||||
|
||||
return reader;
|
||||
}
|
||||
}
|
||||
catch (SQLiteException ex) {
|
||||
MessageBox.Show(ex.Message);
|
||||
return reader;
|
||||
}
|
||||
}
|
||||
|
||||
public static bool insertarProductoDAO(Producto producto)
|
||||
{
|
||||
var db_conexion = new Conexion();
|
||||
try
|
||||
{
|
||||
using (SQLiteConnection conexion = db_conexion.get_connection())
|
||||
{
|
||||
conexion.Open();
|
||||
|
||||
string sql = "INSERT INTO Producto (nombre, descripcion, precio, stock, idcategoria) "+
|
||||
"VALUES (@nombre, @descripcion, @precio, @stock, @idcategoria)";
|
||||
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("@idcategoria", producto.categoria.idCategoria);
|
||||
int filasAfectadas = command.ExecuteNonQuery();
|
||||
|
||||
if(filasAfectadas > 0) { return true; }
|
||||
else { return false; }
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (SQLiteException ex)
|
||||
{
|
||||
MessageBox.Show(ex.Message);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private static void crearDB()
|
||||
{
|
||||
var db_conexion = new Conexion();
|
||||
@@ -74,15 +138,22 @@ namespace compabetov2.database
|
||||
conexion.Open();
|
||||
string[] consultas = new string[]
|
||||
{
|
||||
"DROP TABLE IF EXISTS Empleado",
|
||||
"DROP TABLE IF EXISTS Login",
|
||||
"DROP TABLE IF EXISTS Empleado",
|
||||
"DROP TABLE IF EXISTS Producto",
|
||||
"DROP TABLE IF EXISTS Categoria",
|
||||
|
||||
"CREATE TABLE Empleado (idempleado INTEGER PRIMARY KEY AUTOINCREMENT, nombre TEXT, "+
|
||||
"apellido TEXT, calle TEXT, fecha_contratacion TEXT, colonia TEXT, cp TEXT, no_ext TEXT" +
|
||||
"no_int TEXT, referencia TEXT, fecha_nac TEXT, telefono TEXT, activo INTEGER)",
|
||||
"CREATE TABLE Empleado (idempleado INTEGER PRIMARY KEY AUTOINCREMENT, nombre TEXT NOT NULL, "+
|
||||
"apellido TEXT NOT NULL, calle TEXT NOT NULL, fecha_contratacion TEXT NOT NULL, colonia TEXT NOT NULL, cp TEXT NOT NULL, no_ext TEXT NOT NULL" +
|
||||
"no_int TEXT NOT NULL, referencia TEXT NOT NULL, fecha_nac TEXT NOT NULL, telefono TEXT NOT NULL, activo INTEGER NOT NULL)",
|
||||
|
||||
"CREATE TABLE Login (idlogin INTEGER PRIMARY KEY AUTOINCREMENT, tipo TEXT, usuario TEXT, "+
|
||||
"contra TEXT, fk_idempleado INTEGER, FOREIGN KEY (fk_idempleado) REFERENCES Empleado (idempleado))",
|
||||
"CREATE TABLE Login (idlogin INTEGER PRIMARY KEY AUTOINCREMENT, tipo TEXT NOT NULL, usuario TEXT NOT NULL, "+
|
||||
"contra TEXT NOT NULL, fk_idempleado INTEGER NOT NULL, FOREIGN KEY (fk_idempleado) REFERENCES Empleado (idempleado))",
|
||||
|
||||
"CREATE TABLE Categoria (idcategoria INTEGER PRIMARY KEY AUTOINCREMENT, nombre TEXT NOT NULL, descripcion TEXT NOT NULL)",
|
||||
|
||||
"CREATE TABLE Producto (idproducto INTEGER PRIMARY KEY AUTOINCREMENT, nombre TEXT NOT NULL, descripcion TEXT NOT NULL, "+
|
||||
"precio DECIMAL(10, 2) NOT NULL, stock INTEGER NOT NULL, fk_idcategoria INTEGER NOT NULL, FOREIGN KEY (fk_idcategoria) REFERENCES Categoria (idcategoria))",
|
||||
|
||||
"INSERT INTO Empleado ( nombre, apellido, calle, fecha_contratacion, colonia, cp, no_ext, referencia, fecha_nac, telefono, activo) VALUES('jefe', '', '', '', '', '', '', '', '', '', 0);",
|
||||
"INSERT INTO Login ( tipo, usuario, contra, fk_idempleado) VALUES('admin', 'admin', '12345', 1);"
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace compabetov2.database.modelos
|
||||
{
|
||||
internal class Categoria
|
||||
{
|
||||
public int idCategoria;
|
||||
public string nombre;
|
||||
public string descricion;
|
||||
|
||||
public Categoria(int idCategoria, string nombre = "", string descricion = "")
|
||||
{
|
||||
this.idCategoria = idCategoria;
|
||||
this.nombre = nombre;
|
||||
this.descricion = descricion;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace compabetov2.database.modelos
|
||||
{
|
||||
internal class Producto
|
||||
{
|
||||
public int idProducto;
|
||||
public string nombre;
|
||||
public string descripcion;
|
||||
public decimal precion;
|
||||
public int stock;
|
||||
public Categoria categoria;
|
||||
|
||||
public Producto( string nombre, string descripcion, decimal precion, Categoria categoria, int stock = 0, int idProducto = 0)
|
||||
{
|
||||
this.idProducto = idProducto;
|
||||
this.nombre = nombre;
|
||||
this.descripcion = descripcion;
|
||||
this.precion = precion;
|
||||
this.stock = stock;
|
||||
this.categoria = categoria;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data.SQLite;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
@@ -14,5 +15,37 @@ namespace compabetov2.database
|
||||
{
|
||||
return DAO.iniciarSesionDAO(usuario.nombreUsuario, usuario.contra);
|
||||
}
|
||||
|
||||
public static List<Producto> conseguirProductos()
|
||||
{
|
||||
List<Producto> productos = new List<Producto>();
|
||||
|
||||
SQLiteDataReader reader = DAO.conseguirProductosDAO();
|
||||
|
||||
while (reader.Read())
|
||||
{
|
||||
Categoria categoria = new Categoria(
|
||||
(int)reader["idcategoria"],
|
||||
reader["nombre"].ToString(),
|
||||
reader["descripcion"].ToString());
|
||||
|
||||
Producto producto = new Producto(
|
||||
reader["nombre"].ToString(),
|
||||
reader["descripcion"].ToString(),
|
||||
(decimal)reader["precio"],
|
||||
categoria,
|
||||
(int)reader["stock"],
|
||||
(int)reader["idproducto"]);
|
||||
|
||||
productos.Add(producto);
|
||||
}
|
||||
|
||||
return productos;
|
||||
}
|
||||
|
||||
public static bool insertarProducto(Producto producto)
|
||||
{
|
||||
return DAO.insertarProductoDAO(producto);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user