想要显示所有问题和选项 [英] Want to display all question and options
本文介绍了想要显示所有问题和选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我希望它显示问题到目前为止我已经制作了一个数组,,, thnx
我尝试了什么:
i want it to display question so far i have made an array ,,, thnx
What I have tried:
private void SelectedIndexChanged(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection();
string cmdText = "my command";
SqlDataAdapter da = new SqlDataAdapter(cmdText, conn);
SqlCommand cmd = new SqlCommand(cmdText, conn);
conn.ConnectionString = constring;
try
{
conn.Open();
SqlDataReader rd = cmd.ExecuteReader();
{
textBox1.Text = (rd["Question"].ToString());
radioButton4.Text = rd["opt4"].ToString();
radioButton1.Text = rd["opt1"].ToString();
radioButton2.Text = rd["opt2"].ToString();
radioButton3.Text = rd["opt3"].ToString();
}
}
}
catch (Exception exp)
{
MessageBox.Show(exp.ToString());
}
finally
{
conn.Close();
}
}
}
推荐答案
- 查看关于SQL注入攻击漏洞的评论。永远不要使用连接字符串来创建SQL命令,而是使用参数化查询(如下所示)。
- 没有必要乱搞数组,只需保留在DataTable中读取数据库的结果。
- 使用使用
作为SqlConnection和SqlCommand实现IDisposable。
- 将读取数据库的问题从填充UI中分离出来
我可能会做这样的事情(警告 - 这是未经测试的)
- See the comments about vulnerability to SQL Injection attacks. Never use concatenated strings to create your SQL command, use Parameterised queries instead (like below).
- There is no need to mess around with an array, just keep the results of the database read in a DataTable.
- Make use ofusing
as SqlConnection and SqlCommand implement IDisposable.
- Separate the concerns of reading the database from populating the UI
I would probably do something like this (caveat - this is untested)
private DataTable quest = new DataTable();
private int lastQuest = -1;
private DataTable GetQuestions(string difficulty, string subject)
{
//If one of the inputs is empty return the list of questions we currently have
if (string.IsNullOrEmpty(difficulty) || string.IsNullOrEmpty(subject)) return quest;
var dt = new DataTable();
const string cmdText = "SELECT Question,Opt1,Opt2,Opt3,Opt4 from Questions where DiffLevel=@diff AND SubName= @subj";
using (var conn = new SqlConnection(constring))
{
conn.Open();
using(var cmd = new SqlCommand(cmdText, conn))
{
cmd.Parameters.AddWithValue("@diff", difficulty);
cmd.Parameters.AddWithValue("subj", subject);
var dr = cmd.ExecuteReader();
dt.Load(dr);
}
conn.Close();
}
return dt;
}
private void PopulateQuestion(int questNo)
{
if (questNo > quest.Rows.Count - 1 || questNo < 0 ) return;
textBox1.Text = quest.Rows[questNo]["Question"].ToString();
lastQuest = questNo; //NOTICE THIS !!
radioButton1.Text = quest.Rows[questNo]["opt1"].ToString();
radioButton2.Text = quest.Rows[questNo]["opt2"].ToString();
radioButton3.Text = quest.Rows[questNo]["opt3"].ToString();
radioButton4.Text = quest.Rows[questNo]["opt4"].ToString();
}
private void difflevel_SelectedIndexChanged(object sender, EventArgs e)
{
HandleIndexChanged();
}
private void subj_SelectedIndexChanged(object sender, EventArgs e)
{
HandleIndexChanged();
}
private void HandleIndexChanged()
{
quest = GetQuestions(difflevel.SelectedItem.ToString(), subj.SelectedItem.ToString());
if (quest.Rows.Count > 0)
PopulateQuestion(0); //Populate the first question
}
private void btnNext_Click(object sender, EventArgs e)
{
PopulateQuestion(q + 1);
}
private void btnPrev_Click(object sender, EventArgs e)
{
PopulateQuestion(q - 1);
}
[编辑 - 修改代码以使用全局int lastQuest
存储最后一个问题
这篇关于想要显示所有问题和选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文