ACCP S1 C#第十七章

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;

namespace no17
{
    class Program
    {
        static void Main(string[] args)
        {
            dbhandle d = new dbhandle();
            d.login();
        }
    }
}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;

namespace no17
{
    class usermanager
    {
        
        public void showmenu() {
            bool b=true;
            do
            {
                dbhandle d = new dbhandle();
                Console.WriteLine("=======欢迎登陆QQ用户信息管理系统=======");
                Console.WriteLine("--------------请选择菜单项--------------");
                Console.WriteLine("1.显示用户清单");
                Console.WriteLine("2.更新在线天数");
                Console.WriteLine("3.添加用户新纪录");
                Console.WriteLine("4.更新用户等级");
                Console.WriteLine("5.删除用户记录");
                Console.WriteLine("0.退出");
                Console.WriteLine("========================================");
                string i = Console.ReadLine();
                switch (i)
                {
                    case "1":
                        d.getuserlist();
                        break;
                    case "2":
                        d.updateonlineday();
                        break;
                    case "3":
                        d.insertuserinfo();
                        break;
                    case "4":
                        d.updatelevel();
                        break;
                    case "5":
                        d.deleteuserinfo();
                        break;
                    case "0":
                        Console.WriteLine("是否退出(y/n)");
                        string answer = Console.ReadLine();
                        if (answer=="y")
                        {
                            b = false;
                        }
                        break;
                }
            } while (b);
        }
    }
}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;

namespace no17
{
    class dbhandle
    {
        usermanager u = new usermanager();
        public void login() {
             string s;
             string ss;
             int j;
            for(int i=0;i<3;){
            s = "Data Source=.;Initial Catalog=QQDB;Integrated Security=True";
            SqlConnection c = new SqlConnection(s);
            Console.WriteLine("请输入用户名:");
            string name = Console.ReadLine();
            Console.WriteLine("请输入密码:");
            string pwd =Console.ReadLine();
            c.Open();
            ss = "select count(*) from admin where loginid='"+name+"' and loginpwd='"+pwd+"'";
            SqlCommand d = new SqlCommand(ss, c);
            j = (int)d.ExecuteScalar();
                if (j != 0)
                {
                    Console.WriteLine("登陆成功");
                    c.Close();
                    u.showmenu();
                    break;
                }
                else { 
                    i++;
                    if (i == 3)
                    {
                        Console.WriteLine("三次失败,退出");
                        break;
                    }
                    Console.WriteLine("登陆失败");
                }
            c.Close();
            }
            Console.ReadLine();
        }
        public void getuserlist() {
            string s = "Data Source=.;Initial Catalog=QQDB;Integrated Security=True";
            SqlConnection c = new SqlConnection(s);
            c.Open();
            StringBuilder sb = new StringBuilder();
            sb.Append("select * from userinfo u inner join level l on l.levelid=u.levelid");
            SqlCommand d = new SqlCommand(sb.ToString(), c);
            SqlDataReader r = d.ExecuteReader();
            Console.WriteLine("ID   用户名   用户密码   等级   电子邮箱   在线天数");
            while (r.Read()) {
                Console.WriteLine(r["userid"] + "    " + r["username"] + "    " + r["userpwd"] + "    " + r["levelname"] + "    " + r["email"] + "    " + r["onlineday"]);
            }
            r.Close();
            c.Close();
            Console.ReadLine();
        }
        public void updateonlineday(){
            string s = "Data Source=.;Initial Catalog=QQDB;Integrated Security=True";
            SqlConnection c = new SqlConnection(s);
            c.Open();
            Console.WriteLine("请输入用户ID:");
            string name=Console.ReadLine();
            Console.WriteLine("请输入新的天数:");
            float day =float.Parse(Console.ReadLine());
            StringBuilder sb = new StringBuilder();
            sb.Append("update userinfo set onlineday='"+day+"' where userid='"+name+"'");
            SqlCommand d = new SqlCommand(sb.ToString(), c);
            int i = d.ExecuteNonQuery();
            if (i != 0)
            {
                Console.WriteLine("修改成功!");
            }
            else {
                Console.WriteLine("修改失败!");
            }
            c.Close();
            Console.ReadLine();
        }
        public void insertuserinfo()
        {
            string s = "Data Source=.;Initial Catalog=QQDB;Integrated Security=True";
            SqlConnection c = new SqlConnection(s);
            c.Open();
            Console.WriteLine("请输入用户昵称:");
            string name = Console.ReadLine();
            Console.WriteLine("请输入密码:");
            string pwd = Console.ReadLine();
            Console.WriteLine("请输入邮箱地址:");
            string em = Console.ReadLine();
            StringBuilder sb = new StringBuilder();
            sb.Append("insert into userinfo(username,userpwd,email) values('" + name + "','" + pwd + "','" + em + "')");
            SqlCommand d = new SqlCommand(sb.ToString(), c);
            int i = d.ExecuteNonQuery();
            if (i != 0)
            {
                string a = "select userid from userinfo where username='"+name+"'";
                SqlCommand m = new SqlCommand(a,c);
                int j = (int)m.ExecuteScalar();
                Console.WriteLine("插入成功!用户编号是:"+j);
            }
            else
            {
                Console.WriteLine("插入失败!");
            }
            c.Close();
            Console.ReadLine();
        }
        public void updatelevel()
        {
            string s = "Data Source=.;Initial Catalog=QQDB;Integrated Security=True";
            SqlConnection c = new SqlConnection(s);
            c.Open();
            StringBuilder sb = new StringBuilder();
            sb.Append("update userinfo set levelid=1 where onlineday>0 and onlineday<5");
            sb.Append("update userinfo set levelid=2 where onlineday>4 and onlineday<32");
            sb.Append("update userinfo set levelid=3 where onlineday>31 and onlineday<320");
            sb.Append("update userinfo set levelid=4 where onlineday>319");
            SqlCommand d = new SqlCommand(sb.ToString(), c);
            int i = d.ExecuteNonQuery();
            if (i != 0)
            {
                Console.WriteLine("本次共更新用户记录数:"+i);
                Console.WriteLine("更新成功!");
            }
            else
            {
                Console.WriteLine("修改失败!");
            }
            c.Close();
            Console.ReadLine();
        }
        public void deleteuserinfo()
        {
            string s = "Data Source=.;Initial Catalog=QQDB;Integrated Security=True";
            SqlConnection c = new SqlConnection(s);
            c.Open();
            Console.WriteLine("请输入用户ID:");
            string name = Console.ReadLine();
            StringBuilder sb = new StringBuilder();
            sb.Append("select * from userinfo where userid='" + name + "'");
            SqlCommand d = new SqlCommand(sb.ToString(), c);
            int i = d.ExecuteNonQuery();
            if (i != 0)
            {
                SqlDataReader r = d.ExecuteReader();
                r.Read();
                Console.WriteLine("将要删除的用户信息是:");
                Console.WriteLine(r["userid"] + "   " + r["username"] + "   " + r["levelid"] + "   " + r["email"] + "   " + r["onlineday"]);
                Console.WriteLine("要删除该用户记录吗?(y/n)");
                string answer = Console.ReadLine();
                if (answer == "y")
                {
                    StringBuilder sb1 = new StringBuilder();
                    sb1.Append("delete from userinfo where userid='" + name + "'");
                    SqlCommand d1 = new SqlCommand(sb1.ToString(), c);
                    Console.WriteLine("删除成功!");
                }
            }
            else
            {
                Console.WriteLine("删除失败!");
            }
            c.Close();
            Console.ReadLine();
        }
    }
}


你可能感兴趣的:(ACCP S1 C#第十七章)