Ejemplo n.º 1
0
        public Inquilino ObtenerInquilino(int id)
        {
            var inquilino = new Inquilino();

            using (SqlConnection connection = new SqlConnection(connectionString))
            {
                string sql = $"SELECT Id, Nombre, Apellido, Dni, Telefono, Email" +
                             $" FROM Inquilinos WHERE Id = @id AND Estado = 1";
                using (SqlCommand command = new SqlCommand(sql, connection))
                {
                    command.Parameters.Add("@id", SqlDbType.Int).Value = id;
                    command.CommandType = CommandType.Text;
                    connection.Open();
                    var reader = command.ExecuteReader();
                    if (reader.Read())
                    {
                        inquilino = new Inquilino
                        {
                            Id       = reader.GetInt32(0),
                            Nombre   = reader.GetString(1),
                            Apellido = reader.GetString(2),
                            Dni      = reader.GetString(3),
                            Telefono = reader.GetString(4),
                            Email    = reader.GetString(5),
                        };
                    }
                    connection.Close();
                }
            }
            return(inquilino);
        }
Ejemplo n.º 2
0
        public List <Inquilino> ObtenerTodos()
        {
            var res = new List <Inquilino>();

            using (SqlConnection connection = new SqlConnection(connectionString))
            {
                string sql = $"SELECT Id, Nombre, Apellido, Dni, Telefono, Email" +
                             $" FROM Inquilinos WHERE Estado = 1";
                using (SqlCommand command = new SqlCommand(sql, connection))
                {
                    command.CommandType = CommandType.Text;
                    connection.Open();
                    var reader = command.ExecuteReader();
                    while (reader.Read())
                    {
                        Inquilino i = new Inquilino
                        {
                            Id       = reader.GetInt32(0),
                            Nombre   = reader.GetString(1),
                            Apellido = reader.GetString(2),
                            Dni      = reader.GetString(3),
                            Telefono = reader["Telefono"].ToString(),
                            Email    = reader.GetString(5),
                        };
                        res.Add(i);
                    }
                    connection.Close();
                }
            }
            return(res);
        }
Ejemplo n.º 3
0
        public int Modificar(Inquilino i)
        {
            int res = -1;

            using (SqlConnection connection = new SqlConnection(connectionString))
            {
                string sql = $"UPDATE Inquilinos SET" + " Nombre=@nombre, Apellido=@apellido, Dni=@dni, Telefono=@telefono, Email=@email " +
                             $"WHERE Id = @id";


                using (SqlCommand command = new SqlCommand(sql, connection))
                {
                    command.CommandType = CommandType.Text;
                    command.Parameters.AddWithValue("@nombre", i.Nombre);
                    command.Parameters.AddWithValue("@apellido", i.Apellido);
                    command.Parameters.AddWithValue("@dni", i.Dni);
                    command.Parameters.AddWithValue("@telefono", i.Telefono);
                    command.Parameters.AddWithValue("@email", i.Email);
                    command.Parameters.AddWithValue("@id", i.Id);
                    connection.Open();
                    res = command.ExecuteNonQuery();
                    connection.Close();
                }
            }
            return(res);
        }
Ejemplo n.º 4
0
        public int Alta(Inquilino i)
        {
            int res = -1;

            using (SqlConnection connection = new SqlConnection(connectionString))
            {
                string sql = "INSERT INTO inquilinos(Nombre, Apellido, Dni, Telefono, Email) " + $"VALUES(@nombre, @apellido, @dni, @telefono, @email);";

                using (SqlCommand command = new SqlCommand(sql, connection))
                {
                    command.CommandType = CommandType.Text;
                    command.Parameters.AddWithValue("@nombre", i.Nombre);
                    command.Parameters.AddWithValue("@apellido", i.Apellido);
                    command.Parameters.AddWithValue("@dni", i.Dni);
                    command.Parameters.AddWithValue("@telefono", i.Telefono);
                    command.Parameters.AddWithValue("@email", i.Email);
                    connection.Open();
                    res  = Convert.ToInt32(command.ExecuteScalar());
                    i.Id = res;
                    connection.Close();
                }
            }
            return(res);
        }