mysql连接超时命令,MySQL连接超时后命令执行出错

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

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值