public bool AddUser(User user) { MySqlConnection connection = DBUtility.Connect(); try { MySqlCommand cmd = new MySqlCommand("insert into user set username=@username and password=@password", connection); cmd.Parameters.AddWithValue("username", user.username); cmd.Parameters.AddWithValue("password", user.password); int isSuccess = cmd.ExecuteNonQuery(); if (isSuccess != 0) { return(true); } else { return(false); } } catch (Exception e) { Console.WriteLine(e); throw; } finally { DBUtility.CloseConnection(connection); } }
/// <summary> /// 验证登录 /// </summary> /// <param name="connection"></param> /// <param name="user"></param> /// <returns></returns> public bool VertifyUser(User user) { //连接数据库 MySqlConnection connection = DBUtility.Connect(); MySqlDataReader reader = null; try { MySqlCommand cmd = new MySqlCommand("select * from user where username=@username and password=@username", connection); cmd.Parameters.AddWithValue("username", user.username); cmd.Parameters.AddWithValue("password", user.password); reader = cmd.ExecuteReader(); if (reader.Read()) { return(true); } else { return(false); } } catch (Exception e) { Console.WriteLine("在vertifyUser时出现异常:" + e); throw; } finally { DBUtility.CloseConnection(connection); } }
/// <summary> /// 获取section表中的所有数据 /// </summary> /// <returns></returns> public List <Chapter> SelectAllChapterItem() { MySqlConnection connection = DBUtility.Connect(); MySqlDataReader reader = null; MySqlCommand cmd = null; List <Chapter> list = new List <Chapter>(); try { //Debug.Log("Dao正在查询数据库"); cmd = new MySqlCommand("select * from chapters", connection); reader = cmd.ExecuteReader(); while (reader.Read()) { list.Add(new Chapter(reader.GetInt32("Id"), reader.GetString("ChapterName"), reader.GetInt32("ChapterId"))); //Debug.Log(reader.GetInt32("Id")+","+ reader.GetString("ChapterName")+","+reader.GetInt32("ChapterId")); } return(list); } catch (Exception e) { Debug.Log("在SelectAllSectionItem时出现异常:" + e); } finally { reader.Close(); DBUtility.CloseConnection(connection); } return(null); }
/// <summary> /// </summary> /// <param name="sql"></param> /// <returns>返回值为List<Atom></returns> private List <Atom> Select(string sql) { MySqlConnection connection = DBUtility.Connect(); MySqlDataReader reader = null; MySqlCommand cmd = null; List <Atom> list = new List <Atom>(); bool isSuccess = false; string id = "id"; string atomName = "atomName"; string atomSymbol = "atomSymbol"; string atomEnglishName = "atomEnglishName"; string atomNumber = "atomNumber"; string relativeAtomMass = "relativeAtomMass"; string physicalProperty = "physicalProperty"; string importantKnowledge = "importantKnowledge"; string atomIntroduction = "atomIntroduction"; try { cmd = new MySqlCommand(sql, connection); reader = cmd.ExecuteReader(); while (reader.Read()) { isSuccess = true; id = "id"; atomName = "atomName"; atomSymbol = "atomSymbol"; atomEnglishName = "atomEnglishName"; atomNumber = "atomNumber"; relativeAtomMass = "relativeAtomMass"; physicalProperty = "physicalProperty"; importantKnowledge = "importantKnowledge"; atomIntroduction = "atomIntroduction"; //Debug.Log(reader.GetInt32(id) + "," + reader.GetString(atomName) + "," + reader.GetString(atomSymbol) + "," + reader.GetString( // atomEnglishName) + "," + reader.GetInt32(atomNumber) + "," + reader.GetDouble(relativeAtomMass) + "," + reader.GetString(physicalProperty) + "," + // reader.GetString(importantKnowledge) + "," + reader.GetString(atomIntroduction)); list.Add(new Atom(reader.GetInt32(id), reader.GetString(atomName), reader.GetString(atomSymbol), reader.GetString( atomEnglishName), reader.GetInt32(atomNumber), reader.GetDouble(relativeAtomMass), reader.GetString(physicalProperty), reader.GetString(importantKnowledge), reader.GetString(atomIntroduction))); } if (isSuccess) { return(list); } else { return(null); } } catch (Exception e) { Debug.Log("在Select时出现异常:" + e); } finally { // ReSharper disable once PossibleNullReferenceException reader.Close(); DBUtility.CloseConnection(connection); } return(null); }