QQ管理器

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

namespace QQ信息管理系统
{
    class UserManager
    {
       
        public void Login()
        {
            int count = 0;
            do
            {
                string strUserName = string.Empty;//初始化管理员登录名
                string strPwd = string.Empty;//初始化管理员密码

                count++;

                Console.WriteLine("请输入用户名:");
                strUserName = Console.ReadLine();
                Console.WriteLine("请输入密码:");
                strPwd = Console.ReadLine();

                //非空验证
                if (strUserName.Equals(string.Empty) || strPwd.Equals(string.Empty))
                {
                    Console.WriteLine("输入错误,请重新输入!\n");
                    continue;//重新输入用户名和密码
                }
                else
                {
                    // 需返回的结果信息
                    string strMsg = string.Empty;
                    //数据库验证
                    bool bRet = a.CheckAdminInfo(strUserName, strPwd, ref strMsg);
                    if (bRet)
                    {
                        Console.WriteLine("登录成功!");
                        // 显示菜单
                        ShowMenu();                           //在此处添加菜单名
                        break;//退出程序
                    }
                    else
                    {
                        Console.WriteLine("登录失败:" + strMsg + "\n");
                        continue;//重新输入用户名和密码
                    }
                }
            } while (count < 3);
            if (count == 3)
                Console.WriteLine("\n连续三次登录失败,退出本系统!\n");
        }
   
        private DBHandle a = new DBHandle();                   //创建DBHandle的实例
        const String EXCEPT = "出现异常。请与系统管理员联系!";
        private void ShowMenu()
        {
            DBHandle a = new DBHandle();
            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"://显示用户信息
                        a.GetUserList();
                        continue;
                    case "2"://更新在线天数   
                        UpdateOnLineDay();
                        continue;
                    case "3"://添加用户     
                        InsertUserInfo();
                        continue;
                    case "4"://更新用户等级 
                        UpdateUserLevel();
                        continue;
                    case "5"://删除用户
                        continue;
                    case "0":
                        if (IsExit())
                        {
                            break;//退出
                        }
                        else
                        {
                            continue;
                        }
                    default:
                        continue;
                }

                break;
            } while (true);

        }
         

        private bool IsExit()
        {
            bool flag = true;
            Console.WriteLine("是否退出?(Y/N)");
            string strRet = Console.ReadLine();
            strRet = strRet.Trim().ToUpper();
            if (strRet.Equals("Y"))
            {
                flag= true;
            }
            else
            {
                flag= false;
            }
            return flag;
        }
        //public string showDesign(string strLeve) {
        //    string strDesign = string.Empty;
        //    switch (strLeve)
        //    {
        //        case "无等级":
        //            strDesign = "―";
        //            break;
        //        case "星星":
        //            strDesign = "☆";
        //            break;
        //        case "月亮":
        //            strDesign = "€";
        //            break;
        //        case "太阳":
        //            strDesign = "◎";
        //            break;
        //        default:
        //            strDesign = "-----";
        //            break;
        //    }
        //    return strDesign;
        
        
        
        //}
        public string showDesign(string strLevel)
        {
            string strDesign = string.Empty;
            switch (strLevel)
            {
                case "无等级":
                    strDesign = "_";
                    break;
                case "星星":
                    strDesign = "☆";
                    break;
                case "月亮":
                    strDesign = "#";
                    break;
                case "太阳":
                    strDesign = "@";
                    break;
                //default: strDesign = "◎";
                //    break;
            }
            return strDesign;
        }
        
        //private void ShowUserInfo()
        //{
        //    SqlDataReader reader =a.GetUserList();
        //    if (reader == null)
        //    {
        //        Console.WriteLine(EXCEPT);
        //        return;
        //    }
        //    DisplayUserInfo(reader);
        //    Console.ReadLine();
        //}
        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 = a.UpdateOnlineDay(iUserId, iNewOnlineDay);
                if (iRet == -1)
                    Console.WriteLine("");
                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(a.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)//5<=在线天数<32更新为星星
            {
                iNewLevel = 2;
            }
            else if (iOnlineDay >= LEVEL2 && iOnlineDay < LEVEL3)//32<=在线天数<320更新为月亮
            {
                iNewLevel = 3;
            }
            else if (iOnlineDay >= LEVEL3)//在线天数>=320更新为太阳
            {
                iNewLevel = 4;
            }
            else
            {
                iNewLevel = 1;
            }
            return iNewLevel;
        }
        private void UpdateUserLevel()
        {
            //取得所有用户的用户编号和在线天数
            SqlDataReader reader = a.GetUserIdAndOnlineDay();
            if (reader == null)
            {
                Console.WriteLine(EXCEPT);
                return;
            }

            //Console.WriteLine("----------------------开始更新--------------------------------");
            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);//根据在线天数判定用户等级
                a.UpdateUserLevel(iUserId, iLevelId);
                count++;
            }
            Console.WriteLine("本次共更新用户记录数:", count);
            Console.WriteLine("更新成功!");
            //Console.WriteLine("----------------------更新结束---------------------------------");

        }
    }

}

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

namespace QQ信息管理系统
{
    class DBHandle
    {
        public bool CheckAdminInfo(string userName, string pwd, ref string strMsg)
    {
            //数据库连接字符串
          string strconn = "Data Source=.;Initial Catalog=QQDB;Integrated Security=True";
        //创建数据库连接
        SqlConnection conn = new SqlConnection(strconn);

  
        try
        {
            //创建Sql语句
          string strSql = "select count(*) from Admin where LoginId='" + userName + "' and LoginPwd='" + pwd + "'";
            //打开数据库
          conn.Open();
            //创建Command命令
            SqlCommand comm = new SqlCommand(strSql, conn);
            int iRet =(int) comm.ExecuteScalar();
            if (iRet != 1)
            {
                strMsg = "输入无效!";
                return false;
            }
            else
            {
                return true;
            }
        }
        catch (Exception)
        {
            strMsg = "发生异常!";
            return false;
        }
        finally
        {
            //关闭数据库连接
            conn.Close();
        }
    }
        public void GetUserList() {
           UserManager b = new UserManager();
            string strconn = "Data Source=.;Initial Catalog=QQDB;Integrated Security=True";                                     //连接数据库
            SqlConnection conn = new SqlConnection(strconn);                                                                     //创建连接类
            try
            {
                conn.Open();
                StringBuilder sb = new StringBuilder();
                sb.AppendLine("SELECT");
                sb.AppendLine("           a.UserId");
                sb.AppendLine("           ,a.UserName");
                sb.AppendLine("           ,b.levename");
                sb.AppendLine("           ,a.Email");
                sb.AppendLine("           ,a.OnLineDay");
                sb.AppendLine("FROM");
                sb.AppendLine("                [UserInfo] a,Leve b");
                sb.AppendLine("  where  ");
                sb.AppendLine("        a.LeveId=b.LeveId");

                Console.WriteLine(sb.ToString());
                SqlCommand comm = new SqlCommand(sb.ToString(), conn);
                Console.WriteLine("-------------------------------------------------------------------------");
                Console.WriteLine("编号\t昵称\t\t等级\t\t邮箱\t\t在线天数");
                Console.WriteLine("-------------------------------------------------------------------------");
               SqlDataReader w= comm.ExecuteReader();

                while (w.Read())
                {
                    string o =b.showDesign(w["LeveName"].ToString());
                    Console.WriteLine(w);
                    //Console.WriteLine(showDesign((string)reader["LeveName"]));
                    Console.WriteLine(w["UserId"].ToString() + "\t" + w["UserName"].ToString() + "\t" + o + "\t" + w["Email"].ToString() + "\t" + w["OnLineDay"].ToString());


                }
              
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Console.WriteLine("数据库操作失败!");
               
            }
            finally {
                //conn.Close();                                  //关闭连接
            }
        }
        public int UpdateOnlineDay(int userId, double newOnlineDay)
    {
        string strconn = "Data Source=.;Initial Catalog=QQDB;Integrated Security=True";
        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);
            Console.WriteLine(sb);
            SqlCommand comm = new SqlCommand(sb.ToString(), conn);
            return comm.ExecuteNonQuery();
        }
        catch (Exception)
        {
            return -1;
        }
    }
        public object InsertUserInfo(string userName, string userPwd, string email)
    {
             string strconn = "Data Source=.;Initial Catalog=QQDB;Integrated Security=True";
        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;");
            Console.WriteLine(sb);
            SqlCommand comm = new SqlCommand(sb.ToString(), conn);
       
            return comm.ExecuteScalar();
        }
        catch (Exception)
        {               
            return -1;
        }
    }
        public SqlDataReader GetUserIdAndOnlineDay()
        {
            string strconn = "Data Source=.;Initial Catalog=QQDB;Integrated Security=True";

            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 UpdateUserLevel(int userId, int iLevel)
        {
            string strconn = "Data Source=.;Initial Catalog=QQDB;Integrated Security=True";

            try
            {
                SqlConnection conn = new SqlConnection(strconn);
                conn.Open();

                StringBuilder sb = new StringBuilder();
                sb.AppendLine(" UPDATE");
                sb.AppendLine("          from [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;
            }
        }
    

    }
}

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

namespace QQ信息管理系统
{
    class Program
    {
        static void Main(string[] args)
        {
            UserManager manger = new UserManager();
            Console.WriteLine();
            manger.showDesign("星星");
            manger.Login();
        }
    }
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
提供的源码资源涵盖了安卓应用、小程序、Python应用和Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值