17章 QQ用户信息管理系统 1-2
2016-12-02 15:53
387 查看
using System; using System.Collections.Generic; using System.Data.SqlClient; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication1 { class UserManager { private DBHandle _dbHandle = new DBHandle(); const String ERRMSG = "数据操作失败!"; const String EXCEPT = "出现异常。请与系统管理员联系!"; public void Lofin() { 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 = _dbHandle.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 void ShowMenu() { 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("======================================="); int option =int.Parse(Console.ReadLine()); switch (option) { case 1://显示用户信息 ShowUserInfo(); continue; case 2://更新在线天数 UpdateOnLineDay(); continue; case 3://添加用户 continue; case 4://更新用户等级 continue; case 5://删除用户 continue; case 0: if (option==0) { 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; } /// <summary> /// 更新在线天数 /// </summary> private void UpdateOnLineDay() { try { Console.WriteLine("请输入用户编号:"); int iUserId = Convert.ToInt32(Console.ReadLine()); Console.WriteLine("请输入新的在线天数"); double iNewOnlineDay = Convert.ToDouble(Console.ReadLine()); 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); } } } } using System; using System.Collections.Generic; using System.Data.SqlClient; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication1 { class DBHandle { //连接字符串 private const string strConn = @"Data Source=.;Initial Catalog=QQDB;Integrated Security=True"; /// <summary> /// 登陆 /// </summary> /// <param name="UserName"></param> /// <param name="UserPwd"></param> /// <param name="strMsg"></param> /// <returns></returns> public bool CheckAdminInfo(string UserName,string UserPwd,ref string strMsg) { SqlConnection conn = new SqlConnection(strConn); try { //sql 语句 string str = @"SELECT COUNT(*) FROM [QQDB].[dbo].[admin] where LoginId='"+UserName+"'and LoginPwd='"+UserPwd+"'"; //打开数据库 conn.Open(); //创建command命令 SqlCommand comm = new SqlCommand(str, conn); int i = (int)comm.ExecuteScalar(); if (i != 1) { strMsg = "输入无效!"; return false; } else { return true; } } catch (Exception ex) { Console.WriteLine(ex.Message); strMsg = "发生异常!"; return false; } finally { conn.Close(); } } /// <summary> /// 取得学生列表 /// </summary> /// <returns></returns> public SqlDataReader GetUserList() { try { SqlConnection conn = new SqlConnection(strConn); conn.Open(); string sql = @"select a.UserId, a.UserName, b.LevelName, a.Email, a.OnLineDay from UserInfo a, Level b where a.LevelId=b.LevelId"; SqlCommand comm = new SqlCommand(sql, conn); return comm.ExecuteReader(); } catch (Exception e) { Console.WriteLine(e); return null; } } public int UpdateOnlineDay(int userId, double newOnlineDay) { SqlConnection conn = new SqlConnection(strConn); string sql = "update UserInfo set OnLineDay='" + newOnlineDay + "' where UserId=" + userId + ""; try { conn.Open(); SqlCommand comm = new SqlCommand(sql, conn); return comm.ExecuteNonQuery(); } catch (Exception e) { Console.WriteLine(e); return -1; } } } } using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { UserManager s = new UserManager(); s.Lofin(); Console.ReadLine(); } } }
相关文章推荐
- C# ,十七章 项目案例: QQ用户信息管理系统
- QQ用户信息管理系统 大结局
- SQL第十七章项目案例:QQ用户信息管理系统
- 企业信息管理系统 用户需求报告编写指南
- “MVC+Nhibernate+Jquery-EasyUI” 信息发布系统 第五篇(用户管理之“用户权限分配”)
- 用字典创建一个平台的用户信息(包含用户名和密码)管理系统
- 校友信息管理系统&SNS互动平台之用户需求及概要设计
- 使用PHP制作 简易员工管理系统之五(分页显示用户信息)
- “MVC+Nhibernate+Jquery-EasyUI” 信息发布系统 第四篇(用户管理功能的实现)
- Redis 笔记与总结7 PHP + Redis 信息管理系统(用户信息的增删改查)
- 用户管理系统——分页信息
- 使用PHP制作 简易员工管理系统之七(MVC实现用户信息增、删、改、查)
- 权限管理系统(用户信息管理模块业务组件实现代码,带注解)
- MVC框架——学生信息管理系统(多表,多事务如何处理,一个用户如何共用一个Connection连接)
- 微型用户信息管理系统MUIMS(Micro User Information Management System)V1.0
- Anynote 基于ExtJS多用户个人信息管理系统
- 学生信息管理系统之万能用户'or''='
- “MVC+Nhibernate+Jquery-EasyUI” 信息发布系统 第四篇(用户管理功能的实现)
- ASP.NET学生信息管理系统-权限管理-用户分组
- C#开发微信门户及应用(10)--在管理系统中同步微信用户分组信息