这是课程设计的时候写的一个随机组卷的原理代码 ,没有进行相关的优化处理,但是功能已经实现,能够从数据库中随机的取出n条数据组成试卷。
代码如下:
Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration.AppSettingsReader
Namespace WebApplication1
Partial Class SuiJi
Inherits System.Web.UI.Page
#Region " Web 窗体设计器生成的代码 "
'该调用是 Web 窗体设计器所必需的。
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
End Sub
Public db As New SupperSDR.DbConnection
Dim strconn As String = ConfigurationSettings.AppSettings("strdata")
'注意: 以下占位符声明是 Web 窗体设计器所必需的。
'不要删除或移动它。
Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
'CODEGEN: 此方法调用是 Web 窗体设计器所必需的
'不要使用代码编辑器修改它。
InitializeComponent()
End Sub
'*************张海波定义的秘密变量嘿嘿**************
Dim ArrNum, Num As Integer '这个变量是数组长度减去1所余下的数值(arrnum),Num是所要产生的小题的数目
'************秘密变量就藏在这里,一般不会被小菜鸟发现滴**********
#End Region
Dim ArrListXz, ArrListPd, ArrListTk As ArrayList '声明选择判断填空数组用来保存该类试题
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'在此处放置初始化页的用户代码
If Not IsPostBack Then
BindKc()
End If
End Sub
Sub BindKc() '绑定课程
db.DbConn.Open()
db.DbCmd.CommandText = "select distinct(CourseType) from testType"
Dim dr As SqlClient.SqlDataReader = db.DbCmd.ExecuteReader
While dr.Read
KcName.Items.Add(dr(0))
End While
db.DbConn.Close()
End Sub
Private Sub Type_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Type.SelectedIndexChanged
Try
IDArrlist()
Catch ex As Exception
End Try
End Sub
Function IDArrlist() As Integer
'先根据表TestInfo中的CourseType和Type项目索引出试题的Id号码,组成一个以逗号为分隔符的字符串
db.DbConn.Open()
db.DbCmd.CommandText = "select id from testinfo where CourseType=@kecheng and type=@leixing"
db.DbCmd.Parameters.Add("@kecheng", KcName.SelectedValue)
db.DbCmd.Parameters.Add("@leixing", Type.SelectedValue)
Dim dr As SqlClient.SqlDataReader = db.DbCmd.ExecuteReader
ltID.Text = ""
While dr.Read
ltID.Text += dr(0) & ","
End While
Session.Add("ID", ltID.Text)
db.DbConn.Close()
'******开始调用相关函数****
'ChuLiStr()
'*********结束*************
Dim StrId As String = Session("id")
Dim Num As Integer = 2
Dim str() As String = StrId.Replace("(", "").Replace(")", "").Split(",")
Dim ArrNum As Integer = str.Length
Select Case Type.SelectedValue
Case "选择"
Session("xz") = ""
LtXiaoTi.Text += "《选择题》" & "<br>"
Case "判断"
Session("pd") = ""
LtXiaoTi.Text += "《判断题》" & "<br>"
Case "填空"
Session("tk") = ""
LtXiaoTi.Text += "《填空题》" & "<br>"
End Select
For Num = 1 To CInt(txtNUm.Text)
Dim i As Integer = CInt(Int((ArrNum - 1 + 1) * Rnd() + 1))
db.DbConn.Close()
db.DbConn.Open()
db.DbCmd.CommandText = "select * from testinfo where id=" & str(i)
Dim SdrDr As SqlClient.SqlDataReader = db.DbCmd.ExecuteReader
While SdrDr.Read
Select Case Type.SelectedValue
Case "选择"
LtXiaoTi.Text += "【" & SdrDr(0) & "】、" & SdrDr(1) & "<br>"
Session.Add("xz", LtXiaoTi.Text)
Case "判断"
LtXiaoTi.Text += "【" & SdrDr(0) & "】、" & SdrDr(1) & "<br>"
Session.Add("pd", LtXiaoTi.Text)
Case "填空"
LtXiaoTi.Text += "【" & SdrDr(0) & "】、" & SdrDr(1) & "<br>"
Session.Add("tk", LtXiaoTi.Text)
End Select
End While
'Response.Write("这是下标" & i & "值" & str(i))
Next
End Function
'Function ChuLiStr() '分割字符串操作将分割好的字符串组成数组,并返回数组长度
' Dim StrId As String = Session("id")
' Dim str() As String = StrId.Replace("(", "").Replace(")", "").Split(",")
' ArrNum = str.Length - 1
' Response.Write("数组长度" & ArrNum & "<br>" & "这是数组的第一项" & str(0) & "<br>")
' ' Return ArrNum '返回数组的长度
' Num = 3000
' Dim i As Integer
' 'Response.Write("这是什么" & str(CInt(Sjhanshu(1, ArrNum, Num))))
' Response.Write("数组下标:" & Sjhanshu(1, ArrNum, Num) & "<br>对应ID号" & str(CInt(lbNum.Text)) & "<br>" & "数组的第三项是:" & str(2))
' For i = 1 To Num
' 'db.DbConn.Open()
' 'db.DbCmd.CommandText = "select * from testinfo where id=" & Str(Sjhanshu(1, ArrNum, Num)) & ""
' 'Dim SdrDr As OleDbDataReader = db.DbCmd.ExecuteReader
' 'While SdrDr.Read
' ' Response.Write("这是随机取出的题号" & SdrDr(0) & "这是题干" & SdrDr(1) & "结束输出")
' 'End While
' Next
'End Function
Function Sjhanshu(ByVal lowerbound As Integer, ByVal upperbound As Integer) As Integer '这是一个产生随机数字的函数
'这是产生随机数字用的代码
lbNum.Text = ""
lbNum.Text = CInt(Int((upperbound - lowerbound + 1) * Rnd() + lowerbound))
End Function
Private Sub btnCreatPaper_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCreatPaper.Click
LtXiaoTi.Visible = False
LtShiJuan.Text = LtXiaoTi.Text
End Sub
Private Sub BtnSubmit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnSubmit.Click
db.DbConn.Close()
Try
db.DbConn.Open()
db.DbCmd.CommandText = "insert into paperinfo(TestContent,CourseType) values(@SdrNeiRong,@SdrKecheng)"
db.DbCmd.Parameters.Add("@SdrNeiRong", LtShiJuan.Text)
db.DbCmd.Parameters.Add("@SdrKecheng", KcName.SelectedValue)
db.DbCmd.ExecuteNonQuery()
lbmsg.Visible = True
lbmsg.Text = "保存成功"
db.DbConn.Close()
Catch ex As Exception
Response.Write("<script language='javascript'>'保存失败'</script>")
End Try
End Sub
Private Sub txtNUm_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtNUm.TextChanged
Try
Dim Snum As Integer = CInt(txtNUm.Text)
Catch ex As Exception
txtNUm.Text = 10
End Try
End Sub
End Class
End Namespace
代码如下:
Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration.AppSettingsReader
Namespace WebApplication1
Partial Class SuiJi
Inherits System.Web.UI.Page
#Region " Web 窗体设计器生成的代码 "
'该调用是 Web 窗体设计器所必需的。
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
End Sub
Public db As New SupperSDR.DbConnection
Dim strconn As String = ConfigurationSettings.AppSettings("strdata")
'注意: 以下占位符声明是 Web 窗体设计器所必需的。
'不要删除或移动它。
Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
'CODEGEN: 此方法调用是 Web 窗体设计器所必需的
'不要使用代码编辑器修改它。
InitializeComponent()
End Sub
'*************张海波定义的秘密变量嘿嘿**************
Dim ArrNum, Num As Integer '这个变量是数组长度减去1所余下的数值(arrnum),Num是所要产生的小题的数目
'************秘密变量就藏在这里,一般不会被小菜鸟发现滴**********
#End Region
Dim ArrListXz, ArrListPd, ArrListTk As ArrayList '声明选择判断填空数组用来保存该类试题
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'在此处放置初始化页的用户代码
If Not IsPostBack Then
BindKc()
End If
End Sub
Sub BindKc() '绑定课程
db.DbConn.Open()
db.DbCmd.CommandText = "select distinct(CourseType) from testType"
Dim dr As SqlClient.SqlDataReader = db.DbCmd.ExecuteReader
While dr.Read
KcName.Items.Add(dr(0))
End While
db.DbConn.Close()
End Sub
Private Sub Type_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Type.SelectedIndexChanged
Try
IDArrlist()
Catch ex As Exception
End Try
End Sub
Function IDArrlist() As Integer
'先根据表TestInfo中的CourseType和Type项目索引出试题的Id号码,组成一个以逗号为分隔符的字符串
db.DbConn.Open()
db.DbCmd.CommandText = "select id from testinfo where CourseType=@kecheng and type=@leixing"
db.DbCmd.Parameters.Add("@kecheng", KcName.SelectedValue)
db.DbCmd.Parameters.Add("@leixing", Type.SelectedValue)
Dim dr As SqlClient.SqlDataReader = db.DbCmd.ExecuteReader
ltID.Text = ""
While dr.Read
ltID.Text += dr(0) & ","
End While
Session.Add("ID", ltID.Text)
db.DbConn.Close()
'******开始调用相关函数****
'ChuLiStr()
'*********结束*************
Dim StrId As String = Session("id")
Dim Num As Integer = 2
Dim str() As String = StrId.Replace("(", "").Replace(")", "").Split(",")
Dim ArrNum As Integer = str.Length
Select Case Type.SelectedValue
Case "选择"
Session("xz") = ""
LtXiaoTi.Text += "《选择题》" & "<br>"
Case "判断"
Session("pd") = ""
LtXiaoTi.Text += "《判断题》" & "<br>"
Case "填空"
Session("tk") = ""
LtXiaoTi.Text += "《填空题》" & "<br>"
End Select
For Num = 1 To CInt(txtNUm.Text)
Dim i As Integer = CInt(Int((ArrNum - 1 + 1) * Rnd() + 1))
db.DbConn.Close()
db.DbConn.Open()
db.DbCmd.CommandText = "select * from testinfo where id=" & str(i)
Dim SdrDr As SqlClient.SqlDataReader = db.DbCmd.ExecuteReader
While SdrDr.Read
Select Case Type.SelectedValue
Case "选择"
LtXiaoTi.Text += "【" & SdrDr(0) & "】、" & SdrDr(1) & "<br>"
Session.Add("xz", LtXiaoTi.Text)
Case "判断"
LtXiaoTi.Text += "【" & SdrDr(0) & "】、" & SdrDr(1) & "<br>"
Session.Add("pd", LtXiaoTi.Text)
Case "填空"
LtXiaoTi.Text += "【" & SdrDr(0) & "】、" & SdrDr(1) & "<br>"
Session.Add("tk", LtXiaoTi.Text)
End Select
End While
'Response.Write("这是下标" & i & "值" & str(i))
Next
End Function
'Function ChuLiStr() '分割字符串操作将分割好的字符串组成数组,并返回数组长度
' Dim StrId As String = Session("id")
' Dim str() As String = StrId.Replace("(", "").Replace(")", "").Split(",")
' ArrNum = str.Length - 1
' Response.Write("数组长度" & ArrNum & "<br>" & "这是数组的第一项" & str(0) & "<br>")
' ' Return ArrNum '返回数组的长度
' Num = 3000
' Dim i As Integer
' 'Response.Write("这是什么" & str(CInt(Sjhanshu(1, ArrNum, Num))))
' Response.Write("数组下标:" & Sjhanshu(1, ArrNum, Num) & "<br>对应ID号" & str(CInt(lbNum.Text)) & "<br>" & "数组的第三项是:" & str(2))
' For i = 1 To Num
' 'db.DbConn.Open()
' 'db.DbCmd.CommandText = "select * from testinfo where id=" & Str(Sjhanshu(1, ArrNum, Num)) & ""
' 'Dim SdrDr As OleDbDataReader = db.DbCmd.ExecuteReader
' 'While SdrDr.Read
' ' Response.Write("这是随机取出的题号" & SdrDr(0) & "这是题干" & SdrDr(1) & "结束输出")
' 'End While
' Next
'End Function
Function Sjhanshu(ByVal lowerbound As Integer, ByVal upperbound As Integer) As Integer '这是一个产生随机数字的函数
'这是产生随机数字用的代码
lbNum.Text = ""
lbNum.Text = CInt(Int((upperbound - lowerbound + 1) * Rnd() + lowerbound))
End Function
Private Sub btnCreatPaper_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCreatPaper.Click
LtXiaoTi.Visible = False
LtShiJuan.Text = LtXiaoTi.Text
End Sub
Private Sub BtnSubmit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnSubmit.Click
db.DbConn.Close()
Try
db.DbConn.Open()
db.DbCmd.CommandText = "insert into paperinfo(TestContent,CourseType) values(@SdrNeiRong,@SdrKecheng)"
db.DbCmd.Parameters.Add("@SdrNeiRong", LtShiJuan.Text)
db.DbCmd.Parameters.Add("@SdrKecheng", KcName.SelectedValue)
db.DbCmd.ExecuteNonQuery()
lbmsg.Visible = True
lbmsg.Text = "保存成功"
db.DbConn.Close()
Catch ex As Exception
Response.Write("<script language='javascript'>'保存失败'</script>")
End Try
End Sub
Private Sub txtNUm_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtNUm.TextChanged
Try
Dim Snum As Integer = CInt(txtNUm.Text)
Catch ex As Exception
txtNUm.Text = 10
End Try
End Sub
End Class
End Namespace