以下代码在cmd上调用ExecuteNonQuery()的行上抛出以下异常。
System.InvalidOperationException: Connection must be valid and open.
at MySql.Data.MySqlClient.ExceptionInterceptor.Throw(Exception exception)
at MySql.Data.MySqlClient.MySqlCommand.Throw(Exception ex)
at MySql.Data.MySqlClient.MySqlCommand.CheckState()
at MySql.Data.MySqlClient.MySqlCommand.ExecuteReader(CommandBehavior behavior)
at MySql.Data.MySqlClient.MySqlCommand.ExecuteNonQuery()
at Oasis.AlertManager.DB.MySQL.Insert(MySqlCommand cmd) in c:\BuildAgent\work\8b0cd51e92588e6d\Oasis\Oasis.AlertManager.DB\MySQL.cs:line 471
public int Insert(MySqlCommand cmd)
{
int rows;
using (masterConnection = new MySqlConnection(masterAlertConnectionStr))
{
masterConnection.Open();
cmd.Connection = masterConnection;
rows = cmd.ExecuteNonQuery();
}
return rows;
}
我很困惑,因为在masterConnection上调用了Open()。谁看过这个吗?它可能与命令有关,还是没有在某处设置的超时?