Visual Basic 测验 1 题无论如何总是错的
Visual Basic quiz 1 questions is always wrong no matter what
简而言之,我有一个包含问题、可能答案和好的答案的数据库。我从数据库中随机抽取 10 个问题,在 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个问题我只是不想过去太多代码
谢谢
您的代码使用字符串在 Possible_Answers
table 中搜索匹配项,而不是将搜索限制为当前 ID_Question。
如果 table 包含两个具有相同答案文本的记录,则该查询很可能与不同问题的答案预期匹配。
所以修复相对容易。当您查询 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
....
现在可以使用标签 属性
重写最终查询
str4 = "SELECT Good_Answer From PossibleAnswers " & _
"where Possible_Answer='" & RadioArray1(i).Text & "' " & _
"AND ID_Question = " & RadioArray1(i).Tag
最后一点。我按照您的风格写了这个答案。但我真的应该警告你,连接字符串来构建命令文本被认为是 the GOTO of Database Programming。你不应该使用这种方法。始终使用参数化查询方法来避免 Sql 注入和解析问题
简而言之,我有一个包含问题、可能答案和好的答案的数据库。我从数据库中随机抽取 10 个问题,在 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个问题我只是不想过去太多代码 谢谢
您的代码使用字符串在 Possible_Answers
table 中搜索匹配项,而不是将搜索限制为当前 ID_Question。
如果 table 包含两个具有相同答案文本的记录,则该查询很可能与不同问题的答案预期匹配。
所以修复相对容易。当您查询 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
....
现在可以使用标签 属性
重写最终查询str4 = "SELECT Good_Answer From PossibleAnswers " & _
"where Possible_Answer='" & RadioArray1(i).Text & "' " & _
"AND ID_Question = " & RadioArray1(i).Tag
最后一点。我按照您的风格写了这个答案。但我真的应该警告你,连接字符串来构建命令文本被认为是 the GOTO of Database Programming。你不应该使用这种方法。始终使用参数化查询方法来避免 Sql 注入和解析问题