I have created a game but I want to save the users to a SQL Server database. But once I close the form, all the records are lost. But when I go back to a different form and don't close the application, I get all the records.
This is my connection string in app.config
Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\HighScores.mdf;Integrated Security=True
Code:
connectionString = ConfigurationManager.ConnectionStrings["ChickenBlaster.Properties.Settings.user_dataConnectionString"].ConnectionString;
public SqlConnection conn;
string connectionString;
conn = new SqlConnection(connectionString);
conn.Open();
string query = ("INSERT INTO dbo.Highscore (id,naam) VALUES (@id, @sam)");
SqlCommand cmd = new SqlCommand(query, conn);
cmd.Parameters.AddWithValue("@id", id);
cmd.Parameters.AddWithValue("@naam", txtUsername.Text);
cmd.ExecuteNonQuery();
conn.Close();