Beispiel #1
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 #2
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 #3
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 #4
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);
        }