public static List <Cargo> ObtenerTodos() { string consulta = "SELECT TOP 500 c.Id, c.IdEstado, c.Nombre FROM Cargo c"; SqlCommand comando = ComunDB.ObtenerComando(); comando.CommandText = consulta; SqlDataReader reader = ComunDB.EjecutarComandoReader(comando); List <Cargo> ListaCargos = new List <Cargo>(); while (reader.Read()) { Cargo cargo = new Cargo(); cargo.Id = reader.GetByte(0); cargo.IdEstado = reader.GetByte(1); cargo.Nombre = reader.GetString(2); ListaCargos.Add(cargo); } return(ListaCargos); }
public static List <Cargo> Buscar(Cargo pCargo) { string consulta = "SELECT TOP 500 c.Id, c.IdEstado, c.Nombre FROM Cargo c"; string wheresql = " "; // agrega condiciones a las consultas segun los campos buscados byte contadoCampos = 0; // sirve para saber si agregar AND a la consulta SqlCommand comando = ComunDB.ObtenerComando(); // agregar condiciones a la consulta para obtener los registros filtrados por los campos de busueda if (pCargo.IdEstado > 0) // verificar que la propiedad IdEstado tenga informacion que buscar { if (contadoCampos > 0) { wheresql += " AND "; //SELECT TOP 500 c.Id, c.IdEstado, c.Nombre FROM Cargo c WHERE IdEstado=@IdEstado } contadoCampos++; wheresql += " IdEstado = @IdEstado"; comando.Parameters.AddWithValue("@IdEstado", pCargo.IdEstado); } if (pCargo.Nombre != null && pCargo.Nombre.Trim().Length > 0) //verificar que nombre tenga informacion para buscar { if (contadoCampos > 0) { wheresql += " AND "; //SELECT TOP 500 c.Id, c.IdEstado, c.Nombre FROM Cargo c WHERE IdEstado=@IdEstado AND Nombre LIKE Nombre } contadoCampos++; wheresql += " Nombre LIKE @Nombre "; comando.Parameters.AddWithValue("@Nombre", '%' + pCargo.Nombre + '%'); } if (wheresql.Trim().Length > 0) // verificar si hay texto { wheresql = " WHERE " + wheresql; // } comando.CommandText = consulta + wheresql; SqlDataReader reader = ComunDB.EjecutarComandoReader(comando); List <Cargo> ListaCargos = new List <Cargo>(); while (reader.Read()) { Cargo cargo = new Cargo(); cargo.Id = reader.GetByte(0); cargo.IdEstado = reader.GetByte(1); cargo.Nombre = reader.GetString(2); ListaCargos.Add(cargo); } comando.Connection.Dispose(); return(ListaCargos); }
public static Estado ObtenerPorId(byte pId) { string consulta = "SELECT e.Id, e.Nombre FROM Estado e WHERE Id=@Id"; SqlCommand comando = ComunDB.ObtenerComando(); comando.CommandText = consulta; comando.Parameters.AddWithValue("@Id", pId); SqlDataReader reader = ComunDB.EjecutarComandoReader(comando); Estado estado = new Estado(); while (reader.Read()) { estado.Id = reader.GetByte(0); estado.Nombre = reader.GetString(1); } return(estado); }
public static Cargo ObtenerPorId(byte pId) { string consulta = "SELECT c.Id, C.IdEstado, c.Nombre FROM Cargo c WHERE c.Id=@Id"; SqlCommand comando = ComunDB.ObtenerComando(); comando.CommandText = consulta; comando.Parameters.AddWithValue("@Id", pId); SqlDataReader reader = ComunDB.EjecutarComandoReader(comando); Cargo cargo = new Cargo(); while (reader.Read()) { cargo.Id = reader.GetByte(0); cargo.IdEstado = reader.GetByte(1); cargo.Nombre = reader.GetString(2); } return(cargo); }
public static List <Estado> ObtenerTodos() { string consulta = "SELECT TOP 500 e.Id, e.Nombre FROM Estado e"; SqlCommand comando = ComunDB.ObtenerComando(); comando.CommandText = consulta; SqlDataReader reader = ComunDB.EjecutarComandoReader(comando); List <Estado> ListaEstados = new List <Estado>(); while (reader.Read()) { Estado estado = new Estado(); estado.Id = reader.GetByte(0); estado.Nombre = reader.GetString(1); ListaEstados.Add(estado); } return(ListaEstados); }