MySql / Odbc connection problem

Posted by Ingvald on Stack Overflow See other posts from Stack Overflow or by Ingvald
Published on 2009-11-24T07:23:02Z Indexed on 2010/06/17 10:03 UTC
Read the original article Hit count: 221

Filed under:
|
|

I'm accessing a MySql database via ODBC. It normally works fine, but if the database is stopped and restarted I have to restart my application in order to reconnect to the database.

The code for accessing the database is like this:

        OdbcConnection connection = new OdbcConnection(connectString);
        OdbcCommand command = connection.CreateCommand();
        command.CommandType = CommandType.Text;
        command.CommandText = "select * from cds";
        OdbcDataAdapter dataAdapter = new OdbcDataAdapter(command);
        DataSet dataSet = new DataSet();
        connection.Open();
        dataAdapter.Fill(dataSet);
        connection.Close();

After a restart of the database, I get a 'MySql server has gone away' exception in dataAdapter.Fill method. Is there any way I can reconnect to the database when I detect that the connection has broken?

I use VS2008 and MySql 5.1.30.

© Stack Overflow or respective owner

Related posts about c#

Related posts about mysql