[英]C# How to store value and check with database?
我正在从数据库中随机抽取问题,但是现在我想存储值和问题ID并与数据库核对,看看天气,答案是否正确? 单击提交按钮后,如何存储单选按钮和文本框中的值?
这是我对随机问题的编码
public void showquestion()
{
string Category = "10-20";
string query = "SELECT top 10 * FROM question where Age_group= '"+ Category +"' order by newid()";
if (question.OpenConnection() == true)
{
SqlCommand comm = new SqlCommand(query, question.connection);
question.rdr = comm.ExecuteReader();
while (question.rdr.Read())
{
for (int j = 0; j < 1; j++)
{
HtmlTableRow row = new HtmlTableRow();
for (int i = 0; i < 1; i++)
{
HtmlTableCell cell = new HtmlTableCell();
Label label = new Label();
label.Text = Environment.NewLine + count + ") " + question.rdr["Question"].ToString();
label.ID = "Qustion_" + question.rdr["ID"].ToString() + "Label";
cell.Controls.Add(label);
row.Cells.Add(cell);
StoreQuestion[ArrayCount] = label.ID.ToString();
count++;
ArrayCount++;
}
table1.Rows.Add(row);
}
switch (Convert.ToInt32(question.rdr["question_type"]))
{
case 1:
Database question1 = new Database();
if (question1.OpenConnection() == true)
{
string query1 = "SELECT * FROM Choice WHERE Question_ID= " + question.rdr["id"];
SqlCommand comm1 = new SqlCommand(query1, question1.connection);
question1.rdr = comm1.ExecuteReader();
while (question1.rdr.Read())
{
for (int j = 0; j < 1; j++)
{
HtmlTableRow row = new HtmlTableRow();
for (int i = 0; i < 1; i++)
{
HtmlTableCell cell = new HtmlTableCell();
RadioButton radioButton = new RadioButton();
radioButton.Text = question1.rdr["Choice"].ToString();
radioButton.GroupName = question.rdr["Question"].ToString();
radioButton.ID = question.rdr["Question"].ToString() + question1.rdr["Choice"].ToString();
cell.Controls.Add(radioButton);
row.Cells.Add(cell);
}
table1.Rows.Add(row);
}
}
}
question1.CloseConnection();
break;
case 2://text box fill
for (int j = 0; j < 1; j++)
{
HtmlTableRow row = new HtmlTableRow();
for (int i = 0; i < 1; i++)
{
HtmlTableCell cell = new HtmlTableCell();
TextBox textbox = new TextBox();
textbox.ID = question.rdr["Question"].ToString();
cell.Controls.Add(textbox);
row.Cells.Add(cell);
}
table1.Rows.Add(row);
}
break;
}
}
question.CloseConnection();
protected void Submitbutton_Click(object sender, EventArgs e) {}
}
}
创建一个静态类,如下所示:
public class MySession
{
public static MySession Instance
{
// this is the element you store the question and answer in by using MySession.Instance.QnsAndAnswer.Add(qnID, ansId);
public Dictionary<QuestionID, AnswerID> QnsAndAnswers { get; set; }
get
{
MySession session = (MySession)HttpContext.Current.Session["__MySession__"];
if (session == null)
{
session = new MySession();
HttpContext.Current.Session["__MySession__"] = session;
}
return session;
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.