vb.net sqlconnection invalidoperationexception连接已打开

eivnm1vs  于 2021-06-25  发布在  Mysql
关注(0)|答案(1)|浏览(315)

当我启动项目时,它工作正常,datagridview工作正常,它显示xampp数据库中的列。但当我单击“添加”按钮时,会显示以下错误:
sqlconnection invalidoperationexception连接已打开
以下是完整的异常详细信息:

"System.InvalidOperationException was unhandled
 HResult=-2146233079
 Message=The connection is already open.
Source=MySql.Data
 StackTrace:
   at MySql.Data.MySqlClient.ExceptionInterceptor.Throw(Exception exception)
   at MySql.Data.MySqlClient.MySqlConnection.Throw(Exception ex)
   at MySql.Data.MySqlClient.MySqlConnection.Open()
   at Library.Form1.Button1_Click(Object sender, EventArgs e) in C:\Users\Aron\Desktop\Programming\Library\Library\Form1.vb:line 40
   at System.Windows.Forms.Control.OnClick(EventArgs e)
   at System.Windows.Forms.Button.OnClick(EventArgs e)
   at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
   at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.ButtonBase.WndProc(Message& m)
   at System.Windows.Forms.Button.WndProc(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
   at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
   at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
   at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
   at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
   at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()
   at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()
   at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine)
   at Library.My.MyApplication.Main(String[] Args) in 17d14f5c-a337-4978-8281-53493378c1071.vb:line 81
   at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
   at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
   at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
   at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Threading.ThreadHelper.ThreadStart()
InnerException: "

这是我的完整代码(对不起,我是初学者)

Imports MySql.Data.MySqlClient
Public Class Form1
    Dim ServerString As String = "Server = localhost;User Id 
    =root;Password=;Database=library"
    Dim SQLConnection As New MySqlConnection(ServerString)

    Sub load()
        Dim query As String = "SELECT * FROM books"
        Dim adpt As New MySqlDataAdapter(query, SQLConnection)
        Dim ds As New DataSet()
        adpt.Fill(ds, "EMP")
        DataGridView1.DataSource = ds.Tables(0)
        SQLConnection.Close()
        TextBox1.Clear()
        TextBox2.Clear()
        TextBox3.Clear()
        TextBox4.Clear()
    End Sub
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        load()
    End Sub
    Private Sub DataGridView1_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
        Dim gridrow As DataGridViewRow = DataGridView1.CurrentRow
        Try
            TextBox1.Text = gridrow.Cells(0).Value.ToString()
            TextBox5.Text = gridrow.Cells(1).Value.ToString()
            TextBox2.Text = gridrow.Cells(2).Value.ToString()
            TextBox3.Text = gridrow.Cells(3).Value.ToString()
            TextBox4.Text = gridrow.Cells(4).Value.ToString()
            TextBox6.Text = gridrow.Cells(5).Value.ToString()

        Catch ex As Exception

        End Try
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim cmd As MySqlCommand

        SQLConnection.Open()
        Try
            cmd = SQLConnection.CreateCommand
            cmd.CommandText = "INSERT INTO Books(Group,Book_Name,Publisher,Author,Publishing_Year)VALUES(@Group,@Book_Name,@Publisher,@Author,@Publishing_Year);"
            cmd.Parameters.AddWithValue("@Group", TextBox5.Text)
            cmd.Parameters.AddWithValue("@Book_Name", TextBox2.Text)
            cmd.Parameters.AddWithValue("@Publisher", TextBox3.Text)
            cmd.Parameters.AddWithValue("@Author", TextBox4.Text)
            cmd.Parameters.AddWithValue("@Publishing_Year", TextBox6.Text)
            cmd.ExecuteNonQuery()
            load()
        Catch ex As Exception

        End Try
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim cmd As MySqlCommand
        SQLConnection.Open()
        Try
            cmd = SQLConnection.CreateCommand()
            cmd.CommandText = "update Books set Group=@Group, Book_Name=@Book_Name, Publisher=@Publisher, Author=@Author, Publishing_Year=@Publishing_Year where Book_ID=@Book_ID ;"
            cmd.Parameters.AddWithValue("@Book_ID", TextBox1.Text)
            cmd.Parameters.AddWithValue("@Group", TextBox5.Text)
            cmd.Parameters.AddWithValue("@Book_Name", TextBox2.Text)
            cmd.Parameters.AddWithValue("@Publisher", TextBox3.Text)
            cmd.Parameters.AddWithValue("@Author", TextBox4.Text)
            cmd.Parameters.AddWithValue("@Publishing_Year", TextBox6.Text)
            cmd.ExecuteNonQuery()
            load()
        Catch ex As Exception

        End Try
    End Sub

    Private Sub Button8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button8.Click
        Dim cmd As MySqlCommand
        SQLConnection.Open()
        Try
            cmd = SQLConnection.CreateCommand()
            cmd.CommandText = "DELETE FROM Books WHERE Book_ID=@Book_ID;"
            cmd.Parameters.AddWithValue("@Book_ID", TextBox1.Text)
            TextBox1.Clear()
            cmd.ExecuteNonQuery()
            load()
        Catch ex As Exception

        End Try
    End Sub

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

    Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
        If DataGridView1.CurrentRow.Index < DataGridView1.Rows.Count Then
            DataGridView1.Rows(DataGridView1.CurrentRow.Index + 1).Selected = True
        End If
    End Sub
End Class
mv1qrgav

mv1qrgav1#

谢谢你的回复,终于收到了。把“组”改成“主题”。。。

相关问题