Beispiel #1
0
        public clsDepartamento departamentoPorID(int id)
        {
            clsDepartamento objDepartamento = new clsDepartamento();
            clsMyConnection miConexion      = new clsMyConnection();
            SqlConnection   conexion        = miConexion.getConnection();
            SqlCommand      miComando       = new SqlCommand();
            SqlDataReader   miLector;

            try
            {
                miComando.CommandText = "SELECT * FROM PD_Departamentos WHERE IDDepartamento  = " + id;
                miComando.Connection  = conexion;
                miLector = miComando.ExecuteReader();
                //Si hay lineas en el lector
                if (miLector.HasRows)
                {
                    while (miLector.Read())
                    {
                        objDepartamento        = new clsDepartamento();
                        objDepartamento.id     = (int)miLector["IDDepartamento"];
                        objDepartamento.nombre = (string)miLector["NombreDepartamento"];
                    }
                }
                miLector.Close();
                miConexion.closeConnection(ref conexion);
            }
            catch (SqlException exSql)
            {
                throw exSql;
            }

            return(objDepartamento);
        }
Beispiel #2
0
        /// <summary>
        /// Este metodo nos permite obtener un listado de las personas almacenadas en la BBDD.
        /// </summary>
        /// <returns>Devuelve un list de clsPersona</returns>
        public async Task <List <clsPersona> > listadoPersonas()
        {
            List <clsPersona> listadoPersonas = new List <clsPersona>();

            HttpClient      miCliente  = new HttpClient();
            clsMyConnection miConexion = new clsMyConnection();

            String uriBase    = miConexion.getUriBase();
            Uri    requestUri = new Uri(uriBase + "PersonasAPI");

            //Send the GET request asynchronously and retrieve the response as a string.
            HttpResponseMessage httpResponse = new HttpResponseMessage();
            string httpResponseBody          = "";

            try
            {
                //Send the GET request
                httpResponse = await miCliente.GetAsync(requestUri);

                httpResponse.EnsureSuccessStatusCode();
                httpResponseBody = await httpResponse.Content.ReadAsStringAsync();

                listadoPersonas = JsonConvert.DeserializeObject <List <clsPersona> >(httpResponseBody);
            }
            catch (Exception ex)
            {
                httpResponseBody = "Error: " + ex.HResult.ToString("X") + " Message: " + ex.Message;
            }

            return(listadoPersonas);
        }
Beispiel #3
0
        public List <clsPersona> listadoPersonas()
        {
            clsMyConnection miConexion = new clsMyConnection();
            SqlConnection   connection = miConexion.getConnection();
            SqlCommand      miComando  = new SqlCommand();
            SqlDataReader   miLector;

            System.Type       tipoDBNULL      = DBNull.Value.GetType();
            List <clsPersona> listadoPersonas = new List <clsPersona>();
            clsPersona        objPersona;

            try
            {
                miComando.CommandText = "SELECT * FROM PD_Personas";
                miComando.Connection  = connection;
                miLector = miComando.ExecuteReader();
                if (miLector.HasRows)
                {
                    while (miLector.Read())
                    {
                        objPersona                 = new clsPersona();
                        objPersona.idPersona       = miLector["IdPersona"].GetType() != tipoDBNULL ? (int)miLector["IdPersona"] : 0;
                        objPersona.nombre          = miLector["NombrePersona"].GetType() != tipoDBNULL ? (string)miLector["NombrePersona"] : null;
                        objPersona.apellidos       = miLector["ApellidosPersona"].GetType() != tipoDBNULL ? (string)miLector["ApellidosPersona"] : null;
                        objPersona.fechaNacimiento = miLector["FechaNacimientoPersona"].GetType() != tipoDBNULL ? (DateTime)miLector["FechaNacimientoPersona"] : new DateTime();
                        objPersona.direccion       = miLector["Direccion"].GetType() != tipoDBNULL ? (string)miLector["Direccion"] : null;
                        objPersona.telefono        = miLector["TelefonoPersona"].GetType() != tipoDBNULL ? (string)miLector["TelefonoPersona"] : null;
                        objPersona.foto            = miLector["FotoPersona"].GetType() != tipoDBNULL ? (byte[])miLector["FotoPersona"] : null;
                        objPersona.idDepartamento  = miLector["IDDepartamento"].GetType() != tipoDBNULL ? (int)miLector["IDDepartamento"] : 0;
                        listadoPersonas.Add(objPersona);
                    }
                }
                miLector.Close();
            }
            catch (SqlException e)
            {
                throw e;
            }
            finally {
                miConexion.closeConnection(ref connection);
            }

            return(listadoPersonas);
        }
Beispiel #4
0
        /// <summary>
        /// Funcion que recibe como parametro un entero id y devuelve la persona correspondiente a ese id.
        /// </summary>
        /// <param name="id"></param>
        /// <returns>Devuelve un objeto persona</returns>
        public clsPersona buscarPersona_DAL(int id)
        {
            clsPersona objPersona = new clsPersona();

            clsMyConnection miConexion = new clsMyConnection();
            SqlConnection   conexion   = miConexion.getConnection();
            SqlCommand      comando    = new SqlCommand();
            SqlDataReader   miLector   = null;

            System.Type tipoDBNULL = DBNull.Value.GetType();
            String      idPersona;

            try
            {
                comando.Connection = conexion;
                comando.Parameters.Add("@id", System.Data.SqlDbType.Int).Value = id;
                idPersona           = "@id";
                comando.CommandText = "Select * From PD_Personas Where Idpersona =" + idPersona;
                miLector            = comando.ExecuteReader();

                if (miLector.HasRows)
                {
                    miLector.Read();

                    objPersona.idPersona       = miLector["IdPersona"].GetType() != tipoDBNULL ? (int)miLector["IdPersona"] : 0;
                    objPersona.nombre          = miLector["NombrePersona"].GetType() != tipoDBNULL ? (string)miLector["NombrePersona"] : null;
                    objPersona.apellidos       = miLector["ApellidosPersona"].GetType() != tipoDBNULL ? (string)miLector["ApellidosPersona"] : null;
                    objPersona.fechaNacimiento = miLector["FechaNacimientoPersona"].GetType() != tipoDBNULL ? (DateTime)miLector["FechaNacimientoPersona"] : new DateTime();
                    objPersona.direccion       = miLector["Direccion"].GetType() != tipoDBNULL ? (string)miLector["Direccion"] : null;
                    objPersona.telefono        = miLector["TelefonoPersona"].GetType() != tipoDBNULL ? (string)miLector["TelefonoPersona"] : null;
                    objPersona.foto            = miLector["FotoPersona"].GetType() != tipoDBNULL ? (byte[])miLector["FotoPersona"] : null;
                    objPersona.idDepartamento  = miLector["IDDepartamento"].GetType() != tipoDBNULL ? (int)miLector["IDDepartamento"] : 0;
                }

                miLector.Close();
            }
            catch (SqlException e) {
                throw e;
            }
            finally{
                miConexion.closeConnection(ref conexion);
            }
            return(objPersona);
        }
Beispiel #5
0
        public List <clsDepartamento> listadoDepartamentos()
        {
            clsMyConnection miConexion = new clsMyConnection();
            SqlConnection   conexion   = miConexion.getConnection();
            SqlCommand      comando    = new SqlCommand();
            SqlDataReader   miLector   = null;

            System.Type            tipoDBNULL      = DBNull.Value.GetType();
            clsDepartamento        objDepartamento = new clsDepartamento();
            List <clsDepartamento> listado         = new List <clsDepartamento>();


            try
            {
                comando.Connection = conexion;

                comando.CommandText = "Select * from PD_Departamentos";
                miLector            = comando.ExecuteReader();

                if (miLector.HasRows)
                {
                    while (miLector.Read())
                    {
                        objDepartamento        = new clsDepartamento();
                        objDepartamento.id     = miLector["IdDepartamento"].GetType() != tipoDBNULL ? (int)miLector["IdDepartamento"] : 0;
                        objDepartamento.nombre = miLector["NombreDepartamento"].GetType() != tipoDBNULL ? (string)miLector["NombreDepartamento"] : null;
                        listado.Add(objDepartamento);
                    }
                }
                miLector.Close();
            }
            catch (SqlException e)
            {
                throw e;
            }
            finally
            {
                miConexion.closeConnection(ref conexion);
            }
            return(listado);
        }
Beispiel #6
0
        /// <summary>
        /// Funcion que recibe como parametro un entero id y borra a la persona correspondiente de ese id en la DB
        /// </summary>
        /// <param name="id"></param>
        /// <returns>Devuelve el numero de filas afectadas</returns>
        public int borrarPersona_DAL(int id)
        {
            clsMyConnection miConexion = new clsMyConnection();
            SqlConnection   conexion   = miConexion.getConnection();
            int             filas      = 0;
            SqlCommand      comando    = new SqlCommand();

            try{
                comando.Connection = conexion;

                comando.Parameters.Add("@id", System.Data.SqlDbType.Int).Value = id;
                comando.CommandText = "Delete FROM PD_Personas Where IdPersona = @id";

                filas = comando.ExecuteNonQuery();
            }catch (SqlException e) {
                throw e;
            }
            finally {
                miConexion.closeConnection(ref conexion);
            }
            return(filas);
        }
Beispiel #7
0
        /// <summary>
        /// Metodo para comprobar que existe la persona
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public Boolean existePersona_DAL(int id)
        {
            clsMyConnection miConexion = new clsMyConnection();
            SqlConnection   connection = miConexion.getConnection();
            SqlCommand      miComando  = new SqlCommand();
            SqlDataReader   miLector;
            Boolean         existe = false;


            miComando.CommandText = "SELECT * FROM PD_Personas Where Idpersona =" + id;
            miComando.Connection  = connection;
            miLector = miComando.ExecuteReader();

            if (miLector.HasRows)
            {
                existe = true;
            }

            miLector.Close();
            miConexion.closeConnection(ref connection);

            return(existe);
        }
Beispiel #8
0
        /// <summary>
        /// Funcion que recibe por parametros a un objeto clsPersona y lo inserta en la DB
        /// </summary>
        /// <param name="objPersona"></param>
        /// <returns>Devuelve el numero de filas afectadas</returns>
        public int insertarPersona_DAL(clsPersona objPersona)
        {
            clsMyConnection miConexion = new clsMyConnection();
            SqlCommand      comando    = new SqlCommand();
            int             filas      = 0;
            SqlConnection   conexion   = miConexion.getConnection();

            System.Type tipoDBNULL = DBNull.Value.GetType();
            String      foto, fechaNacimiento, telefono, direccion, apellidos, nombre, idDepartamento;

            try
            {
                comando.Connection = conexion;

                if (objPersona.nombre != null)
                {
                    comando.Parameters.Add("@nombre", System.Data.SqlDbType.VarChar).Value = objPersona.nombre;
                    nombre = "@nombre";
                }
                else
                {
                    nombre = "NULL";
                }
                if (objPersona.apellidos != null)
                {
                    comando.Parameters.Add("@apellidosPersona", System.Data.SqlDbType.VarChar).Value = objPersona.apellidos;
                    apellidos = "@apellidosPersona";
                }
                else
                {
                    apellidos = "NULL";
                }

                comando.Parameters.Add("@IDDepartamento", System.Data.SqlDbType.Int).Value = objPersona.idDepartamento;
                idDepartamento = "@IDDepartamento";

                if (objPersona.fechaNacimiento != null)
                {
                    comando.Parameters.Add("@fechaNacimiento", System.Data.SqlDbType.DateTime).Value = objPersona.fechaNacimiento;
                    fechaNacimiento = "@fechaNacimiento";
                }
                else
                {
                    fechaNacimiento = "NULL";
                }

                if (objPersona.telefono != null)
                {
                    comando.Parameters.Add("@telefonoPersona", System.Data.SqlDbType.VarChar).Value = objPersona.telefono;
                    telefono = "@telefonoPersona";
                }
                else
                {
                    telefono = "NULL";
                }

                if (objPersona.foto != null)
                {
                    comando.Parameters.Add("@fotoPersona", System.Data.SqlDbType.Binary).Value = objPersona.foto;
                    foto = "@fotoPersona";
                }
                else
                {
                    foto = "NULL";
                }

                if (objPersona.direccion != null)
                {
                    comando.Parameters.Add("@direccion", System.Data.SqlDbType.VarChar).Value = objPersona.direccion;
                    direccion = "@direccion";
                }
                else
                {
                    direccion = "NULL";
                }

                comando.CommandText = "Insert Into PD_Personas (NombrePersona,ApellidosPersona,IDDepartamento,FechaNacimientoPersona,TelefonoPersona,FotoPersona,Direccion) " +
                                      "Values(" + nombre + "," + apellidos + "," + idDepartamento + "," + fechaNacimiento + "," + telefono + "," + foto + "," + direccion + ")";

                filas = comando.ExecuteNonQuery();
            }catch (SqlException e) {
                throw e;
            }
            finally {
                miConexion.closeConnection(ref conexion);
            }

            return(filas);
        }