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.

183 lines
6.2 KiB

  1. /* Describtion : Class for Data Analyze
  2. * Company : Wuxi Xinje
  3. * Author : Somuns
  4. * DateTime : 2021/1/18
  5. */
  6. using MySql.Data.MySqlClient;
  7. using System.Data;
  8. using System.Windows.Forms;
  9. namespace StuMgmLib.MyNameSpace
  10. {
  11. /// <summary>
  12. /// 数据操作
  13. /// </summary>
  14. public class DataAnalyze
  15. {
  16. private enum verifyCode : short
  17. {
  18. notFound = -1,
  19. error = -2,
  20. admin = 1,
  21. teacher = 2,
  22. student = 3,
  23. }
  24. private const string conStr = "data source=localhost; initial catalog=xinje; user id=root; pwd=980505;charset = utf8";
  25. /* Recv: ___________________________________________________________________
  26. * | Account | Password | (SqlOperate) |
  27. * |___short_____string______string[]________________________________________|
  28. * Analyze:
  29. * Account Permission (SqlOperate)
  30. *
  31. * Send: ____________________________________________________________________
  32. * | Permission | DataSet |
  33. * |___short________DS___________________________________________________|
  34. *
  35. */
  36. /// <summary>
  37. /// 解析ClientSend
  38. /// </summary>
  39. public static Info.ServerSend ClientSendAnalyze(Info.ClientSend cs)
  40. {
  41. Info.ServerSend ss = new Info.ServerSend();
  42. ss.Permission = loginVerify(cs.Account, cs.Password); // 验证身份
  43. if (ss.Permission < 0) // 小于0,则权限有误
  44. {
  45. ss.Ds = null;
  46. return ss;
  47. }
  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.SqlSucceed = false;
  66. if (cs.SqlStr != null) // sql语句为空,则表示仅登录验证;若不为空,则取数据库操作返回值,并返回SS;
  67. {
  68. ss.SqlSucceed = mySqlModify(tbName, cs.SqlStr);
  69. return ss;
  70. }
  71. ss.Ds = getDataSet(tbName, stuFlag, cs.Account);
  72. return ss;
  73. }
  74. /// <summary>
  75. /// 登录验证,若失败,则返回错误码;若身份验证成功,则返回用户权限;
  76. /// </summary>
  77. private static short loginVerify(short account, string psw)
  78. {
  79. short notFound = -1;
  80. short error = -2;
  81. string qStu = "select * from user where account = " + account + " and password = '" + psw + "'";
  82. MySqlConnection con = new MySqlConnection(conStr);
  83. try
  84. {
  85. con.Open();
  86. MySqlCommand mCmd = new MySqlCommand(qStu, con);
  87. MySqlDataReader mReader = mCmd.ExecuteReader();
  88. if (mReader.HasRows)
  89. {
  90. mReader.Read();
  91. return mReader.GetInt16("permission");
  92. }
  93. else
  94. return notFound;
  95. }
  96. catch (MySqlException)
  97. {
  98. return error;
  99. }
  100. finally
  101. {
  102. con.Close();
  103. }
  104. }
  105. /// <summary>
  106. /// 改
  107. /// </summary>
  108. private static bool mySqlModify(string[] tbName, string[] sqlStr) // Need to change ......
  109. {
  110. MySqlConnection con = new MySqlConnection(conStr);
  111. try
  112. {
  113. con.Open();
  114. int len = sqlStr.Length;
  115. for (int index = 0; index < len; index++)
  116. {
  117. MySqlCommand mCmd = new MySqlCommand(sqlStr[index], con); // 优化:所操作数据表是否匹配权限
  118. mCmd.ExecuteNonQuery();
  119. }
  120. return true;
  121. }
  122. catch
  123. {
  124. return false;
  125. }
  126. finally
  127. {
  128. con.Close();
  129. }
  130. }
  131. /// <summary>
  132. /// 查 将各表填入dataset
  133. /// </summary>
  134. private static DataSet getDataSet(string[] tbName, bool stuFlag, int account)
  135. {
  136. string str = "select * from ";
  137. MySqlConnection con = new MySqlConnection(conStr);
  138. try
  139. {
  140. con.Open();
  141. DataSet ds = new DataSet();
  142. for (int index = 0; index < tbName.Length; index++)
  143. {
  144. string newStr = str + " " + tbName[index];
  145. if ((stuFlag == true) && (tbName[index] == "user_info"))
  146. {
  147. newStr += " where job_id = " + account.ToString(); // 学员权限时,返回该学员数据项
  148. }
  149. MySqlCommand mCmd = new MySqlCommand(newStr, con);
  150. MySqlDataReader mReader = mCmd.ExecuteReader();
  151. DataTable dt = new DataTable();
  152. dt.Load(mReader);
  153. dt.TableName = tbName[index];
  154. ds.Tables.Add(dt);
  155. }
  156. return ds;
  157. }
  158. catch (MySqlException mySqlEx)
  159. {
  160. MessageBox.Show(mySqlEx.Message);
  161. return null;
  162. }
  163. finally
  164. {
  165. con.Close();
  166. }
  167. }
  168. }
  169. }