我正在开发一个在线考试系统,但我很难尝试从数据库中读取问题,以便在页面加载时显示在aspx上。请帮帮我...扫管笏我做...我试过这段代码,但效果不好。
string cs = ConfigurationManager.ConnectionStrings["OnlineExamDBCS"].ToString();
SqlConnection conn = new SqlConnection(cs);
conn.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandText = "SELECT questionTitle, Answer1, Answer2, Answer3, Answer4, Answer5
FROM tblQuestions WHERE CourseCode = \'" + question + "\'";
SqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
LabelRadio1.Questions = reader["questionTitle"].ToString();
LabelRadio1.Answers = reader["Answer1"].ToString();
LabelRadio1.Answers = reader["Answer2"].ToString();
LabelRadio1.Answers = reader["Answer3"].ToString();
LabelRadio1.Answers = reader["Answer4"].ToString();
LabelRadio1.Answers = reader["Answer5"].ToString();
}
答案 0 :(得分:1)
尝试此代码,这可能会对您有所帮助
protected void Page_Load(object sender,EventArgs e)
{ DataTable dt = new DataTable();
string select_qry = "SELECT questionTitle, Answer1, Answer2, Answer3, Answer4, Answer5 FROM tblQuestions WHERE CourseCode = \'" + question + "\'";
SqlCommand cmd = new SqlCommand(select_qry);
dt= GetData(cmd);
if (dt.Rows.Count > 0)
{
LabelRadio1.Questions = dt.Rows[0]["questionTitle"].ToString();
LabelRadio1.Answers = dt.Rows[0]["Answer1"].ToString();
LabelRadio1.Answers = dt.Rows[0]["Answer2"].ToString();
LabelRadio1.Answers = dt.Rows[0]["Answer3"].ToString();
LabelRadio1.Answers = dt.Rows[0]["Answer4"].ToString();
LabelRadio1.Answers = dt.Rows[0]["Answer5"].ToString();
}
}
public DataTable GetData(SqlCommand cmd)
{
string sqlCon =System.Configuration.ConfigurationManager.ConnectionStrings["OnlineExamDBCS"].ToString();
SqlConnection Con = new SqlConnection(sqlCon);
DataTable dt = new DataTable();
SqlDataAdapter sda = new SqlDataAdapter();
cmd.CommandType = CommandType.Text;
cmd.Connection = Con;
Con.Open();
sda.SelectCommand = cmd;
sda.Fill(dt);
return dt;
}