public bool SavePlayer(Player player) { string id = player.id; PlayersubCmd playersubCmd = player.subCmd; IFormatter formatter = new BinaryFormatter(); MemoryStream stream = new MemoryStream(); try { formatter.Serialize(stream, playersubCmd); } catch (SerializationException e) { Console.WriteLine("[subCmdMgr] SavePlayer 序列化:" + e.Message); return(false); } byte[] byteArray = stream.ToArray(); string cmdStr = string.Format("update player set subCmd=@subCmd where id='{0}';", id); MySqlCommand sqlCom = new MySqlCommand(cmdStr, sqlCon); sqlCom.Parameters.Add("@subCmd", MySqlDbType.Blob); sqlCom.Parameters[0].Value = byteArray; try { sqlCom.ExecuteNonQuery(); return(true); } catch (Exception e) { Console.WriteLine("[subCmdMgr] SavePlayer:" + e.Message); return(false); } }
public PlayersubCmd GetPlayersubCmd(string id) { PlayersubCmd playersubCmd = new PlayersubCmd(); string cmdStr = string.Format("select * from player where id='{0}';", id); MySqlCommand sqlCom = new MySqlCommand(cmdStr, sqlCon); byte[] buffer = new byte[1]; try { MySqlDataReader subCmdReader = sqlCom.ExecuteReader(); if (!subCmdReader.HasRows) { subCmdReader.Close(); return(playersubCmd); } subCmdReader.Read(); long len = subCmdReader.GetBytes(1, 0, null, 0, 0); buffer = new byte[len]; subCmdReader.GetBytes(1, 0, buffer, 0, (int)len); subCmdReader.Close(); } catch (Exception e) { Console.WriteLine("[subCmdMgr] GetPlayersubCmd:" + e.Message); return(playersubCmd); } MemoryStream stream = new MemoryStream(buffer); try { BinaryFormatter formatter = new BinaryFormatter(); playersubCmd = (PlayersubCmd)formatter.Deserialize(stream); return(playersubCmd); } catch (SerializationException e) { Console.WriteLine("[subCmdMgr] GetPlayersubCmd 反序列化:" + e.Message); return(playersubCmd); } }
public Player() { this.subCmd = new PlayersubCmd(); }