// POST api/<controller> public async System.Threading.Tasks.Task <string> PostAsync() { string returndata = ""; string result = await Request.Content.ReadAsStringAsync(); JavaScriptSerializer jsonSerializer = new JavaScriptSerializer(); Game1Record game1Record = Newtonsoft.Json.JsonConvert.DeserializeObject <Game1Record>(result); // insert to database record string connetionString = null; SqlConnection connection; SqlCommand command; string sql = null; connetionString = ConfigurationManager.ConnectionStrings["SmartPlaygroundConnectionString"].ConnectionString; sql = "INSERT INTO [Game1Record] (kidId, zoneId, score, wrongColor, boardHit, timestamp) " + "VALUES (@kidId, @zoneId, @score, @wrongColor, @boardHit, @timestamp)"; connection = new SqlConnection(connetionString); try { connection.Open(); command = new SqlCommand(sql, connection); command.Parameters.AddWithValue("@kidId", game1Record.KidId); command.Parameters.AddWithValue("@zoneId", 1); command.Parameters.AddWithValue("@score", game1Record.Score); command.Parameters.AddWithValue("@wrongColor", game1Record.WrongColor); command.Parameters.AddWithValue("@boardHit", game1Record.BoardHit); DateTime recordDT = DateTime.ParseExact(game1Record.Timestamp, "dd/MM/yyyy H:mm:ss", null); command.Parameters.AddWithValue("@timestamp", recordDT); command.ExecuteNonQuery(); command.Dispose(); connection.Close(); returndata = "yes"; } catch (Exception ex) { returndata = ex.Message.ToString(); } //{ // "kidId": 1, // "score": 5, // "takeCorrect": 2, // "takeWrong": 3, // "throwIn": "5", // "throwOut": "6" // "timestamp": "14.04.2019 08:36:14" //} return(returndata); }
// GET api/<controller> public IEnumerable <Game1Record> Get() { List <Game1Record> lsGameRecord = new List <Game1Record>(); string connetionString = null; SqlConnection connection; SqlCommand command; string sql = null; SqlDataReader dataReader; connetionString = ConfigurationManager.ConnectionStrings["SmartPlaygroundConnectionString"].ConnectionString; sql = "SELECT * FROM [Game1Record]"; connection = new SqlConnection(connetionString); try { connection.Open(); command = new SqlCommand(sql, connection); dataReader = command.ExecuteReader(); while (dataReader.Read()) { Game1Record newGame1Record = new Game1Record(); newGame1Record.KidId = int.Parse(dataReader["kidId"].ToString()); newGame1Record.Score = int.Parse(dataReader["score"].ToString()); newGame1Record.WrongColor = int.Parse(dataReader["wrongColor"].ToString()); newGame1Record.BoardHit = int.Parse(dataReader["boardHit"].ToString()); newGame1Record.Timestamp = (dataReader["timeStamp"].ToString()); lsGameRecord.Add(newGame1Record); } dataReader.Close(); command.Dispose(); connection.Close(); } catch (Exception ex) { string err = ex.Message.ToString(); } return(lsGameRecord); }