在测试我的程序时,当我尝试登录另一个用户时,弹出一个错误,说“连接太多”。
我需要修复它,以便连接不会自行超时,以便它能够完美运行。
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
感谢那些愿意回答的人。
答案 0 :(得分:0)
您可以使用1个连接执行多个命令/操作。
您可以尝试这样的事情:
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()
答案 1 :(得分:-1)
这个程序的数据库是什么?你可以在mysqlconnection的末尾添加;pooling=false
,如下所示
server=localhost;user=root;database=world;port=3306;password=******;pooling=false
并试一试
答案 2 :(得分:-1)
您可以在查询中找到许多查询。尝试尽可能删除。这是第一个例子。 你应该尽快关闭连接。
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
您可以做的最好的事情是将每个查询放在一个函数中。此外,参数化所有内容!
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