📄 exam.cs
字号:
using System;
using System.Data;
using System.Configuration;
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.SqlClient;
using System.IO;
namespace ExamOnline
{
//该源码下载自www.51aspx.com(51aspx.com)
/// <summary>
/// 和试卷读取相关的类
/// </summary>
public class Exam
{
string strcon = ""; //连接字符串
/// <summary>
/// 构造函数,连接字符串
/// </summary>
public Exam()
{
if (strcon == "")
{
if (HttpContext.Current.Application["strcon"] == null)
{
string path = HttpContext.Current.Request.PhysicalApplicationPath + "DBSet.ini";//获取文件物理路径
StreamReader sr = new StreamReader(path, System.Text.Encoding.Default);
strcon = sr.ReadLine();//读取文件内容
HttpContext.Current.Application["strcon"] = strcon;
}
else
{
strcon = HttpContext.Current.Application["strcon"].ToString();
}
}
}
/// <summary>
/// 取得当前可以考试的试卷ID
/// </summary>
/// <returns></returns>
public int getCurrentPaperID()
{
SqlConnection con = new SqlConnection(strcon);
string strcmdExam = "select * from testpaper_list where paper_time > @dtNow and test=1 and audit=1 order by paper_time asc ";
SqlCommand cmdExam = new SqlCommand(strcmdExam, con);
cmdExam.Parameters.Add("@dtNow", SqlDbType.DateTime).Value =(new Login()).dsDelay(30);//调用自定义函数dsDelay()
con.Open();
SqlDataReader drExam = cmdExam.ExecuteReader();
if (drExam.Read())
{
return drExam.GetInt32(0);
}
else
{
return 0;
}
}
/// <summary>
/// 取得当前考试试卷的名称
/// </summary>
/// <returns></returns>
public string getCurrentPageTitle()
{
SqlConnection con = new SqlConnection(strcon);
string strcmdExam = "select paper_name from testpaper_list where paper_id=@ID";
SqlCommand cmdExam = new SqlCommand(strcmdExam,con);
cmdExam.Parameters.Add("@ID",SqlDbType.Int).Value = getCurrentPaperID();
con.Open();
string strExamTitle = "信息读取错误";
SqlDataReader dr = cmdExam.ExecuteReader();
if (dr.Read())
{
strExamTitle = dr.GetString(0);
con.Close();
}
return strExamTitle;
}
public string getCurrentPageTitle(int paperid)
{
SqlConnection con = new SqlConnection(strcon);
string strcmdExam = "select paper_name from testpaper_list where paper_id=@ID";
SqlCommand cmdExam = new SqlCommand(strcmdExam, con);
cmdExam.Parameters.Add("@ID", SqlDbType.Int).Value = paperid;
con.Open();
string strExamTitle = "信息读取错误";
SqlDataReader dr = cmdExam.ExecuteReader();
if (dr.Read())
{
strExamTitle = dr.GetString(0);
con.Close();
}
return strExamTitle;
}
/// <summary>
/// 取得考生详细信息,通过学号
/// </summary>
/// <param name="stu_id"></param>
/// <returns></returns>
public string getCurrentStudMessage(string stu_id)
{
SqlConnection con = new SqlConnection(strcon);
string strcmdExam = "select name,sex,grade,major,class from students where stu_id = @ID";
SqlCommand cmdExam = new SqlCommand(strcmdExam,con);
cmdExam.Parameters.Add("@ID",SqlDbType.VarChar).Value = stu_id;
con.Open();
SqlDataReader drExam = cmdExam.ExecuteReader();
if (drExam.Read())
{
string strName = drExam.GetString(0);
string strSex = drExam.GetString(1);
string strGrade = drExam.GetString(2);
string strMajor = drExam.GetString(3);
string strClass = drExam.GetString(4);
string strAll = "学号:<u>" + stu_id + "</u>;姓名:<u>" + strName + "</u>;性别:<u>" + strSex + "</u>;年级:<u>" + strGrade + "</u>;专业:<u>" + strMajor + "</u>;班级:<u>" + strClass + "</u>;";
drExam.Close();
con.Close();
return strAll;
}
else
{
con.Close();
string strMessage = "数据读取错误!";
return strMessage;
}
}
/// <summary>
/// 通过试卷ID来统计该试卷的总题量
/// </summary>
/// <param name="paperID"></param>
/// <returns></returns>
public int getQuesNum(int paperID)
{
SqlConnection con = new SqlConnection(strcon);
string strcmdExam = "select count(*) from testpaper where paper_id=@paperID";
SqlCommand cmdExam = new SqlCommand(strcmdExam,con);
cmdExam.Parameters.Add("@paperID",SqlDbType.Int).Value = paperID;
con.Open();
SqlDataReader drExam = cmdExam.ExecuteReader();
if(drExam.Read())
{
int quesNum = drExam.GetInt32(0);
con.Close();
return quesNum;
}
con.Close();
return 0;
}
/// <summary>
/// 通过试卷ID来统计该试卷的总分
/// </summary>
/// <param name="paperID"></param>
/// <returns></returns>
public int getQuesScore(int paperID)
{
SqlConnection con = new SqlConnection(strcon);
string strcmdExam = "select qscore from testpaper where paper_id=@paperID";
SqlCommand cmdExam = new SqlCommand(strcmdExam, con);
cmdExam.Parameters.Add("@paperID", SqlDbType.Int).Value = paperID;
con.Open();
SqlDataReader drExam = cmdExam.ExecuteReader();
int scoreAll = 0;
while (drExam.Read())
{
scoreAll += drExam.GetInt32(0);
}
con.Close();
return scoreAll;
}
/// <summary>
/// 通过试卷ID来取得试卷的题型
/// </summary>
/// <param name="paperID"></param>
/// <returns></returns>
public DataTable getStyles(int paperID)
{
SqlConnection con = new SqlConnection(strcon);
string strcmdExam = "select distinct q.sid,s.sname from styles s left join questions q on q.sid = s.sid left join testpaper t on t.qid = q.qid where t.paper_id =@paperID order by q.sid asc";
SqlCommand cmdExam = new SqlCommand(strcmdExam, con);
cmdExam.Parameters.Add("@paperID", SqlDbType.Int).Value = paperID;
con.Open();
SqlDataAdapter da = new SqlDataAdapter(cmdExam);
DataTable dt = new DataTable();
DataSet ds = new DataSet();
da.Fill(ds, "table");
dt = ds.Tables["table"];
con.Close();
return dt;
}
/// <summary>
/// 通过试卷ID和题型ID来取得某一个题型题目table[q_num 题目顺序,qid 题目ID,content题目内容]
/// </summary>
/// <param name="paperID">试卷ID</param>
/// <param name="sID">题型ID</param>
/// <returns>返回该题型的题目datatable</returns>
public DataTable getQues(int paperID,int sID)
{
SqlConnection con = new SqlConnection(strcon);
string strcmdExam = "select tp.q_num,tp.qid,qs.content from testpaper tp left join questions qs on tp.qid=qs.qid where tp.paper_id=@paperID and qs.sid=@sID order by tp.q_num";
SqlCommand cmdExam = new SqlCommand(strcmdExam, con);
cmdExam.Parameters.Add("@paperID", SqlDbType.Int).Value = paperID;
cmdExam.Parameters.Add("@sID", SqlDbType.Int).Value = sID;
con.Open();
SqlDataAdapter da = new SqlDataAdapter(cmdExam);
DataTable dt = new DataTable();
DataSet ds = new DataSet();
da.Fill(ds, "table");
dt = ds.Tables["table"];
con.Close();
return dt;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -