Click here to Skip to main content
15,908,661 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
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
Posted
Updated 31-May-11 13:25pm
v2

1 solution

I think you have to change the Connection String[^] you are using. Perhaps change the timeout or change how connection pool is setup.
 
Share this answer
 
Comments
Simon_Whale 6-Jun-11 4:52am    
Nice answer
Kim Togo 6-Jun-11 4:58am    
Thanks Simon

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900