using MySql.Data.MySqlClient; using System.Data; using System.Windows.Forms; namespace StuMgmLib.MyNameSpace { /// /// 数据操作 /// public class DataAnalyze { private enum verifyCode : short { error = -1, notFound = -2, admin = 1, teacher = 2, student = 3, } private const string conStr = "data source=localhost; initial catalog=xinje; user id=root; pwd=980505;charset = utf8"; /* Recv: ___________________________________________________________________ * | Account | Password | (SqlOperate) | * |___short_____string______string[]________________________________________| * Analyze: * Account Permission (SqlOperate) * * Send: ____________________________________________________________________ * | Permission | DataSet | * |___short________DS___________________________________________________| * */ /// /// 解析ClientSend /// public static Info.ServerSend ClientSendAnalyze(Info.ClientSend cs) { Info.ServerSend ss = new Info.ServerSend(); ss.permission = loginVerify(cs.account, cs.password); // 验证身份 if (ss.permission < 0) // 小于0,则权限有误 { ss.ds = null; return ss; } string[] tbName; bool stuFlag = false; switch (ss.permission) { case (short)verifyCode.admin: tbName = new string[] { "user_info", "course_info", "user" }; break; case (short)verifyCode.teacher: tbName = new string[] { "user_info", "course_info" }; break; case (short)verifyCode.student: tbName = new string[] { "user_info", "course_info" }; stuFlag = true; break; default: tbName = null; break; } ss.sqlSucceed = false; if (cs.sqlStr != null) // sql语句为空,则表示仅登录验证;若不为空,则取数据库操作返回值,并返回SS; { ss.sqlSucceed = mySqlModify(tbName, cs.sqlStr); return ss; } ss.ds = getDataSet(tbName, stuFlag, cs.account); return ss; } /// /// 登录验证,若失败,则返回错误码;若身份验证成功,则返回用户权限; /// private static short loginVerify(short account, string psw) { short notFound = -1; short error = -2; string qStu = "select * from user where account = " + account + " and password = '" + psw + "'"; MySqlConnection con = new MySqlConnection(conStr); try { con.Open(); MySqlCommand mCmd = new MySqlCommand(qStu, con); MySqlDataReader mReader = mCmd.ExecuteReader(); if (mReader.HasRows) { mReader.Read(); return mReader.GetInt16("permission"); } else return notFound; } catch (MySqlException) { return error; } finally { con.Close(); } } /// /// 改 /// private static bool mySqlModify(string[] tbName, string[] sqlStr) // Need to change ...... { MySqlConnection con = new MySqlConnection(conStr); try { con.Open(); int len = sqlStr.Length; for (int index = 0; index < len; index++) { MySqlCommand mCmd = new MySqlCommand(sqlStr[index], con); // 优化:所操作数据表是否匹配权限 mCmd.ExecuteNonQuery(); } return true; } catch { return false; } finally { con.Close(); } } /// /// 查 将各表填入dataset /// private static DataSet getDataSet(string[] tbName, bool stuFlag, int account) { string str = "select * from "; MySqlConnection con = new MySqlConnection(conStr); try { con.Open(); DataSet ds = new DataSet(); for (int index = 0; index < tbName.Length; index++) { string newStr = str + " " + tbName[index]; if ((stuFlag == true) && (tbName[index] == "user_info")) { newStr += "where job_id = " + account.ToString(); } MySqlCommand mCmd = new MySqlCommand(newStr, con); MySqlDataReader mReader = mCmd.ExecuteReader(); DataTable dt = new DataTable(); dt.Load(mReader); dt.TableName = tbName[index]; ds.Tables.Add(dt); } return ds; } catch (MySqlException mySqlEx) { MessageBox.Show(mySqlEx.Message); return null; } finally { con.Close(); } } } }