You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

157 line
5.3 KiB

  1. using MySql.Data.MySqlClient;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Data;
  5. using System.Text;
  6. using System.Windows.Forms;
  7. namespace StuMgmLib.MyNameSpace
  8. {
  9. /// <summary>
  10. /// 数据操作
  11. /// </summary>
  12. public class DataAnalyze
  13. {
  14. private enum verifyCode : short
  15. {
  16. error = -1,
  17. notFound = -2,
  18. admin = 1,
  19. teacher = 2,
  20. student = 3,
  21. }
  22. private const string conStr = "data source=localhost; initial catalog=xinje; user id=root; pwd=980505;charset = utf8";
  23. /* Recv: ___________________________________________________________________
  24. * | Account | Password | (SqlOperate) |
  25. * |___short_____string______string________________________________________|
  26. * Analyze:
  27. * Account Permission (SqlOperate)
  28. *
  29. * Send: ____________________________________________________________________
  30. * | Permission | DataSet |
  31. * |___short________DS_______________________________________________________________|
  32. */
  33. /// <summary>
  34. /// 解析ClientSend
  35. /// </summary>
  36. public static Info.ServerSend ClientSendAnalyze(Info.ClientSend cs)
  37. {
  38. Info.ServerSend ss = new Info.ServerSend();
  39. ss.permission = LoginVerify(cs.account, cs.password); // 验证身份
  40. if (ss.permission < 0) // 小于0,则权限有误
  41. {
  42. ss.ds = null;
  43. return ss;
  44. }
  45. // if(operationCode != 0)
  46. // 写数据表操作
  47. // To do sth here ........
  48. string[] tbName;
  49. bool stuFlag = false;
  50. switch (ss.permission)
  51. {
  52. case (short)verifyCode.admin:
  53. tbName = new string[] { "user_info", "course_info", "user" };
  54. break;
  55. case (short)verifyCode.teacher:
  56. tbName = new string[] { "user_info", "course_info" };
  57. break;
  58. case (short)verifyCode.student:
  59. tbName = new string[] { "user_info", "course_info" };
  60. stuFlag = true; break;
  61. default:
  62. tbName = null;
  63. break;
  64. }
  65. ss.ds = getDataSet(tbName, stuFlag, cs.account);
  66. return ss;
  67. }
  68. /// <summary>
  69. /// 登录验证,若失败,则返回错误码;若身份验证成功,则返回用户权限;
  70. /// </summary>
  71. private static short LoginVerify(short account, string psw)
  72. {
  73. short notFound = -1;
  74. short error = -2;
  75. string qStu = "select * from user where account = " + account + " and password = '" + psw + "'";
  76. MySqlConnection con = new MySqlConnection(conStr);
  77. try
  78. {
  79. con.Open();
  80. MySqlCommand mCmd = new MySqlCommand(qStu, con);
  81. MySqlDataReader mReader = mCmd.ExecuteReader();
  82. if (mReader.HasRows)
  83. {
  84. mReader.Read();
  85. return mReader.GetInt16("permission");
  86. }
  87. else
  88. return notFound;
  89. }
  90. catch (MySqlException mySqlEx)
  91. {
  92. MessageBox.Show(mySqlEx.Message);
  93. return error;
  94. }
  95. finally
  96. {
  97. con.Close();
  98. }
  99. }
  100. /// <summary>
  101. /// 改
  102. /// </summary>
  103. private static void mySqlModify()
  104. {
  105. }
  106. /// <summary>
  107. /// 查 将各表填入dataset
  108. /// </summary>
  109. private static DataSet getDataSet(string[] tbName, bool stuFlag, int account)
  110. {
  111. string str = "select * from ";
  112. MySqlConnection con = new MySqlConnection(conStr);
  113. try
  114. {
  115. con.Open();
  116. DataSet ds = new DataSet();
  117. for (int index = 0; index < tbName.Length; index++)
  118. {
  119. string newStr = str + tbName[index];
  120. if ((stuFlag == true) && (tbName[index] == "user_info"))
  121. {
  122. newStr += "where job_id = " + account.ToString();
  123. }
  124. MySqlCommand mCmd = new MySqlCommand(newStr, con);
  125. MySqlDataReader mReader = mCmd.ExecuteReader();
  126. DataTable dt = new DataTable();
  127. dt.Load(mReader);
  128. dt.TableName = tbName[index];
  129. ds.Tables.Add(dt);
  130. }
  131. return ds;
  132. }
  133. catch (MySqlException mySqlEx)
  134. {
  135. MessageBox.Show(mySqlEx.Message);
  136. return null;
  137. }
  138. finally
  139. {
  140. con.Close();
  141. }
  142. }
  143. }
  144. }