public void Eliminar(ClienteCE clienteCE) { //crear mi objeto de conexion ConexionCD conexion = new ConexionCD(); //crear el objeto sqlconnection SqlConnection connBD = conexion.ConectarSQLSERVER(); //abrir la conexion connBD.Open(); //crear un comando SqlCommand cmdBD = connBD.CreateCommand(); //tipo de comando cmdBD.CommandType = CommandType.Text; //asignar la instruccion SQL cmdBD.CommandText = "delete cliente where id=@id"; //Asigno parametros y sus valores cmdBD.Parameters.AddWithValue("@id", clienteCE.id); //EJERCUTAR EL COMMAND cmdBD.ExecuteNonQuery(); //CERRAR LA CONEXION connBD.Close(); }
public void Actualizar(ClienteCE clienteCE) { ConexionCD conexion = new ConexionCD(); SqlConnection connBD = conexion.ConectarSQLSERVER(); connBD.Open(); SqlCommand cmd = connBD.CreateCommand(); cmd.CommandType = CommandType.Text; cmd.CommandText = "update cliente " + "set nombre=@nombre, numruc=@numruc, direccion=@direccion, telefono=@telefono " + "where id=@id"; cmd.Parameters.AddWithValue("@numruc", clienteCE.numruc); cmd.Parameters.AddWithValue("@nombre", clienteCE.nombre); cmd.Parameters.AddWithValue("@direccion", clienteCE.direccion); cmd.Parameters.AddWithValue("@telefono", clienteCE.telefono); cmd.Parameters.AddWithValue("@id", clienteCE.id); cmd.ExecuteNonQuery(); connBD.Close(); }
public void Actualizar(ProductoCE productoCE) { //crear mi objeto de conexion ConexionCD conexion = new ConexionCD(); //crear el objeto sqlconnection SqlConnection connBD = conexion.ConectarSQLSERVER(); //abrir la conexion connBD.Open(); //crear un comando SqlCommand cmdBD = connBD.CreateCommand(); //tipo de comando cmdBD.CommandType = CommandType.Text; //asignar la instruccion SQL cmdBD.CommandText = "update producto " + "set descripcion=@descripcion, categoria=@categoria, precio=@precio " + "where id=@id"; //Asigno parametros y sus valores cmdBD.Parameters.AddWithValue("@descripcion", productoCE.descripcion); cmdBD.Parameters.AddWithValue("@categoria", productoCE.categoria); cmdBD.Parameters.AddWithValue("@precio", productoCE.precio); cmdBD.Parameters.AddWithValue("@id", productoCE.id); //EJERCUTAR EL COMMAND cmdBD.ExecuteNonQuery(); //CERRAR LA CONEXION connBD.Close(); }
public int Nuevo(ProductoCE productoCE) { //crear mi objeto de conexion ConexionCD conexion = new ConexionCD(); //crear el objeto sqlconnection SqlConnection connBD = conexion.ConectarSQLSERVER(); //abrir la conexion connBD.Open(); //crear un comando SqlCommand cmdBD = connBD.CreateCommand(); //tipo de comando cmdBD.CommandType = CommandType.Text; //asignar la instruccion SQL cmdBD.CommandText = "insert into producto (descripcion, categoria, precio) " + "values (@descripcion, @categoria, @precio)"; //Asigno parametros y sus valores cmdBD.Parameters.AddWithValue("@descripcion", productoCE.descripcion); cmdBD.Parameters.AddWithValue("@categoria", productoCE.categoria); cmdBD.Parameters.AddWithValue("@precio", productoCE.precio); //EJERCUTAR EL COMMAND cmdBD.ExecuteNonQuery(); //DETERMINAR EL ULTIMO ID cmdBD.CommandText = "select max(id) as nuevoId from producto where descripcion=@descripcion"; //ASIGNAR VALOR AL PARAMETRO YA CREADO(DESCRIPCION) cmdBD.Parameters["@descripcion"].Value = productoCE.descripcion; //DETERMINAR EL ULTIMO ID //EJECUTAR EL COMANDO SqlDataReader drBD = cmdBD.ExecuteReader(); //LEER EL DATAREADER drBD.Read(); //LEER EL VALOR DE LA COLUMNA EN EL DATAREADER productoCE.id = Convert.ToInt32(drBD["nuevoId"].ToString()); //CERRAR LA CONEXION connBD.Close(); return(productoCE.id); }
public List <ProductoCE> BuscarProducto(string condicion) { //crear mi objeto de conexion ConexionCD conexion = new ConexionCD(); //crear el objeto sqlconnection SqlConnection connBD = conexion.ConectarSQLSERVER(); //abrir la conexion connBD.Open(); //crear un comando SqlCommand cmdBD = connBD.CreateCommand(); //tipo de comando cmdBD.CommandType = CommandType.Text; //asignar la instruccion SQL cmdBD.CommandText = "select * from producto where descripcion like '%' + @descripcion + '%'"; //Asignar parametros y sus valores cmdBD.Parameters.AddWithValue("@descripcion", condicion); //Ejecutar el comando y asignar el resultado a un sqldatareader SqlDataReader drBD = cmdBD.ExecuteReader(); //Declarar la coleccion List <ProductoCE> miListaProductos = new List <ProductoCE>(); //Leer el sqldatareader hasta finalizar el EOF while (drBD.Read()) { //Instanciar un objeto ProductoCE ProductoCE productoCE = new ProductoCE(); //Asignar los valores a la propiedades productoCE.id = Convert.ToInt32(drBD["id"].ToString()); productoCE.descripcion = drBD["descripcion"].ToString(); productoCE.categoria = drBD["categoria"].ToString(); productoCE.precio = Convert.ToDouble(drBD["precio"].ToString()); //Agregar el elemnto a la coleccion MilistaProductos miListaProductos.Add(productoCE); } connBD.Close(); return(miListaProductos); }
public int Nuevo(ClienteCE clienteCE) { ConexionCD conexion = new ConexionCD(); SqlConnection connBD = conexion.ConectarSQLSERVER(); connBD.Open(); SqlCommand cmd = connBD.CreateCommand(); cmd.CommandType = CommandType.Text; cmd.CommandText = "insert into cliente (nombre, numruc, direccion,telefono) " + "values (@nombre, @numruc, @direccion,@telefono)"; cmd.Parameters.AddWithValue("@nombre", clienteCE.nombre); cmd.Parameters.AddWithValue("@numruc", clienteCE.numruc); cmd.Parameters.AddWithValue("@direccion", clienteCE.direccion); cmd.Parameters.AddWithValue("@telefono", clienteCE.telefono); cmd.ExecuteNonQuery(); cmd.CommandText = "select max(id) as nuevoId from cliente where nombre=@nombre"; cmd.Parameters["@nombre"].Value = clienteCE.nombre; SqlDataReader drBD = cmd.ExecuteReader(); drBD.Read(); clienteCE.id = Convert.ToInt32(drBD["nuevoId"].ToString()); connBD.Close(); return(clienteCE.id); }