public partial class test : System.Web.UI.Page
{
int numberofti = 11;//生成试题的数量
int[] testlist;//存放10个随机数的整型数组
DataTable Test = new DataTable();//用于存放随机抽取的试题;
OleDbConnection conn;//数据库连接
protected string[] userselect = new string[10];//考生选择的答案
protected string[] trueanswer = new string[10]; //该题正确的答案
protected void Page_Load(object sender, EventArgs e)
{
lblname.Text = (string)Session["username"];
lbllogintime.Text = (string)Session["logintime"];
Button1.Visible = false;
}
/// <summary>
/// 生成10个随机整数,并存放于数组中
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void btnbegin_Click(object sender, EventArgs e)
{
testlist = new int[numberofti];
string connecionstring = (string)Application["connectstring"];
conn = new OleDbConnection(connecionstring);
string commandtext = "select count(id) from question";
OleDbCommand comm = new OleDbCommand(commandtext);
comm.Connection = conn;
OleDbDataAdapter da = new OleDbDataAdapter(commandtext, conn);
DataTable dt_rowcount = new DataTable("rowcount");
da.Fill(dt_rowcount);
int rows = int.Parse(dt_rowcount.Rows[0][0].ToString());
for (int i = 0; i < numberofti; i++)
{
int temp = GetNumber(rows);
while (ArrayHasItem(testlist, temp))
{
temp = GetNumber(rows);
}
testlist[i] = temp;
}
OleDbConnection newconn = new OleDbConnection((string)Application["connectstring"]);
OleDbCommand newcomm = new OleDbCommand();
newcomm.Connection = newconn;
newconn.Open();
for (int x = 0; x < numberofti; x++)
{
newcomm.CommandText = CreateSQL(testlist[x].ToString());
newcomm.ExecuteNonQuery();//把数组里每一个题的ID所对应的标志位置为1,表示该题已经选上
}
da = new OleDbDataAdapter("select * from question where hasselected='1'", conn);//选择题目
da.Fill(Test);//把选择的题目放入DataTable
newcomm.CommandText = "update question set hasselected='0' where hasselected='1'";//把已经选择的题目的标志位置为0,以便下次再选
conn.Open();
newcomm.ExecuteNonQuery();
conn.Close();
GridView1.DataSource = Test;
GridView1.DataBind();
Button1.Visible = true;
int number = Test.Rows.Count;
}
/// <summary>
/// 按照随机数组里的ID号,抽取试题
/// </summary>
/// <param name="temarray"></param>
/// <returns></returns>
private DataTable GetTestTittle(int[] temarray)
{
DataTable testTable = new DataTable();//存放试题的表
DataTable temptable = new DataTable();
www.751com.cn
/// <summary>
/// 根据每一个ID号生成SQL语句
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
private string CreateSQL(string id)
{
return "update question set hasselected='1' where id=" + id;
}
/// <summary>
/// 得到随机数
/// </summary>
/// <param name="maxvalue">随机数的最大值</param>
/// <returns></returns>
private int GetNumber(int maxvalue)
{
Random rd = new Random(DateTime.Now.Second);
int result = rd.Next(maxvalue + 1);
if (result == 0)
result = 1;
return result;
上一页 [1] [2] [3] [4] [5] [6] [7] 下一页