OleDbDataReader表示它正在读取的表中没有数据(VB.NET)

时间:2013-10-13 11:32:34

标签: sql database vb.net oledb ms-access-2010

我的OleDb访问数据库连接正确计算数据库中表中的行数(条目数),但是当使用for循环读取其中一个字段的每个实例时,我得到的错误是使用databaseReader的表中第一个条目的username(teacher_id / student_id),databaseReader(1)实例中没有数据。

错误:
未处理的类型' System.InvalidOperationException'发生在System.Data.dll中 附加信息:行/列没有数据。

(顺便说一下,我知道还没有密码验证)

Imports System.Data
Imports System.Data.OleDb
Public Class LoginForm
    Const databaseLocation As String = "C:\Users\Patrick\Documents"
    Private Sub OK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)     Handles OKButton.Click
        Dim enteredUsername As String = UsernameTextBox.Text
        Dim enteredPassword As String = PasswordTextBox.Text
        Login(enteredUsername, enteredPassword)
    End Sub

    Private Sub Cancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CancelButton.Click
        Me.Close()
    End Sub

    Sub Login(ByRef username As String, ByRef password As String)
        Dim isUsernameVerified As Boolean
        'This is the admin's username and password, and should not be shown to any other users
        If username = "adminentry" And password = "iamanadmin" Then
            MenuForm.Show()
            Me.Close()
        End If
        isUsernameVerified = VerifyUsernameAndPassword(username, password)
        If isUsernameVerified = True Then
            MenuForm.Show()
            Me.Close()
        Else
            LoginComboBox.Text = "Username or password not found"
        End If

    End Sub

    Function VerifyUsernameAndPassword(ByRef usernameForVerification As String, ByRef passwordForVerification As String)
        'This string tells the connection where the database is, and which provider to use
        Const connectionString As String = ("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & databaseLocation & "\ProjectDatabase.accdb")
        Dim sqlQuery As String
        Dim verified As Boolean
        'database connection required in order to verify username and password inputs
        Using connectionToDatabase As New OleDbConnection(connectionString)
            Dim numberOfEntries As Integer
            'Open the database connection
            connectionToDatabase.Open()
            If LoginComboBox.SelectedItem = "Staff login" Then
                'sql code for counting number of staff ID's
                sqlQuery = "SELECT COUNT(*) AS sqlQuery FROM Teacher_Table"
            Else
                'sql code for counting number of student ID's
                sqlQuery = "SELECT COUNT(*) AS sqlQuery FROM Student_Table"
            End If

            Dim getNumberOfEntriesCommand As New OleDbCommand(sqlQuery, connectionToDatabase)
            'executes counting sql code
            numberOfEntries = getNumberOfEntriesCommand.ExecuteScalar

            If LoginComboBox.SelectedItem = "Staff login" Then
                'sql code for getting staff ID's
                sqlQuery = "SELECT teacher_id FROM Teacher_Table"
            Else
                'sql code for getting student ID's
                sqlQuery = "SELECT student_id FROM Student_Table"
            End If

            Dim loginVerificationCommand As New OleDbCommand(sqlQuery, connectionToDatabase)
            'executes verification sql code
            Dim databaseReader As OleDbDataReader = loginVerificationCommand.ExecuteReader()

            For i = 1 To numberOfEntries
                If databaseReader(i) = usernameForVerification Then
                    verified = True
                End If
            Next
        End Using
        If verified = True Then
            Return True
        Else
            Return False
        End If
    End Function

End Class

1 个答案:

答案 0 :(得分:2)

这是数据阅读器的错误模型。数据读取器公开当前行,并允许您在行中移动。 databaseReader(i)将引用当前行中的字段i,而不是代码所认为的第i行。

使用数据阅读器的标准方法是

Do While databaseReader.Read
    'Do something with the current row
    If databaseReader(0) = usernameForVerification Then
        verified = True
        Exit Loop
    End If
Loop
databaseReader.Close

(我没有使用VB.Net,如果我的语法错误,请道歉)

您可以更好地构建包含要搜索的值的查询。例如:

Select teacher_id From Teacher_Table Where teacher_id = ?

您需要了解参数绑定才能使用它。