using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
namespace ConsoleApplication1
{
class Handle
{ //返回的信息
public const string strCoon = "Data Source=.;Initial Catalog=QQDB;Integrated Security=True";
public bool checkAdminInfo(string userName, string pwd, ref string strMsg)
{ //获取指定的 用户名和密码
SqlConnection coon = new SqlConnection(strCoon); //创建数据库的链接
try
{
string sql = "select count(*) from admin where LoginId='" + userName + "' and LoginPwd='" + pwd + "'";// 创建SQL语句
coon.Open();//打开链接
SqlCommand comm = new SqlCommand(sql, coon); //创建Command语句
int shu = (int)comm.ExecuteScalar();
if (shu != 1)
{
strMsg = "输入无效";
return false;
}
else
{
return true;
}
}
catch (Exception)
{
strMsg = "出现错误!";
return false;
}
finally {
coon.Close();//关闭连接
}
}
public SqlDataReader GetUserList()//取得用户信息列表
{
try
{
SqlConnection conn = new SqlConnection(strCoon);
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(strCoon);
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(strCoon);
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(strCoon);
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(strCoon);
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.ExecuteNonQuery();
return comm.ExecuteScalar();
}
catch (Exception)
{
return -1;
}
}
public SqlDataReader GetUserByID(int UserID) //按用户编号查询用户信息
{
try
{
SqlConnection conn = new SqlConnection(strCoon);
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.[UserId] = " + UserID);
sb.AppendLine(" AND");
sb.AppendLine(" a.[LevelId] = b.[LevelId]");
SqlCommand comm = new SqlCommand(sb.ToString(), conn);
return comm.ExecuteReader();
}
catch(Exception)
{
return null;
}
}
public int DeleteUserInfo(int strUserId) // 删除用户
{
try
{
SqlConnection conn = new SqlConnection(strCoon);
conn.Open();
StringBuilder sb = new StringBuilder();
sb.AppendLine(" DELETE FROM");
sb.AppendLine(" [UserInfo]");
sb.AppendLine("WHERE");
sb.AppendLine(" [UserId]=" + strUserId);
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;
using System.Data.SqlClient;
namespace ConsoleApplication1
{
class UserManager
{
public Handle _a = new Handle();//创建Handle实例
const String ERRMSG = "数据操作失败!";
const String EXCEPT = "出现异常。请与系统管理员联系!";
public void Login() //登录 执行验证管理员登录并处理结果信息 非空验证
{
int count = 0;
do
{
string strUserName ="";//初始化登录名
string strPwd = "";//初始化密码
Console.WriteLine("请输入用户名:");
strUserName = Console.ReadLine();
Console.WriteLine("请输入密码:");
strPwd = Console.ReadLine()
;
if (strUserName.Equals(string.Empty) || strPwd.Equals(string.Empty))
{
Console.WriteLine("输入有误,重新输入!");
continue;
}
else
{
string strMsg = string.Empty;//返回信息
bool bRet = _a.checkAdminInfo(strUserName, strPwd, ref strMsg);
if (bRet)
{
Console.WriteLine("登录成功!");
ShowMenu();
break;
}
else
{
Console.WriteLine("登录失败:" + strMsg);
continue;//重新输入用户名和密码
}
}
} while (count < 3);
if (count == 3){
Console.WriteLine("三次登录失败,退出系统!");
}
}
private void ShowUserInfo()
{
SqlDataReader reader = _a.GetUserList();
if (reader == null)
{
Console.WriteLine(EXCEPT);
return;
}
DisplayUserInfo(reader);
}
public 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");
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 = _a.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(_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;
}
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("更新成功!");
}
private void DeleteUserInfo() // 删除指定的用户记录
{
try
{
//接收要删除的用户编号
Console.WriteLine("请输入删除的用户编号:");
string strUserId = Console.ReadLine();
int iUserId = Convert.ToInt32(strUserId);
//按用户编号查询要删除的用户记录
Console.WriteLine("将要删除的用户信息是:");
SqlDataReader reader = _a.GetUserByID(iUserId);
if (reader == null)
{
Console.WriteLine(EXCEPT);
return;
}
//确认是否要删除用户记录
DisplayUserInfo(reader);
Console.WriteLine("要删除该用户记录吗?(Y/N)");
if (Console.ReadLine().Trim().ToUpper() != "Y")
{
Console.WriteLine("退出删除操作!");
return;
}
//执行删除操作
int iRet = _a.DeleteUserInfo(iUserId);
if (iRet == -1)
{
Console.WriteLine(ERRMSG);
}
else
{
Console.WriteLine("删除成功!");
}
}
catch (Exception ex)
{
Console.WriteLine("删除失败:" + ex.Message);
}
}
private bool IsExit() // 是否退出
{
Console.WriteLine("是否退出?(Y/N)");
string strRet = Console.ReadLine();
strRet = strRet.Trim().ToUpper();
if (strRet.Equals("Y"))
{
return true;
}
else
{
return false;
}
}
public void ShowMenu()//主菜单
{
string choice = "";
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("=======================================");
choice = Console.ReadLine();
switch (choice)
{
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);
}
}
}
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 manger = new UserManager();
manger.Login();
}
}
}
QQ用户信息管理系统 大结局
最新推荐文章于 2021-05-18 22:16:37 发布