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();
}
}
}
转载请注明原文地址: https://ju.6miu.com/read-964404.html