Пример #1
0
        public bool deleteUsuario(ENUsuario en)
        {
            bool          ret = true;
            SqlConnection c   = new SqlConnection(constring);

            try
            {
                c.Open();
                SqlCommand com = new SqlCommand("DELETE FROM Usuarios WHERE " +
                                                "nif='" + en.nifUsuario + "' ", c);
                int affected = com.ExecuteNonQuery();
                if (affected == 0)
                {
                    ret = false;
                    Console.WriteLine("User operation has failed. No hay usuarios con los datos indicados para actualizar.");
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("User operation has failed. Error: {0}", ex.Message);
                ret = false;
            }
            finally
            {
                c.Close();
            }
            return(ret);
        }
Пример #2
0
        public bool updateUsuario(ENUsuario en)
        {
            bool          ret = true;
            SqlConnection c   = new SqlConnection(constring);

            try
            {
                c.Open();
                SqlCommand com = new SqlCommand("Update Usuarios set " +
                                                "nombre='" + en.nombreUsuario + "', " +
                                                "edad='" + en.edadUsuario + "' " +
                                                "where nif='" + en.nifUsuario + "'", c);
                int affected = com.ExecuteNonQuery();
                if (affected == 0)
                {
                    ret = false;
                    Console.WriteLine("User operation has failed. No hay usuarios con el nif indicado para actualizar.");
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("User operation has failed. Error: {0}", ex.Message);
                ret = false;
            }
            finally
            {
                c.Close();
            }
            return(ret);
        }
Пример #3
0
        public bool readFirstUsuario(ENUsuario en)
        {
            SqlConnection conn    = new SqlConnection(constring);
            string        comando = "SELECT * " + "FROM [dbo].[Usuarios]";

            try
            {
                conn.Open();
                SqlCommand    cmd = new SqlCommand(comando, conn);
                SqlDataReader dr  = cmd.ExecuteReader();
                dr.Read();
                en.edad   = int.Parse(dr["edad"].ToString());
                en.nombre = dr["nombre"].ToString();
                en.nif    = dr["nif"].ToString();
                conn.Close();
                return(true);
            }
            catch (Exception)
            {
                conn.Close();
                return(false);
            }
            finally
            {
                conn.Close();
            }
        }
Пример #4
0
        public bool readFirstUsuario(ENUsuario en)
        {
            try
            {
                SqlConnection con = new SqlConnection(constring); //creamos la conexion
                con.Open();                                       //abrimos la conexion

                string consulta = "Select * FROM [dbo].[Usuarios]";

                SqlCommand    com     = new SqlCommand(consulta, con);
                SqlDataReader reading = com.ExecuteReader();
                reading.Read();


                en.nombreUsuario = reading["nombre"].ToString();
                en.nifUsuario    = reading["nif"].ToString();
                en.edadUsuario   = int.Parse(reading["edad"].ToString());


                reading.Close();
                con.Close();

                return(true);;
            }
            catch (SqlException ex)
            {
                Console.WriteLine("User operation has failed.Error: {0}", ex.Message);
                return(false);
            }
            catch (Exception ex)
            {
                Console.WriteLine("User operation has failed.Error: {0}", ex.Message);
                return(false);
            }
        }
Пример #5
0
        public bool updateUsuario(ENUsuario en)
        {
            bool cambiar = false;

            SqlConnection c   = new SqlConnection(constring);
            ENUsuario     usu = en;

            try
            {
                c.Open();
                SqlCommand com = new SqlCommand("update Usuarios set Nombre = '" + usu.nombreUsuario + "', edad =  " + usu.edadUsuario + "  where nif = '" + usu.nifUsuario + "'", c);
                com.ExecuteNonQuery();
                cambiar = true;
            }
            catch (SqlException ex)
            {
                cambiar = false;
                throw new CADException("No se ha podudo actualizar el usuario", ex);
            }
            catch (Exception ex)
            {
                Console.WriteLine("User operation has failed. Error: {0}", ex);
            }
            finally
            {
                c.Close();
            }
            return(cambiar);
        }
Пример #6
0
        public bool deleteUsuario(ENUsuario en)
        {
            bool borrar = false;

            SqlConnection c   = new SqlConnection(constring);
            ENUsuario     usu = en;

            try
            {
                c.Open();
                SqlCommand com = new SqlCommand("Delete from Usuarios where nif= '" + usu.nifUsuario + "'", c);
                com.ExecuteNonQuery();
                borrar = true;
            }
            catch (SqlException ex)
            {
                borrar = false;
                throw new CADException("No se ha podido borrar el usuario", ex);
            }
            catch (Exception ex)
            {
                Console.WriteLine("User operation has failed. Error: {0}", ex);
            }
            finally
            {
                c.Close();
            }
            return(borrar);
        }
Пример #7
0
        public bool readUsuario(ENUsuario en)
        {
            SqlConnection conn    = new SqlConnection(constring);
            String        comando = "select * from [dbo].[Usuarios] where nif='" + en.nif + "'";

            try
            {
                conn.Open();
                SqlCommand    cmd = new SqlCommand(comando, conn);
                SqlDataReader dr  = cmd.ExecuteReader();
                dr.Read();
                if (dr["nif"].ToString() == en.nif)
                {
                    en.nombre = dr["nombre"].ToString();
                    en.nif    = dr["nif"].ToString();
                    en.edad   = int.Parse(dr["edad"].ToString());
                    dr.Close();
                    conn.Close();
                    return(true);
                }
                return(false);
            }
            catch (Exception)
            {
                conn.Close();
                return(false);
            }
        }
Пример #8
0
        public bool createUsuario(ENUsuario en)
        {
            bool          crear = false;
            ENUsuario     usu   = en;
            SqlConnection c     = new SqlConnection(constring);

            try
            {
                c.Open();
                SqlCommand com = new SqlCommand("insert into Usuarios (nombre, nif, edad) values ('" + usu.nombreUsuario + "', '" + usu.nifUsuario + "'," + usu.edadUsuario + ")", c);
                com.ExecuteNonQuery();
                crear = true;
                c.Close();
            }
            catch (SqlException sqlex)
            {
                throw new CADException("El usuario no se ha podido crear", sqlex);
            }
            catch (Exception ex)
            {
                Console.WriteLine("User operation has failed. Error: {0}", ex);
            }
            finally
            {
                c.Close();
            }
            return(crear);
        }
Пример #9
0
        public bool deleteUsuario(ENUsuario en)
        {
            try
            {
                SqlConnection con = new SqlConnection(constring); //creamos la conexion
                con.Open();                                       //abrimos la conexion

                string consulta = "DELETE FROM [dbo].[Usuarios] WHERE nif = '" + en.nifUsuario + "'";

                SqlCommand com = new SqlCommand(consulta, con);

                com.ExecuteNonQuery();
                con.Close();

                return(true);
            }
            catch (SqlException ex)
            {
                Console.WriteLine("User operation has failed.Error: {0}", ex.Message);
                return(false);
            }
            catch (Exception ex)
            {
                Console.WriteLine("User operation has failed.Error: {0}", ex.Message);
                return(false);
            }
        }
Пример #10
0
        public bool createUsuario(ENUsuario en)

        {
            SqlConnection conn = new SqlConnection(constring);

            string comando = "Insert Into [dbo].[Usuarios] (nif, nombre, edad) " + "VALUES ('" + en.nif + "', '" + en.nombre + "', " + en.edad + ")";

            try

            {
                conn.Open();

                SqlCommand cmd = new SqlCommand(comando, conn);

                cmd.ExecuteNonQuery();

                conn.Close();
            }

            catch (Exception e)

            {
                conn.Close();

                Console.WriteLine("Error: " + e);

                return(false);
            }

            return(true);
        }
Пример #11
0
        public bool deleteUsuario(ENUsuario en)

        {
            SqlConnection conn = new SqlConnection(constring);;

            string comando = "Delete from [dbo].[Usuarios] where nif = '" + en.nif + "'";

            try

            {
                conn.Open();

                SqlCommand cmd = new SqlCommand(comando, conn);

                cmd.ExecuteNonQuery();
            }

            catch (Exception)

            {
                conn.Close();

                return(false);
            }

            finally

            {
                conn.Close();
            }

            return(true);
        }
Пример #12
0
        public bool updateUsuario(ENUsuario en)

        {
            SqlConnection conn = new SqlConnection(constring);

            string comando = "UPDATE [dbo].[Usuarios] " + "SET nombre = '" + en.nombre + "',  edad = " + en.edad + "where nif ='" + en.nif + "'";

            try

            {
                conn.Open();

                SqlCommand cmd = new SqlCommand(comando, conn);

                cmd.ExecuteNonQuery();
            }

            catch (Exception)

            {
                conn.Close();

                return(false);
            }

            finally

            {
                conn.Close();
            }



            return(true);
        }
Пример #13
0
        public bool readPrevUsuario()
        {
            CADUsuario c          = new CADUsuario();
            ENUsuario  auxUsuario = new ENUsuario(nif, nombre, edad);

            if (c.readFirstUsuario(auxUsuario) && nifUsuario != auxUsuario.nifUsuario)
            {
                return(c.readPrevUsuario(this));
            }

            return(false);
        }
Пример #14
0
        public bool updateUsuario()
        {
            ENUsuario  auxUsuario = new ENUsuario(this.nif, this.nombre, this.edad);
            CADUsuario c          = new CADUsuario();

            if (c.readUsuario(this))
            {
                this.nombre = auxUsuario.nombre;
                this.nif    = auxUsuario.nif;
                this.edad   = auxUsuario.edad;
                return(c.updateUsuario(this));
            }

            return(false);
        }
Пример #15
0
        public bool readPrevUsuario(ENUsuario en)
        {
            bool          ret      = false;
            SqlConnection c        = new SqlConnection(constring);
            ENUsuario     prevUser = null;

            try
            {
                c.Open();
                SqlCommand    com = new SqlCommand("Select * from Usuarios", c);
                SqlDataReader dr  = com.ExecuteReader();
                while (dr.Read())
                {
                    if (en.nifUsuario == dr["nif"].ToString())
                    {
                        if (prevUser == null)
                        {
                            Console.WriteLine("User operation has failed. No hay usuarios antes del indicado.");
                        }
                        else
                        {
                            ret              = true;
                            en.nifUsuario    = prevUser.nifUsuario;
                            en.nombreUsuario = prevUser.nombreUsuario;
                            en.edadUsuario   = prevUser.edadUsuario;
                        }
                        break;
                    }
                    else
                    {
                        prevUser = new ENUsuario(dr["nif"].ToString(), dr["nombre"].ToString(), Int32.Parse(dr["edad"].ToString()));
                    }
                }
                dr.Close();
            }
            catch (Exception ex)
            {
                Console.WriteLine("User operation has failed. Error: {0}", ex.Message);
                ret = false;
            }
            finally
            {
                c.Close();
            }

            return(ret);
        }
Пример #16
0
        public bool readUsuario(ENUsuario en)
        {
            bool lectura = false;

            SqlConnection c   = new SqlConnection(constring);
            ENUsuario     usu = en;

            try
            {
                c.Open();

                SqlCommand    com = new SqlCommand("select * from Usuarios where nif='" + usu.nifUsuario + "'", c);
                SqlDataReader dr  = com.ExecuteReader();
                if (dr.Read())
                {
                    en.nifUsuario    = dr["nif"].ToString();
                    en.nombreUsuario = dr["nombre"].ToString();
                    en.edadUsuario   = int.Parse(dr["edad"].ToString());
                }
                else
                {
                    return(false);
                }
                dr.Close();

                lectura = true;
            }
            catch (SqlException ex)
            {
                throw new CADException("El usuario no existe en la base de datos", ex);
            }
            catch (Exception ex)
            {
                Console.WriteLine("User operation has failed. Error: {0}", ex);
            }
            finally
            {
                c.Close();
            }
            return(lectura);
        }
Пример #17
0
        public bool readPrevUsuario(ENUsuario en)
        {
            SqlConnection conn    = new SqlConnection(constring);
            string        comando = "SELECT * " + "FROM [dbo].[Usuarios]";

            try
            {
                conn.Open();
                SqlCommand    cmd   = new SqlCommand(comando, conn);
                SqlDataReader dr    = cmd.ExecuteReader();
                ENUsuario     enaux = new ENUsuario();
                while (dr.Read())
                {
                    if (dr["nif"].ToString() == en.nif.ToString())
                    {
                        en.edad   = enaux.edad;
                        en.nombre = enaux.nombre;
                        en.nif    = enaux.nif;
                        return(true);
                    }
                    else
                    {
                        enaux.edad   = int.Parse(dr["edad"].ToString());
                        enaux.nombre = dr["nombre"].ToString();
                        enaux.nif    = dr["nif"].ToString();
                    }
                }
            }
            catch (Exception)
            {
                conn.Close();
                return(false);
            }
            finally
            {
                conn.Close();
            }

            return(false);
        }
Пример #18
0
        public bool createUsuario(ENUsuario en)
        {
            try
            {
                SqlConnection con = new SqlConnection(constring); //creamos la conexion
                con.Open();                                       //abrimos la conexion

                string consulta = "Insert INTO [dbo].[Usuarios] (nombre,nif,edad) VALUES ('" + en.nombreUsuario + "','" + en.nifUsuario + "', '" + en.edadUsuario + "')";

                SqlCommand com = new SqlCommand(consulta, con);
                com.ExecuteNonQuery();
                con.Close();
                return(true);
            }catch (SqlException ex) {
                Console.WriteLine("User operation has failed.Error: {0}", ex.Message);
                return(false);
            }catch (Exception ex)
            {
                Console.WriteLine("User operation has failed.Error: {0}", ex.Message);
                return(false);
            }
        }
Пример #19
0
        public bool readFirstUsuario(ENUsuario en)
        {
            bool lectura = false;

            SqlConnection c = new SqlConnection(constring);

            try
            {
                ENUsuario usu = en;
                c.Open();
                SqlCommand    com = new SqlCommand("Select nif, nombre, edad From Usuarios where id= '1'", c);
                SqlDataReader dr  = com.ExecuteReader();

                if (dr.Read())
                {
                    en.nifUsuario    = dr["nif"].ToString();
                    en.nombreUsuario = dr["nombre"].ToString();
                    en.edadUsuario   = int.Parse(dr["edad"].ToString());
                }
                dr.Close();

                lectura = true;
            }
            catch (SqlException ex)
            {
                lectura = false;
                throw new CADException("No hay usuarios en la base de datos", ex);
            }
            catch (Exception ex)
            {
                Console.WriteLine("User operation has failed. Error: {0}", ex);
            }
            finally
            {
                c.Close();
            }
            return(lectura);
        }
Пример #20
0
        public bool readUsuario(ENUsuario en)
        {
            bool          ret = true;
            SqlConnection c   = new SqlConnection(constring);

            try
            {
                c.Open();
                SqlCommand com = new SqlCommand("Select * from Usuarios " +
                                                "where nif='" + en.nifUsuario + "'", c);
                SqlDataReader dr = com.ExecuteReader();
                if (dr.Read())
                {
                    ret              = true;
                    en.nifUsuario    = dr["nif"].ToString();
                    en.nombreUsuario = dr["nombre"].ToString();
                    en.edadUsuario   = Int32.Parse(dr["edad"].ToString());
                }
                else
                {
                    ret = false;
                    Console.WriteLine("User operation has failed. No hay un usuario con los campos indicados.");
                }
                dr.Close();
            }
            catch (Exception ex)
            {
                Console.WriteLine("User operation has failed. Error: {0}", ex.Message);
                ret = false;
            }
            finally
            {
                c.Close();
            }

            return(ret);
        }
Пример #21
0
        public bool readPrevUsuario(ENUsuario en)
        {
            bool          lectura = false;
            SqlConnection c       = new SqlConnection(constring);

            try
            {
                ENUsuario usu = en;
                c.Open();
                SqlCommand    com = new SqlCommand("select * from Usuarios where id < (select id from Usuarios where nif ='" + en.nifUsuario + "')  order by id desc", c);
                SqlDataReader dr  = com.ExecuteReader();

                dr.Read();

                en.nifUsuario    = dr["nif"].ToString();
                en.nombreUsuario = dr["nombre"].ToString();
                en.edadUsuario   = int.Parse(dr["edad"].ToString());

                dr.Close();

                lectura = true;
            }
            catch (SqlException ex)
            {
                lectura = false;
                throw new CADException("No existe un usuario anterior", ex);
            }
            catch (Exception ex)
            {
                Console.WriteLine("User operation has failed. Error: {0}", ex);
            }
            finally
            {
                c.Close();
            }
            return(lectura);
        }
Пример #22
0
        public bool createUsuario(ENUsuario en)
        {
            bool          ret = true;
            SqlConnection c   = new SqlConnection(constring);

            try
            {
                c.Open();
                SqlCommand com = new SqlCommand("INSERT INTO Usuarios (nif, nombre, edad) " +
                                                "VALUES ('" + en.nifUsuario + "', '" + en.nombreUsuario + "', '" + en.edadUsuario + "')", c);
                com.ExecuteNonQuery();
            }
            catch (SqlException ex)
            {
                Console.WriteLine("User operation has failed. Error: {0}", ex.Message);
                ret = false;
            }
            finally
            {
                c.Close();
            }

            return(ret);
        }