//5、将自己的年龄改为18岁 /// <summary> /// 更新学生信息 /// </summary> public void UpdateInfomation() { sqlAce = new SqlAccess(); con = SqlAccess.con; string sql = ("update Student set Sage='18' where Sname='学生A'"); sqlAce.UpdateInfo(sql, con); sqlAce.CloseMySQL(); }
//1、建立一个“学生”表Student,它由学号Sid、姓名Sname、性别Ssex、年龄Sage、 ,类型自己定义 /// <summary> /// 创建学生表 /// </summary> public void CreateStudentTable() { sqlAce = new SqlAccess(); con = SqlAccess.con; string sql = ("create table student(Sid varchar(12),Sname varchar(8),Sage int,Sdept varchar(8)) CHARSET=utf8"); sqlAce.CreateTable(sql, con); sqlAce.CloseMySQL(); }
public void Register() //注册 { username = RegisterName.text; string pw = RegisterPW.text; if (username.Length < 3 || username.Length > 10) { RegisterFailText.text = "用户名应为3-10个英文或数字!"; } else if (pw.Length < 6 || pw.Length > 14) { RegisterFailText.text = "密码应为6-14个字符!"; } else { bool fail = false; RegisterFailText.text = ""; try { sqlAce = new SqlAccess(); con = SqlAccess.con; string sql = ("select name from users "); Dictionary <int, List <string> > dic = sqlAce.QueryInfo(sql, con); //字典在封装类中 for (int i = 0; i < dic.Count; i++) //用户名查重 { if (dic[i][0] == username) { RegisterFailText.text = "用户名已经存在,请更换新的用户名!"; fail = true; break; } } sqlAce.CloseMySQL(); if (!fail) //查重通过后添加用户 { sqlAce = new SqlAccess(); con = SqlAccess.con; sql = string.Format("insert into users(name,password) values('{0}','{1}')", username, SHA1Encrypt(pw)); sqlAce.InsertInfo(sql, con); RegisterFailText.text = "注册成功!请返回登录窗口。"; RegisterName.text = ""; RegisterPW.text = ""; } } catch (Exception e) { RegisterFailText.text = "未知错误,可能是因为未连接到服务器"; Debug.Log(e.ToString()); return; } finally { sqlAce.CloseMySQL(); } } }
//4、查询所有学生的学号与姓名 /// <summary> /// 查询学生信息02 /// </summary> public void QueryInfomation2() { sqlAce = new SqlAccess(); con = SqlAccess.con; string sql = ("select Sid,Sname from Student"); Dictionary <int, List <string> > dic = sqlAce.QueryInfo(sql, con); for (int i = 0; i < dic.Count; i++) { Debug.Log(string.Format("学生学号:{0} 学生姓名:{1}", dic[i][0], dic[i][1])); } sqlAce.CloseMySQL(); }
//3、查询年龄在18至24岁之间的学生的姓名和专业 /// <summary> /// 查询学生信息01 /// </summary> public void QueryInfomation1() { sqlAce = new SqlAccess(); con = SqlAccess.con; string sql = ("select Sname,Sdept from Student where Sage<'24' and Sage>'18' "); Dictionary <int, List <string> > dic = sqlAce.QueryInfo(sql, con); for (int i = 0; i < dic.Count; i++) { Debug.Log(string.Format("学生姓名:{0} 学生专业:{1}", dic[i][0], dic[i][1])); } sqlAce.CloseMySQL(); }
public void SaveGame(string[] stats) //存档 { try { sqlAce = new SqlAccess(); con = SqlAccess.con; string sql = string.Format("update users set STR='{0}',DEX='{1}',CON='{2}',INTE='{3}',WIS='{4}',CHA='{5}' where name='{6}'", stats[0], stats[1], stats[2], stats[3], stats[4], stats[5], username); sqlAce.UpdateInfo(sql, con); } catch (Exception e) { Debug.Log(e.ToString()); return; } finally { sqlAce.CloseMySQL(); } }
public void Login() //登录,测试用时,用户名example4,密码123456 { username = LoginName.text; string pw = LoginPW.text; if (username.Length < 3 || username.Length > 10) { LoginFailText.text = "用户名应为3-10个英文或数字!"; } else if (pw.Length < 6 || pw.Length > 14) { LoginFailText.text = "密码应为6-14个字符!"; } else { bool fail = true; LoginFailText.text = ""; try { sqlAce = new SqlAccess(); con = SqlAccess.con; string sql = ("select name,password from users "); Dictionary <int, List <string> > dic = sqlAce.QueryInfo(sql, con); for (int i = 0; i < dic.Count; i++) { if (dic[i][0] == username && dic[i][1] == SHA1Encrypt(pw)) { fail = false; LoadGame(); //用户名密码验证通过后加载游戏 break; } } if (fail) { LoginFailText.text = "用户名或密码错误!"; } } catch (Exception e) { LoginFailText.text = "未知错误,可能是因为未连接到服务器"; Debug.Log(e.ToString()); return; } finally { sqlAce.CloseMySQL(); } } }
//2、添加自己所在组的所有成员到表中。 /// <summary> /// 学生表插值 /// </summary> public void InsertInfomation() { sqlAce = new SqlAccess(); con = SqlAccess.con; string sql1 = ("insert into Student(Sid,Sname,Sage,Sdept) values('1001','学生A','20','土木工程')"); string sql2 = ("insert into Student(Sid,Sname,Sage,Sdept) values('1002','学生B','15','计算机与科学')"); string sql3 = ("insert into Student(Sid,Sname,Sage,Sdept) values('1003','学生C','16','电子商务')"); string sql4 = ("insert into Student(Sid,Sname,Sage,Sdept) values('1004','学生D','25','电子竞技')"); string sql5 = ("insert into Student(Sid,Sname,Sage,Sdept) values('1005','学生E','23','网络工程')"); string[] str = new string[5] { sql1, sql2, sql3, sql4, sql5 }; for (int i = 0; i < str.Length; i++) { sqlAce.InsertInfo(str[i], con); } sqlAce.CloseMySQL(); }
//6、删除学号为单数的学生记录 /// <summary> /// 删除表信息 /// </summary> public void DeleteInfomation() { sqlAce = new SqlAccess(); con = SqlAccess.con; string sql2 = ("select Sid from Student"); Dictionary <int, List <string> > dic = sqlAce.QueryInfo(sql2, con); sqlAce.CloseMySQL(); sqlAce = new SqlAccess(); con = SqlAccess.con; for (int i = 0; i < dic.Count; i++) { if (Convert.ToInt32(dic[i][0]) % 2 == 1) { string sql1 = string.Format("delete from Student where Sid='{0}'", dic[i][0]); sqlAce.DeleteInfo(sql1, con); } } sqlAce.CloseMySQL(); }
public void LoadGame() { string[] stats = new string[6]; try //从数据库中获取人物六维 { sqlAce = new SqlAccess(); con = SqlAccess.con; string sql = string.Format("select STR,DEX,CON,INTE,WIS,CHA from users where name ='{0}'", username); Dictionary <int, List <string> > dic = sqlAce.QueryInfo(sql, con); //封装类中的字典会自动将得到的数据转化为字符串格式 for (int i = 0; i <= 5; i++) { stats[i] = dic[0][i]; } } catch (Exception e) { Debug.Log(e.ToString()); return; } finally { sqlAce.CloseMySQL(); } LoginWindow.SetActive(false); PlayerWindow.SetActive(true); PlayerWindow.GetComponent <Player>().LoadInClient(username, stats); //切换窗口,调用玩家类方法改变界面中文本 }