【发布时间】:2021-04-11 03:18:42
【问题描述】:
这是我第一次来这里,因为这个问题我要疯了:
我正在使用多种类型的连接(mysql、odbc 和 SQL 服务器)开发一个带有 VB.NET 的 Windows 窗体应用程序,在我进入 MySQL 之前一切正常。 MySQL服务器是一个物理的windows 7 pc,我通过IPSEC VPN TUNNEL连接到它。
我需要每 x 秒执行 2 个 MySQL 连接,如果我在第一个连接后得到某种类型的结果,那么我将打开第二个连接,以此类推每 x 秒(这都写在我的 timer.tick 事件中处理程序)。 问题是 MySQL 服务器上的一些连接经常在 MySQL 服务器上保持活动状态(已建立),我不知道为什么......代码看起来很好,在正确的时间声明了打开和关闭方法,我我也尝试过 Dispose、ClearPool 和 ClearAllPools 方法,但我一直保持这些连接,直到我关闭程序或达到连接限制。
代码如下:
类连接:
Public Sub connMySQL()
Dim connstring As String
Try
If stabilimento = "1PR" Then
If cesoia = "" Then
connstring = "server=192.168.123.18;userid=xx;password=xx;database=xx;Connect Timeout=30"
Else
connstring = "server=192.168.123.253;userid=xx;password=xx;database=xx;Connect Timeout=30"
End If
End If
If stabilimento = "2PR" Then
If cesoia = "" Then
connstring = "server=192.168.1.18;userid=xx;password=xx;database=xx;Connect Timeout=30"
Else
connstring = "server=192.168.123.253;userid=root;password=xx;database=xx;Connect Timeout=30"
End If
End If
conMySql = New MySqlConnection(connstring)
If conMySql.State = ConnectionState.Closed Then
conMySql.Open()
End If
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub
执行迭代的类:
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
connMySQL()
comm = New MySqlCommand("SELECT count_1,count_2,start_stop,data_ora,id FROM plc_contatori where plc_nome='" + plc_nome + "' and data_ora > '" + data_ieri.ToString("yyyy/MM/dd") + "' order by data_ora desc limit 1", conMySql)
dr = comm.ExecuteReader()
While (dr.Read())
count_1(0) = dr.GetValue(0)
start_stop(0) = dr.GetValue(2)
data_ora(0) = dr.GetValue(3)
If id <> dr.GetValue(4) And count_2(0) <> dr.GetValue(1) Then
id = dr.GetValue(4)
count_2(0) = dr.GetValue(1)
Else
Exit Sub
End If
End While
dr.Close()
dr.Dispose()
conMySql.Close()
conMySql.Dispose()
conMySql.ClearPool(conMySql)
conMySql.ClearAllPools()
If Not conMySql Is Nothing Then conMySql = Nothing
comm.Dispose()
If start_stop(0) = 1 Then
Exit Sub
End If
Dim dum_count_2 As Integer = count_2(0) - 1
connMySQL()
comm = New MySqlCommand("select count_1,count_2,start_stop,data_ora from plc_contatori where plc_nome='" + plc_nome + "' and data_ora > '" + data_ieri.ToString("yyyy/MM/dd") + "' AND count_2=" + dum_count_2.ToString + " ORDER BY data_ora desc limit 1", conMySql)
dr = comm.ExecuteReader()
While (dr.Read())
count_1(1) = dr.GetValue(0)
count_2(1) = dr.GetValue(1)
start_stop(1) = dr.GetValue(2)
data_ora(1) = dr.GetValue(3)
End While
dr.Close()
dr.Dispose()
conMySql.Close()
conMySql.Dispose()
conMySql.ClearPool(conMySql)
conMySql.ClearAllPools()
If Not conMySql Is Nothing Then conMySql = Nothing
comm.Dispose()
If count_1(0) = count_1(1) And start_stop(1) <> 1 And count_2(0) <> count_2(1) Then
'sub that reads some values from an odbc connection
CheckFermo()
End If
End Sub
请注意,我没有在这部分代码中声明的变量是在表单的公共类中声明的。
我想知道可能出了什么问题...也许第二个连接是在服务器关闭第一个连接之前建立的?
【问题讨论】:
-
我不会在字段
conMySql中设置连接的子程序,而是让connMySQL成为一个返回MySqlConnection的函数。在Timer1_Tick中使用它在using块中,如Using con as MySqlConnection = connMySQL() .... End Using。这应该注意正确关闭和处理连接 -
@AlexB。我应该将两个数据读取器放在一个 using 中还是将它们与 2 using 分开?
-
任何选项都可以,但我只会使用一个连接
-
我发现 data_ora 有问题。这是一个日期字段吗?是在数据库中输入日期吗?如果不是,所有字符串是否都以 > 符号可以使用的格式存储?我认为日期应该存储为日期。
标签: mysql vb.net visual-studio connection mysqlconnection