c#小系统开发之登录 注册

欢迎来到 unity脚本学习社区

今天来回顾一下控制台交互的登录与注册

一、注册(数据库的插入操作)

         public int  Registered (string name,string password,int age,string sex)
        {
            SqlConnection con = new SqlConnection("server=.;database=school;Trusted_Connection=SSPI");
            con.Open();
            SqlCommand com = new SqlCommand("insert into student (name,password,age,sex)values('"+name +"','"+password                                               +"',"+age+",'"+sex+"')", con);
            int i = com.ExecuteNonQuery();
            con.Close();
            return i;
        }

二、登录(数据库查询操作)

        public Student Login(string name,string password) {
            Student s=new Student ();
            SqlConnection con = new SqlConnection("server=.;database=school;Trusted_Connection=SSPI");
            con.Open();
            SqlCommand com = new SqlCommand("select name,password from student where name='" + name + "'and password='" + password + "'",             con);
            SqlDataReader re = com.ExecuteReader();
            while (re.Read())
                {
                Student s1=new Student ();
                s1.Name = (string)re.GetValue(0);
                s1.password = (string)re.GetValue(1);
                s = s1;
               }
                 return s;
              }
        }

三、控制台界面(这里拿一个学生登录 注册来举例)

         public void Student()
        {
            Console.WriteLine("*******************************************");
            Console.WriteLine("*******************************************");
            Console.WriteLine("************    输入1 请登录    ***********");
            Console.WriteLine("************    输入2 请注册    ***********");
            Console.WriteLine("*******************************************");
            Console.WriteLine("*******************************************");
            string a = Console.ReadLine();
            switch (a)
            {
                case "1":
                     Console.WriteLine("输入学生姓名");
                    string name1 = Console.ReadLine();
                    Console.WriteLine("输入学生密码");
                    string password1 = Console.ReadLine();
                    s = new Student().Login(name1,password1 );
                    if (s.Name != null)
                    {
                        Console.WriteLine("登录成功");
                        StudentCourse();

                    }
                    else {
                        Console.WriteLine("登录失败 请重新登录");
                    }
                    break;
                case "2":
                    Console.WriteLine("输入学生姓名");
                    string name = Console.ReadLine();
                    Console.WriteLine("输入学生密码");
                    string password = Console.ReadLine();
                    Console.WriteLine("输入学生年龄");
                    int age = int.Parse (Console.ReadLine());
                    Console.WriteLine("输入学生性别");
                    string sex = Console.ReadLine();
                    int i = new Student().Insert(name,password ,age,sex);
                    if (i == 1)
                    {
                        Console.WriteLine("注册成功");
                        Student();
                    }
                    else
                    {
                        Console.WriteLine("注册失败 请重新注册");
                        Student();
                    }
                    break;
                default:
                    Console.WriteLine("请重新输入");
                    break;
                }
           }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值