C#MySql连接异常 [英] C# MySql Connection Exception

查看:89
本文介绍了C#MySql连接异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用WPF C#和mysql来完成我的项目,我有捕获异常问题...请参考下面的代码了解更多信息,当我的数据库服务器未启动时,它将捕获我在此定义的两个异常但是,最后会有消息说程序没有响应,Windows会检查错误。我怎样才能优雅地结束程序?我已经尝试了几种类型的异常,两个catch(异常, MySqlException InvalidOperationException )..任何关于它的想法, 请帮忙。!!谢谢



I am using WPF C# with mysql to do my project, I have the catching exception problem... please refer to code below for further info, when my db server is not up, it will catch both exceptions that I define here, but, in the end a message will come saying that program is not responding, Windows will check for error. How can i end the program gracefully? I have tried for few types of exception for both catch (Exception, MySqlException and InvalidOperationException)..any idea about it, please help.!! Thanks

try
{
    conn = new MySqlConnection(connString);
    conn.Open();
}
catch (MySqlException e)
{
    Console.WriteLine(e.toString());
}

finally
{
    if (conn != null || conn.State == System.Data.ConnectionState.Open)
        conn.Close();
}

try
{
    cmd = conn.CreateCommand();
    cmd.CommandText = sqlQuery;
    reader = cmd.ExecuteReader();
}

catch (InvalidOperationException e)
{
      Console.WriteLine(e.toString());
}

finally
{
    if(!reader.IsClosed || reader != null)
    reader.Close();
}

推荐答案

嗯...通知你,



DataReader 需要使用活动的Open连接。如果在finally块中调用conn.Close然后使用相同的连接对象尝试 cmd.ExecuteReader ,它最终将失败。



所以在关闭 DataReader 对象之前不要关闭它。

:很酷:
Well... To inform you,

DataReader requires an active Open connection to work with. If you invoke conn.Close in the finally block and then try cmd.ExecuteReader using the same connection object, it will eventually fail.

So dont close it until you close the DataReader object.
:cool:


我正在使用mysqlconnector net 5.2.7。它能够在.NET中连接mysql,然后我将我的代码更改为如下,但仍然失败。当结束时,它会提示一条消息,程序没有响应,Windows会检查错误。



试试

{



conn = new MySqlConnection(connString);

conn.Open();



}

catch(MySqlException e)

{

Console.WriteLine(e.toString());

}





试试

{



cmd = conn.CreateCommand();

cmd.CommandText = sqlQuery;

reader = cmd.ExecuteReader();



}



catch(InvalidOperationException e)

{

Console.WriteLine(e。 toString());

}





终于

{

if(!reader.IsClosed ||读者!= null)

reader.Close();



if(conn!= null || conn.State == System。 Data.ConnectionState.Open)

conn.Close();



}
I am using mysqlconnector net 5.2.7. It able to connect mysql in .NET, then i have change my code as following, but still fail. When end up, it will prompt a message, program not responding, Windows will check for error.

try
{

conn = new MySqlConnection(connString);
conn.Open();

}
catch (MySqlException e)
{
Console.WriteLine(e.toString());
}


try
{

cmd = conn.CreateCommand();
cmd.CommandText = sqlQuery;
reader = cmd.ExecuteReader();

}

catch (InvalidOperationException e)
{
Console.WriteLine(e.toString());
}


finally
{
if(!reader.IsClosed || reader != null)
reader.Close();

if (conn != null || conn.State == System.Data.ConnectionState.Open)
conn.Close();

}


我认为我的问题是..



在finally之后,我仍然有一些使用阅读器和连接的声明,以便发生错误。分开后,没关系。然后我发现有人认为,即使捕获不执行,它也会终结也正确吗?
I think wat my problem is..

after "finally", i still have some statement which is using reader and connection, so that the error is occur. After separate it, it's fine. And then i found one think, even the catch is not execute, it will exeute "finally" also right?


这篇关于C#MySql连接异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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