C# ,十七章 项目案例: QQ用户信息管理系统

  测试:
namespace ConsoleApplication1
{
    class Program
    {
        

        static void Main(string[] args)
        {
            UserManager manger = new UserManager();
            manger.Login();
        }
    }
}
 
  
 
DB类 :DB类 :DB类 :
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
namespace ConsoleApplication1
{
    class DBHandle
    {
        string strConn = "Data Source=.;Initial Catalog=QQDB;Integrated Security=True";
        public bool CheckAdminInfo(string userName, string pwd, ref string strMsg)
        {
            SqlConnection conn = new SqlConnection(strConn);
            try
            {
                string strSql = "select count(*) from Admin where LoginId='" + userName + "' and LoginPwd='" + pwd + "'";
                conn.Open();
                SqlCommand comm = new SqlCommand(strSql, conn);
                int iRet = (int)comm.ExecuteScalar();
                if (iRet != 1)
                {
                    strMsg = "输入无效!";
                    return false;
                }
                else
                {
                    return true;
                }



            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                strMsg = "发生异常!";
                return false;
            }
            finally
            {

                conn.Close();
            }
        }
        public SqlDataReader GetUserList()
        {
            try
            {
                SqlConnection conn = new SqlConnection(strConn);
                conn.Open();

                StringBuilder sb = new StringBuilder();
                sb.AppendLine(" SELECT");
                sb.AppendLine("           a.[UserId]");
                sb.AppendLine("          ,a.[UserName]");
                sb.AppendLine("          ,b.[LevelName]");
                sb.AppendLine("          ,a.[Email]");
                sb.AppendLine("          ,a.[OnLineDay]");
                sb.AppendLine(" FROM");
                sb.AppendLine("             [UserInfo] a, [Level] b ");
                sb.AppendLine(" WHERE");
                sb.AppendLine("           a.[LevelId] = b.[LevelId]");
                SqlCommand comm = new SqlCommand(sb.ToString(), conn);

                return comm.ExecuteReader();
            }
            catch (Exception)
            {
                return null;
            }
        }
         
        public SqlDataReader GetUserIdAndOnlineDay()
        {
            try
            {
                SqlConnection conn = new SqlConnection(strConn);
                conn.Open();

                StringBuilder sb = new StringBuilder();
                sb.AppendLine(" SELECT");
                sb.AppendLine("           [UserId]");
                sb.AppendLine("          ,[OnLineDay]");
                sb.AppendLine(" FROM");
                sb.AppendLine("           [UserInfo] ");
                SqlCommand comm = new SqlCommand(sb.ToString(), conn);
                return comm.ExecuteReader();
            }
            catch (Exception)
            {
                return null;
            }
        }
        public int UpdateOnlineDay(int userId, double newOnlineDay)
        {
            try
            {
                SqlConnection conn = new SqlConnection(strConn);
                conn.Open();

                StringBuilder sb = new StringBuilder();
                sb.AppendLine("UPDATE");
                sb.AppendLine("           [UserInfo]");
                sb.AppendLine("SET");
                sb.AppendLine("          [OnLineDay]=" + newOnlineDay);
                sb.AppendLine("WHERE");
                sb.AppendLine("          [UserId]=" + userId);
                SqlCommand comm = new SqlCommand(sb.ToString(), conn);
                return comm.ExecuteNonQuery();
            }
            catch (Exception)
            {
                return -1;
            }
        }
        public int UpdateUserLevel(int userId, int iLevel)
        {
            try
            {
                SqlConnection conn = new SqlConnection(strConn);
                conn.Open();

                StringBuilder sb = new StringBuilder();
                sb.AppendLine(" UPDATE");
                sb.AppendLine("           [UserInfo]");
                sb.AppendLine(" SET");
                sb.AppendLine("           [LevelId]=" + iLevel);
                sb.AppendLine(" WHERE");
                sb.AppendLine("           [UserId]=" + userId);
                SqlCommand comm = new SqlCommand(sb.ToString(), conn);
                return comm.ExecuteNonQuery();
            }
            catch (Exception)
            {
                return -1;
            }
        }
        public object InsertUserInfo(string userName, string userPwd, string email)
        {
            SqlConnection conn = new SqlConnection(strConn);
            try
            {
                conn.Open();

                StringBuilder sb = new StringBuilder();
               
                sb.AppendLine(" INSERT INTO");
                sb.AppendLine("          [UserInfo]");
                sb.AppendLine(" VALUES");
                sb.AppendLine("          ('" + userName + "','" + userPwd + "',1,'" + email + "',0);");
                
                sb.AppendLine(" SELECT @@Identity;");

                SqlCommand comm = new SqlCommand(sb.ToString(), conn);
               
                return comm.ExecuteScalar();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                return -1;
            }
        }

    }
}

User类:
 
  
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
namespace ConsoleApplication1
{
    class UserManager
    {
        private DBHandle _dbHandle = new DBHandle();
        private string EXCEPT;
        private string ERRMSG;
        public void Login()  //登录
        {
            int count = 0;
            do
            {
                string sUserName = string.Empty;
                string sPwd = string.Empty;

                count++; 
                
                Console.WriteLine("请输入用户名:");
                sUserName = Console.ReadLine();
                Console.WriteLine("请输入密码:");
                sPwd = Console.ReadLine();
                if (sUserName.Equals(string.Empty)|| sPwd.Equals(string.Empty))
                {
                    Console.WriteLine("输入错误!重新输入!!");
                    continue;


                }
                else
                {
                    string strMsg = string.Empty;
                 
                    bool bR = _dbHandle.CheckAdminInfo(sUserName, sPwd, ref strMsg);
                    if (bR)
                    { Console.WriteLine("登录成功!");
                        
                        ShowMenu();
                        break;

                    }
                    else
                    {
                        Console.WriteLine("登录失败:" + strMsg + "\n");
                        continue;
                    }
                
                
                }
            } while (count < 3);
             if (count == 3)
                Console.WriteLine("\n连续三次登录失败,退出本系统!\n");
        }
        private void ShowMenu()
        {
            string option = "";
            do
            {
                Console.WriteLine("");
                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("=========================================");
                option = Console.ReadLine();
                switch (option)
                {   case "1":
                        ShowUserInfo();
                        continue;
                    case "2": 
                        UpdateOnLineDay();
                        continue;
                    case "3":
                        
                        InsertUserInfo();
                        continue;
                    case "4":
                        UpdateUserLevel();
                        continue;
                    case "5":
                       // DeleteUserInfo();
                        continue;
                    case "0":
                        if (IsExit())
                        {
                              break;
                      }
                       else
                     {
                          continue;
                     }
                   default:
                        continue;
               }
                 
                     break;
                
                
            } while (true);
	
                        }
        private void ShowUserInfo()
        {
            SqlDataReader reader = _dbHandle.GetUserList();
            if (reader == null)
            {
                Console.WriteLine(EXCEPT);
                return;
            }
            DisplayUserInfo(reader);
        }

        
        private void DisplayUserInfo(SqlDataReader reader)
        {
            Console.WriteLine("--------------------------------------------------------------------------------");
            Console.WriteLine("编号\t昵称\t\t等级\t\t邮箱\t\t在线天数");
            Console.WriteLine("--------------------------------------------------------------------------------");
            while (reader.Read())
            {
                Console.Write(reader["UserId"] + "\t");
                Console.Write(reader["UserName"] + "\t");
                Console.Write(ShowDesign((String)reader["LevelName"])+ "\t\t");
                Console.Write(reader["Email"] + "\t");
                Console.WriteLine(reader["OnLineDay"]);
            }
            Console.WriteLine("--------------------------------------------------------------------------------");
        
        
        }

        private string ShowDesign(string strLevel)
        {
            string strDesign = string.Empty;
            switch (strLevel)
            {
                case "无等级":
                    strDesign = "―";
                    break;
                case "星星":
                    strDesign = "☆";
                    break;
                case "月亮":
                    strDesign = "€";
                    break;
                case "太阳":
                    strDesign = "◎";
                    break;
                default:
                    break;
            }
            return strDesign;
        } 
        private void UpdateOnLineDay()
        {
            try
            {
               
                Console.WriteLine("请输入用户编号:");
               
                string strUserId = Console.ReadLine();
                int iUserId = Convert.ToInt32(strUserId);
               
                Console.WriteLine("请输入新的在线天数");
                string strNewOnlineDay = Console.ReadLine();
                double iNewOnlineDay = Convert.ToDouble(strNewOnlineDay);
              
                int iRet = _dbHandle.UpdateOnlineDay(iUserId, iNewOnlineDay);
                if (iRet == -1)
                    Console.WriteLine(ERRMSG);
                else if (iRet == 0)
                {
                    Console.WriteLine("用户记录不存在");
                }
                else
                {
                    Console.WriteLine("修改成功!");
                }
            }
            catch (Exception)
            {
                Console.WriteLine(EXCEPT);
            }
        }    private void InsertUserInfo()
        {
            Console.WriteLine("请输入用户昵称:");
            string strUserName = Console.ReadLine();
            Console.WriteLine("请输入用户密码:");
            string strUserPwd = Console.ReadLine();
            Console.WriteLine("请输入用户邮箱地址:");
            string strUserEmail = Console.ReadLine();
         
            int iRet = Convert.ToInt32(_dbHandle.InsertUserInfo(strUserName, strUserPwd, strUserEmail));
            if (iRet == -1)
            {
                Console.WriteLine(EXCEPT);
            }
            else if (iRet == 0)
            {
                Console.WriteLine("用户记录不存在");
            }
            else
            {
                Console.WriteLine("插入成功!用户编号是:" + iRet);
            }
        }
        private int JudgeLevelByOnLineDay(double iOnlineDay)
        {
            const int LEVEL1 = 5;
            const int LEVEL2 = 32;
            const int LEVEL3 = 320;

            int iNewLevel = 0;//等级

            if (iOnlineDay >= LEVEL1 && iOnlineDay < LEVEL2)
            {
                iNewLevel = 2;
            }
            else if (iOnlineDay >= LEVEL2 && iOnlineDay < LEVEL3)
            {
                iNewLevel = 3;
            }
            else if (iOnlineDay >= LEVEL3)
            {
                iNewLevel = 4;
            }
            else
            {
                iNewLevel = 1;
            }
            return iNewLevel;
        }
        private void UpdateUserLevel()
        {
          
            SqlDataReader reader = _dbHandle.GetUserIdAndOnlineDay();
            if (reader == null)
            {
                Console.WriteLine(EXCEPT);
                return;
            }

       
            int iUserId = 0;     //用户编号
            double iLineDay = 0; 
            int iLevelId = 0;    //用户等级
            int count = 0;       
           
            while (reader.Read())
            {
                iUserId = Convert.ToInt32(reader["UserId"]);//用户编号的类型转换
                iLineDay = Convert.ToDouble(reader["OnLineDay"]);
                iLevelId = JudgeLevelByOnLineDay(iLineDay);
                _dbHandle.UpdateUserLevel(iUserId, iLevelId);
                count++;
            }
            Console.WriteLine("本次共更新用户记录数:{0}", count);
            Console.WriteLine("更新成功!");
          

        }
        private bool IsExit()
        {
            Console.WriteLine("是否退出?(Y/N)");
            string strRet = Console.ReadLine();
            strRet = strRet.Trim().ToUpper();
            if (strRet.Equals("Y"))
            {
                return true;
            }
            else
            {
                return false;
            }
        }
       }
   
}

你可能感兴趣的:(C# ,十七章 项目案例: QQ用户信息管理系统)