17章

来源:互联网 发布:淘宝提交大学生认证 编辑:程序博客网 时间:2024/05/22 14:15
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Data.SqlClient;namespace ConsoleApplication1{      class Class1    {                 //用户登录和调用        public void yonghu() {           string strsql = "Data Source=.;Initial Catalog=QQDB;Integrated Security=True";           SqlConnection conn=new SqlConnection(strsql);           for (int i = 0; i < 3; i++)           {               conn.Open();               Console.WriteLine("请输入用户名");               string z = Console.ReadLine();               Console.WriteLine("请输入密码");               string pwd = Console.ReadLine();               try               {                   string stri = "select count(*) from admin where LoginId='" + z + "'and LoginPwd='" + pwd + "'";                   SqlCommand comm = new SqlCommand(stri, conn);                   // Console.WriteLine(sb.ToString());                   int j = (int)comm.ExecuteScalar();                   if (j != 0)                   {                       Console.WriteLine("成功");                       biao();                       break;                   }                   else                   {                       Console.WriteLine("失败");                   }               }               catch (Exception e)               {                   Console.WriteLine(e.Message);               }               finally               {                   conn.Close();               }           }                }        //显示和控制系统菜单        public void biao() {            bool b=true;            do            {                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("======================================================");                Console.WriteLine("请输入:");                int a = int.Parse(Console.ReadLine());                switch (a)                {                    case 1:                        xs();                        break;                    case 2:                        ts();                        break;                    case 3:                        zt();                        break;                    case 4:                        dj();                        break;                    case 5:                        sc();                        break;                    case 0:                        Console.WriteLine("是否退出?(1/2)");                        int yi = int.Parse(Console.ReadLine());                        if (yi == 1) {                            b = false;                            Console.WriteLine("输入任意键继续");                            Console.ReadLine();                            b=true;                        }                        else { b = true; }                        break;                    default:                        break;                }            } while (b);        }        //显示用户清单的运行结果            public void xs(){                string stri = "Data Source=.;Initial Catalog=QQDB;Integrated Security=True";                SqlConnection conn = new SqlConnection(stri);                conn.Open();                string s = "select * from UserInfo u inner join  Level l on l.LevelId=u.levelid";                SqlCommand comm = new SqlCommand(s,conn);                SqlDataReader d = comm.ExecuteReader();                Console.WriteLine("ID       姓名         密码             等级            邮箱           在线天数 ");                   while(d.Read()){                        Console.WriteLine(d["UserId"] + "        " + d["UserName"] + "        " + d["UserPwd"] + "       " + d["LevelName"] + "       "  + d["Email"] + "          " + d["OnLineDay"] + " ");                   }                    d.Close();                   conn.Close();                                     Console.ReadLine();            }        //更新用户的在线天数            public void ts() {                string stri = "Data Source=.;Initial Catalog=QQDB;Integrated Security=True";                SqlConnection conn = new SqlConnection(stri);                conn.Open();                Console.WriteLine("请输入用户编号:");                string shu= Console.ReadLine();                Console.WriteLine("请输入新的在线天数");                string shu1=Console.ReadLine();                string s = "update UserInfo set onLineDay ='"+shu1+"' where UserId='" + shu + "'";                SqlCommand comm = new SqlCommand(s, conn);                int i = comm.ExecuteNonQuery();                if (i == 1) {                    Console.WriteLine("成功");                } else {                    Console.WriteLine("失败");                }                conn.Close();                        }        //添加用户新纪录            public void zt() {                string stri = "Data Source=.;Initial Catalog=QQDB;Integrated Security=True";                SqlConnection conn = new SqlConnection(stri);                conn.Open();                Console.WriteLine("请输入用户昵称:");                string nc = Console.ReadLine();                Console.WriteLine("请输入密码:");                string mm = Console.ReadLine();                Console.WriteLine("请输入邮箱地址:");                string yx = Console.ReadLine();                             string s = "insert into UserInfo (UserName,UserPwd,Email) values('"+nc+"', '"+mm+"', '"+yx+"' )   "  ;                SqlCommand comm = new SqlCommand(s, conn);                int i = comm.ExecuteNonQuery();                if (i == 1){                    string a = "select userid from userinfo where userName='"+nc+"'";                    SqlCommand com = new SqlCommand(a, conn);                    int j = (int)com.ExecuteScalar();                 Console.WriteLine("成功");                Console.WriteLine("更新编号是"+j);                                }else{                    Console.WriteLine("失败");                     }                                    }        //更新用户等级            public void dj() {                string stri = "Data Source=.;Initial Catalog=QQDB;Integrated Security=True";                SqlConnection conn = new SqlConnection(stri);                conn.Open();                StringBuilder sb = new StringBuilder();                sb.AppendLine("update userinfo set levelid=1 where OnLineDay<5 and OnLineDay>0");                sb.AppendLine("update userinfo set levelid=2 where OnLineDay<32 and OnLineDay>5");                sb.AppendLine("update userinfo set levelid=3 where OnLineDay<320 and OnLineDay>32");                sb.AppendLine("update userinfo set levelid=4 where  OnLineDay>320");                SqlCommand comm = new SqlCommand(sb.ToString(), conn);                int j = comm.ExecuteNonQuery();                if(0<=j){                    Console.WriteLine("本次更新用户记录"+j);                    Console.WriteLine("输入任意键返回上一页");                    Console.ReadLine();                }else{                    Console.WriteLine("失败");                }                }        //删除用户信息                public void  sc( ) {                string stri = "Data Source=.;Initial Catalog=QQDB;Integrated Security=True";                SqlConnection conn = new SqlConnection(stri);                conn.Open();                Console.WriteLine("请输入删除用户编号:");                int shu = int.Parse(Console.ReadLine());                StringBuilder sb = new StringBuilder();                sb.AppendLine("select * from UserInfo u inner join  Level l on l.LevelId=u.levelid  where UserId='"+shu+"'");                SqlCommand comm = new SqlCommand(sb.ToString(), conn);                SqlDataReader d = comm.ExecuteReader();                Console.WriteLine("-----------------------------------------------------------------------------");                Console.WriteLine("ID       姓名         密码             等级            邮箱           在线天数 ");                Console.WriteLine("-----------------------------------------------------------------------------");                d.Read();                    Console.WriteLine(d["UserId"] + "        " + d["UserName"] + "        " + d["UserPwd"] + "       " + d["LevelName"] + "       " + d["Email"] + "          " + d["OnLineDay"] + " ");                            Console.WriteLine("-----------------------------------------------------------------------------");                san(shu );                d.Close();                conn.Close();                Console.ReadLine();            }            public int san(int a) {                string stri = "Data Source=.;Initial Catalog=QQDB;Integrated Security=True";                SqlConnection conn = new SqlConnection(stri);                StringBuilder sb = new StringBuilder();                conn.Open();                Console.WriteLine("要删除该用户吗(1/2)");                int i = int.Parse(Console.ReadLine());                if (i == 1)                {                    string s = "delete  from UserInfo  where UserId='" + a + "'";                    SqlCommand comm1 = new SqlCommand(s, conn);                    int j = comm1.ExecuteNonQuery();                    Console.WriteLine(j);                    Console.WriteLine("成功");                }                else                {                    Console.WriteLine("失败");                }                        conn.Close();                Console.ReadLine();                return a;            }                        }}

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Data.SqlClient;namespace ConsoleApplication1{    class Program    {        static void Main(string[] args)        {            Class1 c = new Class1();            c.yonghu();            Console.ReadLine();        }    }}

0 0
原创粉丝点击