已经有一个与此命令关联的打开DataReader必须先关闭.-帮助:(

时间:2014-11-20 09:53:27

标签: vb.net

Public Class Create_Student
    Dim conn As New OleDbConnection
    Dim myqry As String = Nothing
    Dim mycmd As New OleDbCommand
    Dim mydr As OleDbDataReader
    Private Sub btn_createAccount_Click(sender As System.Object, e As System.EventArgs) Handles btn_createAccount.Click

        'Connecting to the databae
        Try
            With conn
                If .State = ConnectionState.Open Then .Close()
                .ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=Database.accdb"
                .Open()

            End With

        Catch ex As Exception
            MessageBox.Show("Unable to connect", "error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try


        Dim student_Name As String
        Dim student_Username As String
        Dim student_Password As String
        Dim student_Group As String

        student_Name = txt_firstname.Text & " " & txt_surname.Text
        student_Username = LCase(txt_Username.Text)
        student_Password = txt_password.Text
        student_Group = cbo_tutorGroup.SelectedItem


        'This chunk of code is reading the username column in the student account table in my database and doing a read to see if the inputted username is already existent in the table.


        myqry = "SELECT studentUser FROM TblStudents"
        mycmd = New OleDbCommand(myqry, conn)
        mydr = mycmd.ExecuteReader
        While mydr.Read
            Dim user As String = mydr("studentUser").ToString
            If user = student_Username Then
                MsgBox("Username already exists, please choose another")

            Else

                Dim sqlQry As String
                sqlQry = "INSERT INTO TblStudents(studentName, tutorGroup, studentUser, studentPass) VALUES('" & student_Name & "','" & student_Group & "','" & student_Username & "','" & student_Password & "')"
                With mycmd
                    .CommandText = myqry
                    .Connection = conn
                    .ExecuteNonQuery()
                End With

                MsgBox("Your account has successfully been created")

                Me.Hide()
                Login_Student.Show()
            End If
        End While

1 个答案:

答案 0 :(得分:0)

  mycmd = New OleDbCommand(myqry, conn)

这是您声明cmycmd命令的地方。 然后使用

打开阅读器
 mydr.Read

这个读者在你的if语句中仍然是开放的,在你的if语句中你正在打开另一个读者,使用相同的" mycmd" 。这不可能发生,因为你已经打开了这个阅读器,要么声明一个全新的cmd并打开它并在你写完后关闭它,或者尝试以下方法:

Dim test as Boolean = False
While mydr.Read
    Dim user As String = mydr("studentUser").ToString
    If user = student_Username Then
        MsgBox("Username already exists, please choose another")

    Else
        test = True

    End If

End While
'Close connection and reopen conn
Try
    With conn
        If .State = ConnectionState.Open Then .Close()
        .ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=Database.accdb"
        .Open()
    End With

Catch ex As Exception
    MessageBox.Show("Unable to connect", "error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try


If test = True then

    Dim sqlQry As String
        sqlQry = "your query here"
        With mycmd
            .CommandText = myqry
            .Connection = conn
            .ExecuteNonQuery()
        End With

        'Close currently open connection again
        conn.Close()

        MsgBox("Your account has successfully been created")

        Me.Hide()
        Login_Student.Show()

最好将整个连接移动到模块中并在那里运行连接,以及常用的数据库读/写。这样可以减少整体代码,并允许您重用公共代码。