MysqlException 未处理 DataReader 必须关闭此连接 vb.net [英] MysqlException was unhandled DataReader with this connection must be closed vb.net

查看:59
本文介绍了MysqlException 未处理 DataReader 必须关闭此连接 vb.net的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到过这个问题:

错误:已经有一个与此连接关联的打开的 DataReader,必须先关闭它.

ERROR: There is already an open DataReader associated with this Connection which must be closed first.

请看一下我的代码:

 Dim sqlQuery As String = "SELECT * FROM users"
    Dim myAdapter As New MySqlDataAdapter

    If txtUsername.Text = String.Empty And txtPassword.Text = String.Empty Then
        MsgBox("Enter username and password", MsgBoxStyle.Exclamation, "Tea Sparkle POS")
    Else
        Dim sqlquerry = "Select * From users where username = '" + txtUsername.Text + "' And password= '" + txtPassword.Text + "'"
        Dim myCommand As New MySqlCommand()
        myCommand.Connection = SQLConnection
        myCommand.CommandText = sqlquerry
        'Starting The Query
        myAdapter.SelectCommand = myCommand
        Dim mydata As MySqlDataReader
        mydata = myCommand.ExecuteReader()
        'To check the Username and password and to validate the login a
        If mydata.HasRows = 0 Then
            MsgBox("Invalid Login")
            txtPassword.Clear()
            txtUsername.Clear()
        Else
            Dim authorityid = 0
            While mydata.Read()
                authorityid = mydata.GetInt32("authorityid")
            End While
            MsgBox("Welcome " + txtUsername.Text + "!")
            If authorityid = 1 Then
                MainForm.Show()
            Else
                MainForm.Show()
            End If
            Me.Hide()
        End If
    End If


    Private Sub Login_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load

    SQLConnection.ConnectionString = ServerString

    Try
        If SQLConnection.State = ConnectionState.Closed Then
            SQLConnection.Open()
        Else
            SQLConnection.Close()
        End If
    Catch ex As Exception
        MsgBox(ex.ToString)
    End Try

End Sub

这个错误在这一行:

 mydata = myCommand.ExecuteReader()

这有什么问题吗?任何帮助都非常感谢.

What's wrong with this? Any help is truly appreciated.

推荐答案

这是怎么回事?

好吧,您似乎在重复使用现有连接:

Well, it looks like you're reusing an existing connection:

myCommand.Connection = SQLConnection

不要那样做.每次需要与数据库通信时创建一个新连接,并在完成后关闭它,使用 Using 语句确保即使抛出异常也能关闭它.

Don't do that. Create a new connection each time you need to talk to the database, and close it when you've finished, using a Using statement to make sure it gets closed even if an exception is thrown.

此外,对您的命令使用 Using 语句,为您的读者使用另一个 - 这些都是您应该关闭的资源.

Additionally, use a Using statement for your command, and another for your reader - these are all resources you should be closing.

哦,看起来您也在 UI 线程中执行此操作,这是一个坏主意,因为在数据库访问正在进行时,您的 UI 将无响应.

Oh, and it also looks like you're doing this in the UI thread, which is a bad idea as your UI will be unresponsive while the database access is ongoing.

这篇关于MysqlException 未处理 DataReader 必须关闭此连接 vb.net的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆