其他分享
首页 > 其他分享> > 2020-12-07

2020-12-07

作者:互联网

C# QQ通讯录

要求

实现增删改查功能

制作代码展示

启动方法

        public void start()
        {
            Console.WriteLine("程序启动了,这是start方法");


            //判断是否登录成功
            if (login())
            {
                Console.WriteLine("登录成功!");
                Menu();//显示菜单
            }
            else
            {
                Console.WriteLine("账号或密码错误");
            }



            Console.ReadKey();
        }

登录功能方法

 public bool login()
        {
            bool reaf=false;
            Console.WriteLine("登陆功能,被调用的login方法");
            // 接收键盘输入,用户名,密码
            Console.WriteLine("请输入用户名:");
            usr_name = Console.ReadLine();
            Console.WriteLine("请输入密码:");
            string pwd = Console.ReadLine();

            // 去数据表中查询(需要用户表,用户名列,密码列)
            string sql = "select COUNT(*) from QQUser where QQID=" + usr_name + " and PassWord='" + pwd + "';";
            SqlCommand cmd = new SqlCommand(sql, connection);
            connection.Open();
            string a = cmd.ExecuteScalar().ToString();
            Console.WriteLine(a);
            connection.Close();
            if (a.Equals("1"))
            {
                reaf = true;
            }
            else
            {
                reaf = false;
            }
            // 如果查询成功,代表登陆成功,给一个返回值
            // 如果查询不成功,登陆失败,给一个返回值
            return reaf;
        }

显示菜单方法

 public void Menu()
        {
            Console.WriteLine("1.查询好友");
            Console.WriteLine("2.删除好友");
            Console.WriteLine("3.添加好友");
            Console.WriteLine("4.修改好友");
            Console.WriteLine("q.退出");

            Console.WriteLine("请选择你要操作的选择");
            string a= Console.ReadLine();
            switch (a) { 
                case "1":
                    select_from();//查询方法
                    break;
                case "2":

                    break;
                case "3":

                    break;
                case "4":

                    break;
                default:
                    break;
            }
        }

查询方法

 public void select_from() {
            string sql = "select * from BaseInfo where qqid=" + usr_name + ";";
            string sql2 = "select count(*) from BaseInfo where qqid=" + usr_name + ";";
            connection.Open();
            
            SqlCommand cmd2 = new SqlCommand(sql2, connection);
            int a = Convert.ToInt32(cmd2.ExecuteScalar().ToString());
            if (a > 0)
            {
                SqlCommand cmd = new SqlCommand(sql, connection);
                SqlDataReader sdr = cmd.ExecuteReader();
                while (sdr.Read())
                {
                    Console.WriteLine("QQ号:" + sdr["qqid"]);
                    Console.WriteLine("昵称:" + sdr["nickname"]);
                    Console.WriteLine("年龄:" + sdr["age"]);
                }

            }
            else
            {
                Console.WriteLine("你没有好友");
            }
            
            connection.Close();
        }

标签:12,Console,07,break,connection,2020,WriteLine,SqlCommand,string
来源: https://blog.csdn.net/weixin_51511602/article/details/110790931