using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
namespace QQ
{
class Program
{
static void Main(string[] args)//调用方法
{
denglu();
caidan();
Console.ReadLine();
}
public static void denglu()//进行用户登录的方法
{
for (int i = 0; i < 3; i++)//进行判断,用for循环可以使用户重复错误输入次数为3
{
Console.WriteLine("请输入用户名");//用户进行输入登录验证
string num1 = Console.ReadLine();
Console.WriteLine("请输入密码");
int num2 = Convert.ToInt32(Console.ReadLine());
//如果登录密码正确,登录成功
string sql = "Data source =.; Initial catalog=QQDB; uid=" + num1 + ";pwd=" + num2 + "";//进行打开sql的操作,下面用循环可以让用户进行三次输入登录验证
SqlConnection con = new SqlConnection(sql);
try
{
con.Open();
Console.WriteLine("登陆成功");
break;
}
catch (Exception)
{
Console.WriteLine("登陆失败:输入无效");
for (int j = 3; j < 0; j--)
{
Console.WriteLine("连续三次登陆无效退出系统");
}
con.Close();
}
}
}
public static void caidan()//登录菜单页面
{
Console.WriteLine("======欢迎登陆qq用户信息管理系统=======");
Console.WriteLine("======请选择菜单项=======");
Console.WriteLine("1显示菜单清单");
Console.WriteLine("2:更新在线天数");
Console.WriteLine("3:添加用户新纪录");
Console.WriteLine("4:更新用户等级");
Console.WriteLine("5:删除用户信息");
Console.WriteLine("6:退出");
Console.WriteLine("7:更新之加强版");
Console.WriteLine("=================");
int xuanze = Convert.ToInt32(Console.ReadLine());
switch (xuanze)//用户进行选择
{
case 1:
QQdb();
break;
case 2:
QQdb2();
break;
case 3:
QQdb3();
break;
case 4:
QQdb001();
break;
case 5:
QQdb002();
break;
case 6:
QQdb6();
break;
}
}
private static void QQdb002()//查询Id信息,
{//先对用户登录成功后输入删除的用户的ID进行搜索,查出要删除的用户的信息,调用删除方法进行删除
Console.WriteLine("请输入要删除的用户的ID");
int shanchu = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("您要删除的用户的信息为");
Console.WriteLine("编号\t昵称\t等级\t邮箱\t在线天数");
Console.WriteLine("===========================");
string str = "Data source=.;initial Catalog=QQDB;user id=sa;pwd=111";
SqlConnection con = new SqlConnection(str);
string sql = "select UserId,UserName,LevelId,Email,OnLineDay from UserInfo where UserId=‘" + shanchu + "‘";
SqlCommand cmd = new SqlCommand(sql, con);
try
{
con.Open();
SqlDataReader dr = cmd.ExecuteReader();
if (dr != null)
{
while (dr.Read())
{
int id = Convert.ToInt32(dr["UserId"]);
string Sname = dr["UserName"].ToString();
string levelId = dr["LevelId"].ToString();
float OnLineDay = Convert.ToInt32(dr["OnLineDay"]);
string Email = dr["Email"].ToString();
Console.WriteLine("{0}\t\t{1}\t\t{2}\t\t{3}\t{4}", id, Sname, levelId, Email, OnLineDay);
}
}
Console.WriteLine("确定要删除吗Y/N");//对管理员输入信息在进行一道确定
char shan = Convert.ToChar(Console.ReadLine());
if (shan.Equals(‘y‘))
{
QQdb5(shanchu);//调用下面删除方法来删除用户信息
}
else
{
caidan();
}
}
catch (Exception)
{
Console.WriteLine("登陆失败:输入无效");
for (int j = 3; j < 0; j--)
{
Console.WriteLine("连续三次登陆无效退出系统");
}
}
finally {
con.Close();
}
}
private static void QQdb6()//退出方法
{
Console.WriteLine("是否退出(y/n)");
char tuichu = Convert.ToChar(Console.ReadLine());
if (tuichu.Equals(‘y‘))
{
Console.WriteLine("╮(╯▽╰)╭按任意键继续");
}
else
{
caidan();
}
}
private static void QQdb5(int ID)//删除用户方法
{
string str = "Data source=.;initial Catalog=QQDB;user ID=sa;pwd=111";
SqlConnection con = new SqlConnection(str);
string sql = "Delete from UserInfo where Userid=‘" + ID + "‘";
SqlCommand cmd = new SqlCommand(sql, con);
con.Open();
int count = cmd.ExecuteNonQuery();
if (count > 0)
{
Console.WriteLine("删除用户记录成功");
}
else
{
Console.WriteLine("删除失败");
}
caidan();
con.Close();
}
private static void QQdb001()//更新用户等级
{//更新用户等级要先进行等级的判定,例如在线时间小于五天的便是一级,大于五天小于三十二天的便是2级,写出sql语句,然后调用增删改的方法,对所有的用户进行更新
String str = "Data source=.;initial Catalog=QQDB;user id=sa;pwd=111";
SqlConnection con = new SqlConnection(str);
//下面语句对用户的等级进行判断并且进行更新
string sql = "update UserInfo set LevelId=4 where OnlineDay>=320 update UserInfo set LevelId=3 where OnlineDay>=32 update UserInfo set LevelId=2 where OnlineDay>=5 update UserInfo set LevelId=1 where OnlineDay<5 ";
SqlCommand cmd = new SqlCommand(sql, con);
con.Open();
int count = cmd.ExecuteNonQuery();
if (count > 0)
{
Console.WriteLine("更新次数为:{0}", Convert.ToInt32(cmd.ExecuteNonQuery())) ;
}
else
{
Console.WriteLine("修改失败");
}
caidan();
con.Close();
}
private static void QQdb3()//添加用户新纪录
{//增加用户信首先要输入用户的信息,然后调用.ExecuteNonQuery()进行对用户的增加操作
Console.WriteLine("请输入用户昵称");//管理员添加信息
string bianhao = Console.ReadLine();
Console.WriteLine("请输入用户密码");
string pwd = Console.ReadLine();
Console.WriteLine("请输入用户邮箱");
string yx = Console.ReadLine();
string str = "Data source=.;initial Catalog=QQDB;user id=sa;pwd=111";
SqlConnection con = new SqlConnection(str);
string sql = "insert into UserInfo(UserName,UserPwd,LevelId,Email,OnLineDay) values(‘" + bianhao + "‘,‘" + pwd + "‘,1,‘" + yx + "‘,1 )";
SqlCommand cmd = new SqlCommand(sql, con);
con.Open();
int count = cmd.ExecuteNonQuery();
if (count > 0)
{
Console.WriteLine("添加成功,");
}
else
{
Console.WriteLine("添加失败");
}
caidan();
con.Close();
}
private static void QQdb2()//更新在线信息
{//更新天数便要输入要更新天数的用户的Id,然后手动输入要更新的天数
Console.WriteLine("请输入用户编号");
int bianhao = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("请输入新的在线天数");
int OnLineDay = Convert.ToInt32(Console.ReadLine());
string str = "Data source=.;initial Catalog=QQDB;user id=sa;pwd=111";
SqlConnection con = new SqlConnection(str);
string sql = "update UserInfo Set OnLineDay=" + OnLineDay + " where UserId=" + bianhao + "";
SqlCommand cmd = new SqlCommand(sql, con);
con.Open();
int count = cmd.ExecuteNonQuery();
if (count > 0)
{
Console.WriteLine("修改成功");
}
else
{
Console.WriteLine("修改失败");
}
caidan();
con.Close();
}
private static void QQdb()//显示菜单
{//显示菜单在数据库里用户等级是用1,2,3,4表示的在这里要进行修改,将等级修改成符号的模式进行输出
String str = "Data source=.;initial Catalog=QQDB;user id=sa;pwd=111";
SqlConnection con = new SqlConnection(str);
string sql = "select UserId,UserName,UserPwd,LevelId,Email,OnLineDay from UserInfo";
SqlCommand cmd = new SqlCommand(sql, con);
con.Open();
SqlDataReader dr = cmd.ExecuteReader();
Console.WriteLine("编号\t\t昵称\t\t\t密码\t\t等级\t邮箱\t\t在线天数");
if (dr != null)
{
while (dr.Read())//循环用户信息记录,并且输出
{
int id = Convert.ToInt32(dr["UserId"]);
string Sname = dr["UserName"].ToString();
int sid = Convert.ToInt32(dr["UserPwd"]);
string levelId = dr["LevelId"].ToString();
switch (levelId)
{
case "1":
levelId = "--";
break;
case "2":
levelId = "☆";
break;
case "3":
levelId="?";
break;
case "4":
levelId="?";
break;
}
float OnLineDay = Convert.ToInt32(dr["OnLineDay"]);
string Email = dr["Email"].ToString();
Console.WriteLine("{0}\t\t{1}\t\t{2}\t\t{3}\t{4}\t{5}", id, Sname, sid, levelId, Email, OnLineDay);
}
caidan();
dr.Close();
con.Close();
}
}
}
}