MySql/Odbc 连接问题

发布于 2024-08-12 15:29:44 字数 673 浏览 2 评论 0原文

我正在通过 ODBC 访问 MySql 数据库。它通常工作正常,但如果数据库停止并重新启动,我必须重新启动我的应用程序才能重新连接到数据库。

访问数据库的代码如下:

        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();

重新启动数据库后,我在 dataAdapter.Fill 方法中收到“MySql 服务器已消失”异常。是 当我检测到连接已断开时,有什么方法可以重新连接到数据库?

我使用VS2008和MySql 5.1.30。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

狼亦尘 2024-08-19 15:29:44

这种“重新启动应用程序以重新连接到数据库”简直是一个糟糕的设计决策。数据库连接的初始化不应放在启动代码中,而应放在单独的过程中,如果您的方法之一出现异常“MySql 服务器已消失”,只需调用此过程即可重新初始化与数据库的连接。

This "restart the application in order to reconnect to the database" is simply a bad design decision. The initialization of the database connection should not be put in the startup code, instead put it in a separate procedure, and in case one of your methods got the exception "MySql server has gone away" simply call this procedure in order to re-initialize the connection to the DB.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文