?? test.aspx.cs
字號:
?using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.OleDb;
//該源碼下載自www.cnzz.cn
public partial class test : System.Web.UI.Page
{
int numberofti = 11;//生成試題的數(shù)量
int[] testlist;//存放10個隨機數(shù)的整型數(shù)組
DataTable Test = new DataTable();//用于存放隨機抽取的試題;
OleDbConnection conn;//數(shù)據(jù)庫連接
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個隨機整數(shù),并存放于數(shù)組中
/// </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();//把數(shù)組里每一個題的ID所對應(yīng)的標(biāo)志位置為1,表示該題已經(jīng)選上
}
da = new OleDbDataAdapter("select * from question where hasselected='1'", conn);//選擇題目
da.Fill(Test);//把選擇的題目放入DataTable
newcomm.CommandText = "update question set hasselected='0' where hasselected='1'";//把已經(jīng)選擇的題目的標(biāo)志位置為0,以便下次再選
conn.Open();
newcomm.ExecuteNonQuery();
conn.Close();
GridView1.DataSource = Test;
GridView1.DataBind();
Button1.Visible = true;
int number = Test.Rows.Count;
}
/// <summary>
/// 按照隨機數(shù)組里的ID號,抽取試題
/// </summary>
/// <param name="temarray"></param>
/// <returns></returns>
private DataTable GetTestTittle(int[] temarray)
{
DataTable testTable = new DataTable();//存放試題的表
DataTable temptable = new DataTable();
OleDbDataAdapter odap = new OleDbDataAdapter("select top 30 * from question", conn);
OleDbCommand cmdselect = new OleDbCommand();
odap.Fill(testTable);
return testTable;
}
/// <summary>
/// 根據(jù)每一個ID號生成SQL語句
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
private string CreateSQL(string id)
{
return "update question set hasselected='1' where id=" + id;
}
/// <summary>
/// 得到隨機數(shù)
/// </summary>
/// <param name="maxvalue">隨機數(shù)的最大值</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;
}
/// <summary>
/// 判斷一個整數(shù)數(shù)組里面是否包括一個整數(shù)
/// </summary>
/// <param name="array">數(shù)組</param>
/// <param name="item">一個整數(shù)</param>
/// <returns></returns>
private bool ArrayHasItem(int[] array, int item)
{
for (int i = 0; i < array.Length; i++)
{
if (array[i] == item)
{
return true;
}
}
return false;
}
/// <summary>
/// 退出系統(tǒng),并轉(zhuǎn)到登陸頁面,清除session
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Button2_Click(object sender, EventArgs e)
{
Session.Clear();
Response.Redirect("default.aspx");
}
/// <summary>
/// 交卷
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Button1_Click(object sender, EventArgs e)
{
int testnumber = Test.Rows.Count;
Response.Write(testnumber.ToString());
for (int i = 0; i < testnumber; i++)
{
string selvalue = ((RadioButtonList)GridView1.Rows[i].FindControl("RadioButtonList2")).SelectedValue;
userselect[i] = selvalue;
trueanswer[i] = Test.Rows[i]["answer"].ToString();
}
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -