を収集します。私は基本的に、ユーザーが自分のパフォーマンスなどに基づいてスコアを受け取る複数の選択肢のクイズを作成しています... 私は、私のサイト。私は多項選択テストのその特定のパスで受け取ったスコアがデータベースに既に割り当てられている値よりも小さい場合、スコアを保存したくありません。私は現在データベースにいるユーザーのスコアを抽出できる必要があります(デフォルトのスコアはデータベーステーブルで0なので、常に値があります)。今受け取ったスコアと比較し、それが大きい場合は保存します新しいスコアをデータベースに追加します。MySqlデータベースからデータを集めて変数
ユーザーのスコアだけを抽出して変数に割り当てて、2つのスコアを比較できるようにする方法はありません。コードを注意してください(
protected void Submit_Click(object sender, EventArgs e)
{
if (!this.IsValid)
return;
int score = 0;
List<RadioButtonList> list = new List<RadioButtonList>() { RadioButtonList1, RadioButtonList2, RadioButtonList3, RadioButtonList4, RadioButtonList5, RadioButtonList6, RadioButtonList7, RadioButtonList8, RadioButtonList9, RadioButtonList10 };
foreach (var element in list)
{
if (element.SelectedValue == "Correct")
{
score++;
}
}
Response.Write("you scored: " + score);
Button1.Visible = false;
if (score != 0) ;
{
string UserId = User.Identity.GetUserId();
string MyConString = "SERVER=localhost;" +
"DATABASE=synther_physics;" +
"UID=root;" +
"PASSWORD=rootpass;";
MySqlConnection connection = new MySqlConnection(MyConString);
MySqlCommand commandPull = connection.CreateCommand();
commandPull.CommandText = "SELECT score FROM userscores WHERE Id = UserID";
int DBscore =
connection.Open();
int RowsAffected = commandPull.ExecuteNonQuery();
connection.Close();
if (DBscore < score) ;
{
Response.Write("New high score! Well done!");
MySqlCommand commandReplace = connection.CreateCommand();
commandReplace.CommandText = "REPLACE INTO userscores (Id, momentsandenergytestscore) VALUES (@Id,@score)";
commandReplace.Parameters.AddWithValue("@Id", UserId);
commandReplace.Parameters.AddWithValue("@score", score);
connection.Open();
int RowsAffectedReplace = commandReplace.ExecuteNonQuery();
connection.Close();
}
else;
{
Response.Write("You didnt beat your previous best of" + DBscore);
}
}
}
私は実際に引っ張るとデータを比較しようとしています一部はここにある:私は
これまでのところ、私はfolowingているテーブルの主キーとしてユーザーIDを使用しています)不完全です:
MySqlCommand commandPull = connection.CreateCommand();
commandPull.CommandText = "SELECT score FROM userscores WHERE Id = UserID";
DBscore =
connection.Open();
int RowsAffected = commandPull.ExecuteNonQuery();
connection.Close();
レスポンスありがとうございます。これを試してみるとエラーが表示されます。オブジェクト参照がオブジェクトのインスタンスに設定されていません。 –
多分問題はここにあります: 'commandPull.CommandText =" SELECT score FROM userscoresどこで=ユーザーID ";"。あなたは '@Id'の' UserId'を変更し、 'commandPull.Parameters.AddWithValue(" @ Id "、UserId);' – Pikoh
はこれを新しい行ですか?変数DBscoreの定義の前に? –