private void SaveDataFunc(object parameter) { if (!(parameter is TextBox) || parameter == null) { return; } TextBox NameField = (TextBox)parameter; if (Validation.GetHasError(NameField)) { MessageBox.Show(Validation.GetErrors(NameField)[0].ErrorContent.ToString()); return; } using (SqlConnection db = new SqlConnection(SQLConnectionString.MakeSQLConnectionString())) { SqlCommand query = new SqlCommand($"insert into Scores ([name], score) values (N\'{NameField.Text}\', {moves})", db); SqlDataReader reader; try { db.Open(); reader = query.ExecuteReader(); db.Close(); } catch { MessageBox.Show("Local database is inaccessible, your score would not be saved"); } } CanSave = false; }
public ScoresWindowViewModel() { Scores = new List <ScoreRow>(); using (SqlConnection db = new SqlConnection(SQLConnectionString.MakeSQLConnectionString())) { SqlCommand query = new SqlCommand("select name, score from Scores order by score asc", db); SqlDataReader reader; try { db.Open(); reader = query.ExecuteReader(); SqlDataAdapter adapter = new SqlDataAdapter(query); DataTable table = new DataTable("Scoreboard"); db.Close(); adapter.Fill(table); foreach (DataRow row in table.Rows) { Scores.Add(new ScoreRow((string)row.ItemArray[0], (long)row.ItemArray[1])); } } catch { MessageBox.Show("Local database is inaccessible"); } } }