Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Database 无论发生什么情况,Visual Basic测验1的问题总是错误的_Database_Vb.net_Visual Studio 2010_Ms Access - Fatal编程技术网

Database 无论发生什么情况,Visual Basic测验1的问题总是错误的

Database 无论发生什么情况,Visual Basic测验1的问题总是错误的,database,vb.net,visual-studio-2010,ms-access,Database,Vb.net,Visual Studio 2010,Ms Access,简而言之,我有一个包含问题、可能答案和好答案的数据库。我从数据库中随机抽取10个问题,在vb.net中创建一个多答案测验。我的问题是,问题7的问题永远都不对。对于其他问题,一切都是有效的。 这是我的数据库 很抱歉进行审查。此信息不应共享 这是我用于此部分的代码(可能不需要代码,这可能只是访问中的一个问题) 这是我的变量 'Variables for connection to database Dim provider As String Dim dataFile As String Dim

简而言之,我有一个包含问题、可能答案和好答案的数据库。我从数据库中随机抽取10个问题,在vb.net中创建一个多答案测验。我的问题是,问题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个问题
谢谢

您的代码使用字符串在
可能的答案
表中搜索匹配项,并且不会将搜索限制到当前ID问题

如果该表包含两条具有相同答案文本的记录,则该查询很可能与针对不同问题的答案匹配

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 
      ....
因此,修复相对容易。当您查询
正确答案时
您还需要为ID问题添加WHERE条件

str4 = "SELECT Good_Answer From PossibleAnswers " & _
       "where Possible_Answer='" & RadioArray1(i).Text & "' " & _
       "AND ID_Question = ?????"
现在的问题是,当您在提交答案的按钮内时,如何解决
ID\u问题。这可以通过使用问题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

最后一个音符。我按照你的风格写了这个答案。但我真的应该警告您,可以考虑将字符串连接起来以生成命令文本。你不应该使用这种方法。始终使用参数化查询方法来避免Sql注入和解析问题

如果您的问题7总是错误的,这意味着您已经搞错了它的索引。在赋值过程中,或者在将用户的答案与正确答案进行比较时。因此,请检查比较代码,并确保您正在将用户对问题7的答案与问题7的正确答案进行比较。如果答案正确,请检查作业。因为问题7是随机选择的,所以没有比较。有时问题不在那里,当问题在那里时,它总是在不同的地方。你有可能用同一个文本得到两个可能的答案吗?您的代码使用字符串在表中搜索匹配项,但不限制搜索相对问题id。如果不同问题中的两个答案具有相同的文本,则您可能会匹配错误的文本。这很可能让我尝试更改它的外观,如@Steve是对的,因为在问题6中似乎可以找到相同的答案