问题是:如果我在计时器中每 100 毫秒打开一次数据库连接,有没有问题?我认为最好的方法是打开一次连接,然后在需要的所有时间进行查询。右?
我的代码是:
Friend conexion As MySqlConnection
Private cmd As New MySqlCommand
Private dr As MySqlDataReader
Private ConnectionString = "server=localhost; uid=root; pwd=; database=myblahdb;"
conexion = New MySqlConnection()
conexion.ConnectionString = ConnectionString
conexion.Open()
Dim cmd As New MySqlCommand
With cmd
.CommandText = "select blah blah"
.CommandType = CommandType.Text
.Connection = conexion
End With
dr = cmd.ExecuteReader
If dr.HasRows Then
dr.Read()
//do stuff
End If
cmd.Dispose()
dr.Close()
dr.Dispose()
conexion.Close()
conexion.Dispose()
将其
移动到form_load以便在开始时执行
conexion = New MySqlConnection()
conexion.ConnectionString = ConnectionString
conexion.Open()
并在退出时执行此操作
conexion.Close()
conexion.Dispose()
是解决方案