简体   繁体   中英

VB.NET: Too many connections

While testing my program, an error popped up saying, "Too many connections" when I tried to log in an another user.

I need to fix it so that the connections aren't left open to time out on their own, so that it runs perfectly.

 Private Sub addstub()
    Using connection As New MySqlConnection(connectionstring)
        SQL = "SELECT count(*) from remaining_ham where Stub=@stub and Emp_No LIKE '%" & txtid.Text & "%'"
        Using Command As New MySqlCommand(SQL, connection)
            Command.Parameters.AddWithValue("@stub", txtclaim.Text)
            Command.Parameters.AddWithValue("@claim", "CLAIMED")
            Command.CommandText = SQL
            connection.Open()
            Dim count As Integer = Command.ExecuteScalar

            If count = 1 Then
                MsgBox("PROCESSING")
                Using connection2 As New MySqlConnection(connectionstring)
                    SQL = "Select count(*) from  remaining_ham where status='CLAIMED' and Stub='" & txtclaim.Text & "' and Emp_No LIKE'%" & txtid.Text & "%' "
                    Using command2 As New MySqlCommand(SQL, connection)
                        connection2.Open()
                        Dim i1 As Integer = command2.ExecuteScalar()
                        If i1 = 1 Then
                            MsgBox("ALREADY CLAIMED")
                        Else
                            Using connection3 As New MySqlConnection(connectionstring)
                                SQL = "Select Stub,Total,Brickham,Jamon,Fiesta,status from remaining_ham where Stub='" & txtclaim.Text & "'  and Emp_No LIKE'%" & txtid.Text & "%' "
                                Using myAdapter As New MySqlDataAdapter(SQL, connection)
                                    Dim table = New DataSet
                                    myAdapter.Fill(table)

                                    txtbrick.Text = table.Tables(0).Rows(0)("Brickham").ToString
                                    txtjamon.Text = table.Tables(0).Rows(0)("Jamon").ToString
                                    txtfiesta.Text = table.Tables(0).Rows(0)("Fiesta").ToString
                                    txttotal.Text = table.Tables(0).Rows(0)("Total").ToString

                                    If MsgBox("ARE YOU SURE?" + Environment.NewLine + "Stub No: " + txtid.Text + Environment.NewLine + "Brickham: " + txtbrick.Text + Environment.NewLine + "Jamon De Bola: " + txtjamon.Text + Environment.NewLine + "Fiesta Ham: " + txtfiesta.Text, MsgBoxStyle.YesNo) = MsgBoxResult.Yes Then
                                        'MsgBox("TAMA")
                                        jam = CDbl(txtjamon.Text)
                                        rmham = CDbl(txtremjamon.Text)
                                        txtremjamon.Text = (rmham - jam).ToString
                                        Dim dbjam = Format(CDbl(txtremjamon.Text), "#,###")
                                        If dbjam = "" Then
                                            dbjam = 0
                                        End If

                                        brk = CDbl(txtbrick.Text)
                                        rembrk = CDbl(txtrembrick.Text)
                                        txtrembrick.Text = (rembrk - brk).ToString
                                        Dim dbbrick = Format(CDbl(txtrembrick.Text), "#,###")
                                        If dbbrick = "" Then
                                            dbbrick = 0
                                        End If

                                        fiesta = CDbl(txtfiesta.Text)
                                        rmfiesta = CDbl(txtremfiesta.Text)
                                        txtremfiesta.Text = (rmfiesta - fiesta).ToString
                                        Dim dbfiesta = Format(CDbl(txtremfiesta.Text), "#,###")
                                        If dbfiesta = "" Then
                                            dbfiesta = 0
                                        End If
                                        total = CDbl(txttotal.Text)
                                        rmtotal = CDbl(txtremtotal.Text)
                                        txtremtotal.Text = (rmtotal - total).ToString
                                        Dim dbtotal = Format(CDbl(txtremtotal.Text), "#,###")
                                        If dbtotal = "" Then
                                            dbtotal = 0
                                        End If

                                        Using connection4 As New MySqlConnection(connectionstring)
                                            SQL = "UPDATE order_ham SET rem_brick='" & dbbrick & "', rem_jam='" & dbjam & "', rem_fiesta='" & dbfiesta & "', rem_total='" & dbtotal & "' where Emp_No=" & txtid.Text & "  "
                                            Using command3 As New MySqlCommand(SQL, connection4)
                                                connection4.Open()
                                                Dim i As Integer = command3.ExecuteNonQuery
                                                If i = 0 Then
                                                    MsgBox("WRONG")
                                                    Exit Sub
                                                Else
                                                    ' MsgBox("RIGHT")
                                                    Using connection5 As New MySqlConnection(connectionstring)
                                                        Dim date1 As Date = Date.Today
                                                        SQL = "UPDATE remaining_ham SET status='CLAIMED',ddate='" & DateTime.Now & "' where Stub='" + txtclaim.Text + "' and Emp_No LIKE '%" + txtid.Text + "%'  "
                                                        Using command4 As New MySqlCommand(SQL, connection5)
                                                            connection5.Open()
                                                            Dim a As Integer = command4.ExecuteNonQuery
                                                            connection5.Close()
                                                            If a = 0 Then
                                                                MsgBox("not claim: ERROR ")
                                                                Exit Sub
                                                            Else
                                                                MsgBox("SUCCESS")
                                                                Using da As New MySqlDataAdapter(SQL, connection5)
                                                                    Dim dt As New DataTable
                                                                    da.Fill(dt)
                                                                    MetroGrid1.DataSource = Nothing
                                                                    MetroGrid1.Rows.Clear()
                                                                    MetroGrid3.DataSource = Nothing
                                                                    MetroGrid3.Rows.Clear()
                                                                    loadRemainingHam()
                                                                    remainingorder()
                                                                    txtclaim.Focus()
                                                                End Using


                                                            End If


                                                        End Using
                                                    End Using

                                                End If
                                            End Using
                                        End Using




                                    Else
                                        MsgBox("CANCELLED")
                                    End If

                                End Using
                            End Using
                        End If

                    End Using
                End Using
            Else
                MsgBox("ERROR")
            End If

        End Using
    End Using
End Sub

Thank you for those who will answer.

You can perform multiple commands/actions with 1 connection.

You could try something like this:

Dim connection As New MySqlConnection(connectionstring)
connection.Open()
Dim SQL as String = "SELECT count(*) from remaining_ham where Stub=@stub and Emp_No LIKE '%" & txtid.Text & "%'"

Using ObjCommand As New MySqlCommand(SQL, connection)
    ObjCommand.Parameters.AddWithValue("@stub", txtclaim.Text)
    ObjCommand.Parameters.AddWithValue("@claim", "CLAIMED")
    ObjCommand.CommandText = SQL

    Dim count As Integer = Command.ExecuteScalar

    If count = 1 Then

        MsgBox("PROCESSING")
        SQL = "Select count(*) from  remaining_ham where status='CLAIMED' and Stub='" & txtclaim.Text & "' and Emp_No LIKE'%" & txtid.Text & "%' "

        Using ObjCommand2 As New MySqlCommand(SQL, connection)

            Dim i1 As Integer = command2.ExecuteScalar()

            If i1 = 1 Then

                MsgBox("ALREADY CLAIMED")

            Else

                SQL = "Select Stub,Total,Brickham,Jamon,Fiesta,status from remaining_ham where Stub='" & txtclaim.Text & "'  and Emp_No LIKE'%" & txtid.Text & "%' "

                Using myAdapter As New MySqlDataAdapter(SQL, connection)

                    //code here 

                End Using

            End If

        End Using

    End If

End Using
connection.Close()
connection.Dispose()

What is the database of this program? can you please add ;pooling=false at the end of your mysqlconnection like this

server=localhost;user=root;database=world;port=3306;password=******;pooling=false

and try it

You have way to many queries inside of queries. Try to remove as much as possible. Here's an example with the first one. You should close the connections as soon as possible.

Private Sub addstub()

    '*** GET THE COUNT, CLOSE THE CONNECTION, NOT NEEDED ANYMORE.
    Dim count As Integer

    Using connection As New MySqlConnection(connectionstring)
        SQL = "SELECT count(*) from remaining_ham where Stub=@stub and Emp_No LIKE '%" & txtid.Text & "%'"
        Using Command As New MySqlCommand(SQL, connection)
            Command.Parameters.AddWithValue("@stub", txtclaim.Text)
            Command.Parameters.AddWithValue("@claim", "CLAIMED")
            Command.CommandText = SQL
            connection.Open()
            count = Command.ExecuteScalar
        End Using
    End Using

            If count = 1 Then
                MsgBox("PROCESSING")
                Using connection2 As New MySqlConnection(connectionstring)
                    SQL = "Select count(*) from  remaining_ham where status='CLAIMED' and Stub='" & txtclaim.Text & "' and Emp_No LIKE'%" & txtid.Text & "%' "
                    Using command2 As New MySqlCommand(SQL, connection)
                        connection2.Open()
                        Dim i1 As Integer = command2.ExecuteScalar()
                        If i1 = 1 Then
                            MsgBox("ALREADY CLAIMED")
                        Else
                            Using connection3 As New MySqlConnection(connectionstring)
                                SQL = "Select Stub,Total,Brickham,Jamon,Fiesta,status from remaining_ham where Stub='" & txtclaim.Text & "'  and Emp_No LIKE'%" & txtid.Text & "%' "
                                Using myAdapter As New MySqlDataAdapter(SQL, connection)
                                    Dim table = New DataSet
                                    myAdapter.Fill(table)

                                    txtbrick.Text = table.Tables(0).Rows(0)("Brickham").ToString
                                    txtjamon.Text = table.Tables(0).Rows(0)("Jamon").ToString
                                    txtfiesta.Text = table.Tables(0).Rows(0)("Fiesta").ToString
                                    txttotal.Text = table.Tables(0).Rows(0)("Total").ToString

                                    If MsgBox("ARE YOU SURE?" + Environment.NewLine + "Stub No: " + txtid.Text + Environment.NewLine + "Brickham: " + txtbrick.Text + Environment.NewLine + "Jamon De Bola: " + txtjamon.Text + Environment.NewLine + "Fiesta Ham: " + txtfiesta.Text, MsgBoxStyle.YesNo) = MsgBoxResult.Yes Then
                                        'MsgBox("TAMA")
                                        jam = CDbl(txtjamon.Text)
                                        rmham = CDbl(txtremjamon.Text)
                                        txtremjamon.Text = (rmham - jam).ToString
                                        Dim dbjam = Format(CDbl(txtremjamon.Text), "#,###")
                                        If dbjam = "" Then
                                            dbjam = 0
                                        End If

                                        brk = CDbl(txtbrick.Text)
                                        rembrk = CDbl(txtrembrick.Text)
                                        txtrembrick.Text = (rembrk - brk).ToString
                                        Dim dbbrick = Format(CDbl(txtrembrick.Text), "#,###")
                                        If dbbrick = "" Then
                                            dbbrick = 0
                                        End If

                                        fiesta = CDbl(txtfiesta.Text)
                                        rmfiesta = CDbl(txtremfiesta.Text)
                                        txtremfiesta.Text = (rmfiesta - fiesta).ToString
                                        Dim dbfiesta = Format(CDbl(txtremfiesta.Text), "#,###")
                                        If dbfiesta = "" Then
                                            dbfiesta = 0
                                        End If
                                        total = CDbl(txttotal.Text)
                                        rmtotal = CDbl(txtremtotal.Text)
                                        txtremtotal.Text = (rmtotal - total).ToString
                                        Dim dbtotal = Format(CDbl(txtremtotal.Text), "#,###")
                                        If dbtotal = "" Then
                                            dbtotal = 0
                                        End If

                                        Using connection4 As New MySqlConnection(connectionstring)
                                            SQL = "UPDATE order_ham SET rem_brick='" & dbbrick & "', rem_jam='" & dbjam & "', rem_fiesta='" & dbfiesta & "', rem_total='" & dbtotal & "' where Emp_No=" & txtid.Text & "  "
                                            Using command3 As New MySqlCommand(SQL, connection4)
                                                connection4.Open()
                                                Dim i As Integer = command3.ExecuteNonQuery
                                                If i = 0 Then
                                                    MsgBox("WRONG")
                                                    Exit Sub
                                                Else
                                                    ' MsgBox("RIGHT")
                                                    Using connection5 As New MySqlConnection(connectionstring)
                                                        Dim date1 As Date = Date.Today
                                                        SQL = "UPDATE remaining_ham SET status='CLAIMED',ddate='" & DateTime.Now & "' where Stub='" + txtclaim.Text + "' and Emp_No LIKE '%" + txtid.Text + "%'  "
                                                        Using command4 As New MySqlCommand(SQL, connection5)
                                                            connection5.Open()
                                                            Dim a As Integer = command4.ExecuteNonQuery
                                                            connection5.Close()
                                                            If a = 0 Then
                                                                MsgBox("not claim: ERROR ")
                                                                Exit Sub
                                                            Else
                                                                MsgBox("SUCCESS")
                                                                Using da As New MySqlDataAdapter(SQL, connection5)
                                                                    Dim dt As New DataTable
                                                                    da.Fill(dt)
                                                                    MetroGrid1.DataSource = Nothing
                                                                    MetroGrid1.Rows.Clear()
                                                                    MetroGrid3.DataSource = Nothing
                                                                    MetroGrid3.Rows.Clear()
                                                                    loadRemainingHam()
                                                                    remainingorder()
                                                                    txtclaim.Focus()
                                                                End Using


                                                            End If


                                                        End Using
                                                    End Using

                                                End If
                                            End Using
                                        End Using




                                    Else
                                        MsgBox("CANCELLED")
                                    End If

                                End Using
                            End Using
                        End If

                    End Using
                End Using
            Else
                MsgBox("ERROR")
            End If

End Sub

The best thing you could do would be to put each query in a function. Also, parametrize everything!

Private Function GetClaimCount(ByVal id As String, ByVal claim As String) As Integer

    Dim count As Integer = 0

    Using connection As New MySqlConnection(connectionstring)
        ' id should be a parameter!!
        SQL = "SELECT count(*) from remaining_ham where Stub=@stub and Emp_No LIKE '%" & id & "%'"
        Using Command As New MySqlCommand(SQL, connection)
            Command.Parameters.AddWithValue("@stub", claim)
            Command.Parameters.AddWithValue("@claim", "CLAIMED")
            Command.CommandText = SQL
            connection.Open()
            count = Command.ExecuteScalar
        End Using
    End Using

    Return count
End Function

Private Sub addstub()

    Dim count As Integer = GetClaimCount(txtid.Text, txtclaim.Text)

    If count = 1 Then
        MsgBox("PROCESSING")
        ' ...
    Else
        MsgBox("ERROR")
    End If

End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM