为了简短起见,我有一个包含问题,可能的答案和好答案的数据库。我从数据库中提取了10个randoms问题,以便在vb.net中创建一个多答案测验。我的问题是ID_Question 7的问题永远不对。一切都适用于其他问题。 这是我的数据库 对不起审查,这些信息不应该被共享
这是我对此部分的代码使用(可能不需要代码,它可能只是访问中的问题)
这是我的变量
'Variables for connection to database
Dim provider As String
Dim dataFile As String
Dim connString As String
Public myConnection As OleDbConnection = New OleDbConnection
Public dr As OleDbDataReader
Public dr2 As OleDbDataReader
Public dr3 As OleDbDataReader
'Array of RadioButtons
Dim RadioArray1() As RadioButton = {Q1a, Q1b, Q1c, Q1d, Q1e}
Dim RadioArray2() As RadioButton = {Q2a, Q2b, Q2c, Q2d, Q2e}
Dim RadioArray3() As RadioButton = {Q3a, Q3b, Q3c, Q3d, Q3e}
Dim RadioArray4() As RadioButton = {Q4a, Q4b, Q4c, Q4d, Q4e}
Dim RadioArray5() As RadioButton = {Q5a, Q5b, Q5c, Q5d, Q5e}
Dim RadioArray6() As RadioButton = {Q6a, Q6b, Q6c, Q6d, Q6e}
Dim RadioArray7() As RadioButton = {Q7a, Q7b, Q7c, Q7d, Q7e}
Dim RadioArray8() As RadioButton = {Q8a, Q8b, Q8c, Q8d, Q8e}
Dim RadioArray9() As RadioButton = {Q9a, Q9b, Q9c, Q9d, Q9e}
Dim RadioArray10() As RadioButton = {Q10a, Q10b, Q10c, Q10d, Q10e}
表格加载时
Private Sub Form1_Load(sender As Object, e As System.EventArgs) Handles Me.Load
'Set up connection to databse, change path depending on location
provider = "Provider=Microsoft.Jet.OLEDB.4.0;"
dataFile = "Data Source=F:\Quiz\Programs\UNZipped\questions.mdb;Jet OLEDB:Database Password=magic;"
connString = provider & dataFile
myConnection.ConnectionString = connString
'Open connection
myConnection.Open()
Dim str As String
Dim str2 As String
Dim str3 As String
'Select 10 random questions
str = "SELECT TOP 10 ID_Question, Question From Questions ORDER BY Rnd(-(100000*ID_Question)*Time())"
Dim cmd As OleDbCommand = New OleDbCommand(str, myConnection)
dr = cmd.ExecuteReader
Dim idArray(9) As Integer
Dim QuestionArray(9) As String
Dim LabelArray() As Label = {Label3, Label4, Label5, Label6, Label7, _
Label8, Label9, Label10, Label11, Label12}
'Dim RadioArray As List(Of RadioButton)
Dim PossibleAnswerList As List(Of String) = New List(Of String)
Dim total As Integer = 0
Dim cnt As Integer = 0
While dr.Read()
'Add id and question title into arrays
idArray(cnt) = dr.GetInt32(dr.GetOrdinal("ID_Question"))
QuestionArray(cnt) = dr("Question").ToString
Dim num As Integer 'numbers of possible answers
str2 = "SELECT Possible_Answer From PossibleAnswers Where ID_Question =" & idArray(cnt) & ""
Dim cmd2 As OleDbCommand = New OleDbCommand(str2, myConnection)
dr2 = cmd2.ExecuteReader
str3 = "SELECT Count(Possible_Answer) From PossibleAnswers Where ID_Question =" & idArray(cnt) & ""
Dim cmd3 As OleDbCommand = New OleDbCommand(str3, myConnection)
num = Convert.ToInt32(cmd3.ExecuteScalar)
While dr2.Read()
'Put all the possible answer of all the selected questions into a list
PossibleAnswerList.Add(dr2("Possible_Answer").ToString)
End While
Select Case cnt
Case 0 'Question 1
For i = 0 To num - 1
'Change text on Radio Button
RadioArray1(i).Text = PossibleAnswerList(total)
total = total + 1
Next
Case 1 'Question 2
For i = 0 To num - 1
RadioArray2(i).Text = PossibleAnswerList(total)
total = total + 1
Next i
Case 2 'Question 3
For i = 0 To num - 1
RadioArray3(i).Text = PossibleAnswerList(total)
total = total + 1
Next i
Case 3 'Question 4
For i = 0 To num - 1
RadioArray4(i).Text = PossibleAnswerList(total)
total = total + 1
Next i
Case 4 'Question 5
For i = 0 To num - 1
RadioArray5(i).Text = PossibleAnswerList(total)
total = total + 1
Next i
Case 5 'Question 6
For i = 0 To num - 1
RadioArray6(i).Text = PossibleAnswerList(total)
total = total + 1
Next i
Case 6 'Question 7
For i = 0 To num - 1
RadioArray7(i).Text = PossibleAnswerList(total)
total = total + 1
Next i
Case 7 'Question 8
For i = 0 To num - 1
RadioArray8(i).Text = PossibleAnswerList(total)
total = total + 1
Next i
Case 8 'Question 9
For i = 0 To num - 1
RadioArray9(i).Text = PossibleAnswerList(total)
total = total + 1
Next i
Case 9 'Question 10
For i = 0 To num - 1
RadioArray10(i).Text = PossibleAnswerList(total)
total = total + 1
Next i
End Select
cnt += 1
End While
'Put the Questions text on the Label(runs 10 times)
For i = 0 To QuestionArray.Length - 1
LabelArray(i).Text = QuestionArray(i)
Next i
myConnection.Close()
End Sub
点击提交时
Private Sub ButtonSubmit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonSubmit.Click
Dim cnnOLEDB As New OleDbConnection
Dim cmdOLEDB As New OleDbCommand
Dim cnnOLEDB2 As New OleDbConnection
Dim cmdOLEDB2 As New OleDbCommand
Dim strConnectionString2 = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source = F:\Quiz\Programs\UNZipped\questions.mdb;Jet OLEDB:Database Password=magic;"
cnnOLEDB2.ConnectionString = strConnectionString2
'Declares a variable that counts the number of correct answers
Dim score As Short = 0
'Repeat this part 10 times for every different questions
For i = 0 To RadioArray1.Length - 1
'Find which button is selected
If RadioArray1(i).Checked = True Then
'Set up connection to databse, change path depending on location
provider = "Provider=Microsoft.Jet.OLEDB.4.0;"
dataFile = "Data Source=F:\Quiz\Programs\UNZipped\questions.mdb;Jet OLEDB:Database Password=magic;"
connString = provider & dataFile
myConnection.ConnectionString = connString
'Open connection
myConnection.Open()
Dim str4 As String
Dim good As Int16
'Check if answer is right
str4 = "SELECT Good_Answer From PossibleAnswers where Possible_Answer='" & RadioArray1(i).Text & "'"
Dim cmd4 As OleDbCommand = New OleDbCommand(str4, myConnection)
good = Convert.ToInt16(cmd4.ExecuteScalar)
End sub
注意:最后一部分重复10次,以验证所有10个问题,我只是不想过去很多代码 谢谢
答案 0 :(得分:2)
您的代码使用字符串搜索Possible_Answers
表中的匹配项,并且不会将搜索限制为当前的ID_Question。
如果该表包含两条具有相同答案文本的记录,则该查询很可能与另一个问题的答案意愿相匹配。
所以修复相对容易。当您查询good_answer
时,您还需要为ID_Question添加WHERE条件....
str4 = "SELECT Good_Answer From PossibleAnswers " & _
"where Possible_Answer='" & RadioArray1(i).Text & "' " & _
"AND ID_Question = ?????"
现在的问题是当你在按钮内提交答案时如何解决ID_Question
。这可以解决使用问题的ID设置Radiobutton数组的Tag属性。
Form_Load
....
While dr.Read()
'Add id and question title into arrays
idArray(cnt) = dr.GetInt32(dr.GetOrdinal("ID_Question"))
.....
Select Case cnt
Case 0 'Question 1
For i = 0 To num - 1
'Change text on Radio Button
RadioArray1(i).Text = PossibleAnswerList(total)
RadioArray1(i).Tag = idArray(cnt)
total = total + 1
Next
....
现在可以使用Tag属性
重写最终查询str4 = "SELECT Good_Answer From PossibleAnswers " & _
"where Possible_Answer='" & RadioArray1(i).Text & "' " & _
"AND ID_Question = " & RadioArray1(i).Tag
最后一点。我在写这个答案时遵循了你的风格。但是我应该警告你,连接字符串来构建命令文本被认为是the GOTO of Database Programming。你永远不应该使用这种方法。始终使用参数化查询方法来避免Sql注入和解析问题