MySQL连接超时后命令执行错误 [英] Error on command execution after MySQL connection timeout

查看:146
本文介绍了MySQL连接超时后命令执行错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有一个问题,当我从连接到网络托管的MySQL数据库的Windows应用程序执行SQL命令时,收到以下错误消息.

命令执行期间遇到致命错误

我正在使用VS2010 Express,使用本地托管的db(可能是由于连接速度下降)时,这似乎不是问题.以下是失败的代码(具有数据库凭据被清空的测试应用程序).​​..

Hi,

I am having a problem where I am getting the following error message when executing a SQL command from a Windows App that is connecting to a web hosted MySQL database.

Fatal error encountered during command execution

I am using VS2010 Express, and it doesn''t seem to be a problem when using a locally hosted db (possibly down to connection speed). Below is the code that fails (a test app that has database credentials blanked out) ...

Public _dbconn As MySqlConnection
    Private Const _ConnStr As String = "server=xxxxxxxx; user id=xxx; password=xxx; database=xxxx; "

    Private Sub OpenConnection()
        If _dbconn Is Nothing OrElse Not _dbconn.State = ConnectionState.Open Then
           _dbconn = New MySql.Data.MySqlClient.MySqlConnection(_ConnStr)
           _dbconn.Open()
        End If
    End Sub


    Private Sub cmdGetData_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdGetData.Click
        Dim dr As MySqlDataReader
        Dim sql As String
        Try

            OpenConnection()
            sql = "SELECT * FROM familia"
            Using _dbcmd = New MySqlCommand(sql, _dbconn)
                dr = _dbcmd.ExecuteReader
                Do While dr.Read
                    ''do something here
                Loop
            End Using

        Catch ex As Exception
            MessageBox.Show(ex.ToString)
        Finally
            If Not dr Is Nothing Then
                dr.Close()
            End If
        End Try
    End Sub




第一次连接数据库时,它运行良好.然后等待几分钟后,我再次运行它,并在cmd.ExecuteReader行上收到上述错误.此后(因为现在已打开连接),它将正常工作直到下一次超时.

我认为这是由于互联网连接速度慢导致数据库连接速度慢所致,但是如果有人对我有任何建议或可以在上面的代码中看到错误,请帮忙!

提前多谢,

Greg




The first time I connect to the db it runs fine. Then after waiting a few minutes i run it again and i get the above error on the cmd.ExecuteReader line. After this (as the connection is now open) it will work fine until the next timeout.

I think this is being caused by a slow internet connection causing a slow connection to the database, but if anyone has any advice for me or can see a mistake in the above code then please help!

Thanks a lot in advance,

Greg

推荐答案

我认为您必须更改连接字符串 [您正在使用的 ^ ].也许更改超时时间或更改连接池的设置方式.
I think you have to change the Connection String[^] you are using. Perhaps change the timeout or change how connection pool is setup.


这篇关于MySQL连接超时后命令执行错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆