public static Producto getRegistro(string id) { coneccion c = new coneccion(); MySqlConnection con = c.con(); Producto item = new Producto(); DataSet ds = new DataSet(); try { con.Open(); string sql = ""; sql = " Select idProducto, nombre, codigo,precio,stock,idProveedor from producto" + " WHERE idProducto = @id "; MySqlDataAdapter adp = new MySqlDataAdapter(sql, con); adp.SelectCommand.Parameters.AddWithValue("@id", id); adp.Fill(ds); if (ds.Tables[0].Rows.Count > 0) { for (int i = 0; i < ds.Tables[0].Rows.Count; i++) { item.idProducto = ds.Tables[0].Rows[i]["idProducto"].ToString(); item.nombre = ds.Tables[0].Rows[i]["nombre"].ToString(); item.codigo = ds.Tables[0].Rows[i]["codigo"].ToString(); item.precio = ds.Tables[0].Rows[i]["precio"].ToString(); // float.Parse(ds.Tables[0].Rows[i]["precio"].ToString()); item.stock = ds.Tables[0].Rows[i]["stock"].ToString(); //int.Parse(ds.Tables[0].Rows[i]["stock"].ToString()); item.idProveedor = ds.Tables[0].Rows[i]["idProveedor"].ToString(); //int.Parse(ds.Tables[0].Rows[i]["stock"].ToString()); } } return(item); } catch (Exception ex) { Debug.Write("Error ... " + ex.Message); Debug.Write(ex.StackTrace); return(item); } finally { con.Close(); } }
public static Usuario getRegistro(string nombre, string password) { coneccion c = new coneccion(); MySqlConnection con = c.con(); Usuario item = new Usuario(); DataSet ds = new DataSet(); try { con.Open(); string sql = ""; sql = " Select idUsuario, nombre, password FROM usuarios" + " WHERE nombre = @nombre and password = @password "; MySqlDataAdapter adp = new MySqlDataAdapter(sql, con); adp.SelectCommand.Parameters.AddWithValue("@nombre", nombre); adp.SelectCommand.Parameters.AddWithValue("@password", password); adp.Fill(ds); if (ds.Tables[0].Rows.Count > 0) { for (int i = 0; i < ds.Tables[0].Rows.Count; i++) { item.idUsuario = ds.Tables[0].Rows[i]["idUsuario"].ToString(); item.nombre = ds.Tables[0].Rows[i]["nombre"].ToString(); item.password = ds.Tables[0].Rows[i]["password"].ToString(); } } return(item); } catch (Exception ex) { Debug.Write("Error ... " + ex.Message); Debug.Write(ex.StackTrace); return(item); } finally { con.Close(); } }
public static object GuardarRegistro(Producto producto, string accion) { coneccion c = new coneccion(); MySqlConnection coneccion = c.con(); //comandatabase.CommandTimeout = 60; // MySqlDataReader reader; try { coneccion.Open(); string query = ""; if (accion == "Nuevo") { query = "insert into producto(nombre,codigo,precio,stock,idProveedor) values(@nombre,@codigo,@precio,@stock,@idProveedor)"; } else { query = "UPDATE producto SET nombre = @nombre, codigo = @codigo, precio = @precio, stock = @stock, idProveedor=@idProveedor WHERE idProducto=@id"; } MySqlCommand comandatabase = new MySqlCommand(query, coneccion); // reader = comandatabase.ExecuteReader(); comandatabase.CommandType = CommandType.Text; comandatabase.Parameters.Add("@nombre", MySqlDbType.VarChar).Value = producto.nombre; comandatabase.Parameters.Add("@codigo", MySqlDbType.VarChar).Value = producto.codigo; comandatabase.Parameters.Add("@precio", MySqlDbType.Float).Value = producto.precio; comandatabase.Parameters.Add("@stock", MySqlDbType.Int64).Value = producto.stock; comandatabase.Parameters.Add("@idProveedor", MySqlDbType.Int64).Value = producto.idProveedor; comandatabase.Parameters.Add("@id", MySqlDbType.VarChar).Value = producto.idProducto; int r = comandatabase.ExecuteNonQuery(); return(r); } catch (Exception ex) { // MessageBox.Show(ex.Message); Debug.Write("Error...." + ex.Message); return(-1); //Retornamos menos uno cuando se dió por alguna razón un error } finally { coneccion.Close(); } }
public static object GuardarRegistro(Proveedor proveedor, string accion) { coneccion c = new coneccion(); MySqlConnection coneccion = c.con(); //comandatabase.CommandTimeout = 60; // MySqlDataReader reader; try { coneccion.Open(); string query = ""; if (accion == "Nuevo") { query = "insert into proveedores(nombre,telefono,correo) values(@nombre,@telefono,@correo)"; } else { query = "UPDATE proveedores SET nombre = @nombre, telefono= @telefono, correo= @correo WHERE idProveedor=@id"; } MySqlCommand comandatabase = new MySqlCommand(query, coneccion); // reader = comandatabase.ExecuteReader(); comandatabase.CommandType = CommandType.Text; comandatabase.Parameters.Add("@nombre", MySqlDbType.VarChar).Value = proveedor.nombre; comandatabase.Parameters.Add("@telefono", MySqlDbType.VarChar).Value = proveedor.telefono; comandatabase.Parameters.Add("@correo", MySqlDbType.VarChar).Value = proveedor.correo; comandatabase.Parameters.Add("@id", MySqlDbType.VarChar).Value = proveedor.idProveedor; int r = comandatabase.ExecuteNonQuery(); return(r); } catch (Exception ex) { // MessageBox.Show(ex.Message); Debug.Write("Error...." + ex.Message); return(-1); //Retornamos menos uno cuando se dió por alguna razón un error } finally { coneccion.Close(); } }
public static List <Producto> GetItemsProducto() { //Traemos la conexion coneccion c = new coneccion(); //Creamos un objeto de tipo MysqlConextion y le asignamos el metodo donde viene la conexion. MySqlConnection coneccion = c.con(); //Creamos una lista de tipo producto, ahi guardaremos objetos de los registros de la bd List <Producto> items = new List <Producto>(); try { coneccion.Open(); DataSet ds = new DataSet(); string query = "Select idProducto, nombre, codigo, precio,stock from producto "; // MySqlCommand mysc = new MySqlCommand(query, coneccion); MySqlDataAdapter mAdapter = new MySqlDataAdapter(query, coneccion); mAdapter.Fill(ds); if (ds.Tables[0].Rows.Count > 0) { for (int i = 0; i < ds.Tables[0].Rows.Count; i++) { Producto item = new Producto(); item.idProducto = ds.Tables[0].Rows[i]["idProducto"].ToString(); item.nombre = ds.Tables[0].Rows[i]["nombre"].ToString(); item.codigo = ds.Tables[0].Rows[i]["codigo"].ToString(); item.precio = ds.Tables[0].Rows[i]["precio"].ToString(); // float.Parse(ds.Tables[0].Rows[i]["precio"].ToString()); item.stock = ds.Tables[0].Rows[i]["stock"].ToString(); //int.Parse(ds.Tables[0].Rows[i]["stock"].ToString()); items.Add(item); } } return(items); } catch (Exception ex) { Debug.Write("Error...." + ex.Message); return(items); } finally { coneccion.Close(); } }
public static object eliminarRegistro(string id) { coneccion c = new coneccion(); MySqlConnection con = c.con(); try { con.Open(); string sql = ""; sql = " DELETE from producto " + " WHERE idProducto = @id "; MySqlCommand cmd = new MySqlCommand(sql, con); cmd.CommandType = CommandType.Text; cmd.Parameters.Add("@id", MySqlDbType.VarChar).Value = id; int r = cmd.ExecuteNonQuery(); Debug.Write("r = " + r); Debug.Write("Eliminado -> OK "); return(r); } catch (Exception ex) { Debug.Write("Error ... " + ex.Message); Debug.Write(ex.StackTrace); return(-1); } finally { con.Close(); } }