在线考试系统随机抽题答题整理部分代码

//登陆页面验证码代码

  private string RndNum(int VcodeNum)
    {
        string MaxNum = "";
        string MinNum = "";
        for (int i = 0; i < 5; i++)//这里的4是验证码的位数
        {
            MaxNum = MaxNum + "5";
        }
        MinNum = MaxNum.Remove(0, 1);
        Random rd = new Random();
        string VNum = Convert.ToString(rd.Next(Convert.ToInt32(MinNum), Convert.ToInt32(MaxNum)));
        return VNum;
    }

//在线考试系统公共类的设计代码

public class BaseClass
{
 public BaseClass()
 {
  //
  // TODO: 在此处添加构造函数逻辑
  //
 }
    public static SqlConnection DBCon()    建立数据库的公共方法
    {
        return new SqlConnection("server=.;database=db_ExamOnline;uid=sa;pwd=123456");
    }

    public static void BindDG(GridView dg, string id, string strSql, string Tname)
    {
        SqlConnection conn = DBCon();   //连接数据库
        SqlDataAdapter sda = new SqlDataAdapter(strSql, conn);
        DataSet ds = new DataSet();
        sda.Fill(ds, Tname);
        dg.DataSource = ds.Tables[Tname];
        dg.DataKeyNames = new string[] { id };
        dg.DataBind();
    }
    public static void OperateData(string strsql)   //建立一个执行sql语句的方法
    {
        SqlConnection conn = DBCon();
        conn.Open();
        SqlCommand cmd = new SqlCommand(strsql, conn);
        cmd.ExecuteNonQuery();
        conn.Close();
    }
    //================如果是学生登录=========================================
    public static bool CheckStudent(string studentNum, string studentPwd)
    {
        SqlConnection conn = DBCon();
        conn.Open();
        SqlCommand cmd = new SqlCommand("select count(*) from tb_Student where StudentNum='" + studentNum + "' and StudentPwd='" + studentPwd + "'", conn);
        int i = Convert.ToInt32(cmd.ExecuteScalar());   //返回值
        if (i > 0)
        {
            return true;
        }
        else
        {
            return false;
        }
        conn.Close();
    }
    //================如果是教师登录=========================================
    public static bool CheckTeacher(string teacherNum, string teacherPwd)
    {
        SqlConnection conn = DBCon();
        conn.Open();
        SqlCommand cmd = new SqlCommand("select count(*) from tb_Teacher where TeacherNum='" + teacherNum + "' and TeacherPwd='" + teacherPwd + "'", conn);
        int i = Convert.ToInt32(cmd.ExecuteScalar());
        if (i > 0)
        {
            return true;
        }
        else
        {
            return false;
        }
        conn.Close();
    }
    //================如果是管理员登录=========================================
    public static bool CheckAdmin(string adminNum, string adminPwd)
    {
        SqlConnection conn = DBCon();
        conn.Open();
        SqlCommand cmd = new SqlCommand("select count(*) from tb_Admin where AdminNum='" + adminNum + "' and adminPwd='" + adminPwd + "'", conn);
        int i = Convert.ToInt32(cmd.ExecuteScalar());
        if (i > 0)
        {
            return true;
        }
        else
        {
            return false;
        }
        conn.Close();
    }
}

//随机抽题答题代码

public partial class student_StartExam : System.Web.UI.Page
{
    public string Ans = null;
    public int tNUM;
    protected void Page_Load(object sender, EventArgs e)
    {
            lblEndtime.Text = "考试时间为10分钟,每小题2分,考试已用时:";
            lblStuNum.Text = Session["ID"].ToString();
            lblStuName.Text = Session["name"].ToString();
            lblStuSex.Text = Session["sex"].ToString();
            lblStuKM.Text = "[" + Session["KM"].ToString() + "]" + "考试试题";
            int i = 1;
            SqlConnection conn = BaseClass.DBCon();
            conn.Open();

            //从数据表中随机抽取十条数据
            SqlCommand cmd = new SqlCommand("select top 10 * from tb_test where testCourse='" + Session["KM"].ToString() + "' order by newid()", conn);

             SqlDataReader sdr = cmd.ExecuteReader();
            while (sdr.Read())
            {

                Literal littxt = new Literal();   创建literal 控件
                Literal litti = new Literal();
                RadioButtonList cbk = new RadioButtonList();
                cbk.ID = "cbk" + i.ToString();
                littxt.Text = i.ToString() + "、" + Server.HtmlEncode(sdr["testContent"].ToString()) + "

";
                litti.Text = "
";
                cbk.Items.Add("A. " + Server.HtmlEncode(sdr["testAns1"].ToString()));  //添加A,B,C,D选项
                cbk.Items.Add("B. " + Server.HtmlEncode(sdr["testAns2"].ToString()));
                cbk.Items.Add("C. " + Server.HtmlEncode(sdr["testAns3"].ToString()));
                cbk.Items.Add("D. " + Server.HtmlEncode(sdr["testAns4"].ToString()));
                cbk.Font.Size = 11;
                for (int j = 1; j <= 4; j++)
                {
                    cbk.Items[j - 1].Value = j.ToString();
                }
                Ans += sdr[6].ToString();
                if (Session["a"] == null)
                {
                    Session["Ans"] = Ans;
                }
                Panel1.Controls.Add(littxt);  //将控件添加到Panel中
                Panel1.Controls.Add(cbk);
                Panel1.Controls.Add(litti);
                i++;
                tNUM++;
            }
            sdr.Close();
            conn.Close();
            Session["a"] = 1;
    }
    protected void btnsubmit_Click(object sender, EventArgs e)
    {
        string msc = "";
        for (int i = 1; i <= tNUM; i++)
        {
            RadioButtonList list = (RadioButtonList)Panel1.FindControl("cbk" + i.ToString());
            if (list != null)
            {
                if (list.SelectedValue.ToString() != "")
                {

                    msc += list.SelectedValue.ToString();
                }
                else
                {
                    msc += "0";
                }
            }
        }
        Session["Sans"] = msc;//考生答案
        string sql = "update tb_score set RigthAns='" + Ans + "' where StudentID='" + lblStuNum.Text + "'";
        BaseClass.OperateData(sql);
        string strsql = "update tb_score set StudentAns='" + msc + "' where StudentID='" + lblStuNum.Text + "'";
        BaseClass.OperateData(strsql);
        Response.Redirect("result.aspx?BInt=" + tNUM.ToString());
    }
}

你可能感兴趣的:(在线考试系统随机抽题答题整理部分代码)