⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 test.aspx.cs

📁 基于jsp和access数据库的在线考试系统
💻 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.51aspx.com(51aspx.com)

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();

        OleDbDataAdapter odap = new OleDbDataAdapter("select top 30 * from question", conn);
        OleDbCommand cmdselect = new OleDbCommand();
        odap.Fill(testTable);
        return testTable;
    }
    /// <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;

    }
    /// <summary>
    /// 判断一个整数数组里面是否包括一个整数
    /// </summary>
    /// <param name="array">数组</param>
    /// <param name="item">一个整数</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>
    /// 退出系统,并转到登陆页面,清除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();
        }
       
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -