欢迎来到
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;
}
}