1
私は試験用紙を作ろうとしています。ファイル内の別のテーブルから質問を受け取り、Q1button
をクリックして回答を確認し、適切なメッセージを印刷することが考えられます。さらに、ユーザーに8つの質問をし、回答が正しく回答されていればcounterCorrectAns
に更新するとします。 Q1button
をクリックすると、ランダムな質問のテキストボックスが特定のテーブルから更新されます。試験OLEDBを使用したデータから数学
private int randomQues=0,number=1,counterCorrectAns=0;
private string correctAns = "";
void SetQues1Box(int number)
{
string whatTake="",fromWhere="",autoNum="";
from.Text =""+ number;
to.Text = "" + 8;
string connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\Users\\hodaya\\Desktop\\Project.accdb";
using (OleDbConnection connection = new OleDbConnection(connectionString))
{
switch (number)
{
case 1: whatTake = "misHQueA, mishAnsA"; fromWhere = "mishvaotA"; autoNum = "mishAnum"; break;
case 2: whatTake = "misHQueB, mishAnsB"; fromWhere = "mishvaotB"; autoNum = "mishBnum"; break;
}
string sql = "SELECT " + whatTake + " FROM " + fromWhere + " WHERE " + autoNum + "=?";
Random r = new Random();
randomQues=r.Next(1, 25);
using (OleDbCommand command = new OleDbCommand(sql, connection))
{
connection.Open();
command.Parameters.AddWithValue(autoNum, randomQues);
OleDbDataReader reader = command.ExecuteReader();
reader.Read();
q1Box.Text = reader.GetString(0);
correctAns = ("" + reader.GetInt32(1));
connection.Close();
//**// }
}
}
}
private void Q1Button_Click(object sender, EventArgs e)
{
string exp;
if (number <= 8)
{
if (ans1Box.Text == correctAns)
{
counterCorrectAns++;
exp = "correct answer";
}
else
exp = "'wrong answer";
MessageBox.Show(exp);
number = number + 1;
SetQues1Box(number);
}
else
{
if (counterCorrectAns >= 4)
MessageBox.Show("your great");
else
MessageBox.Show("you are need more practice");
this.Close();
}
}
私が得るエラー:COM object that has been separated from its underlying RCW cannot be used.
は、// ** //を先頭に付けています。
使用してブロックの終了前にクローズしなければならないことを助けた!!ありがとうございました。 – user1358578