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();}}
}