我有这个方法:
static void DbConnect()
{
// Connects to the Db using a simplified Connection string.
try
{
mySqlConnect = new MySqlConnection("server=" + Setup_Controller.server + ";uid=root;");
mySqlConnect.Open();
}
// If a password is required, tries again using the password provided.
catch (MySqlException)
{
mySqlConnect = new MySqlConnection("server=" + Setup_Controller.server + ";uid=root;password='" + Setup_Controller.password + "';");
mySqlConnect.Open();
}
}
但是每次捕获异常时,由于第一次连接尝试导致的当前异常,它不会加载打开连接。我开始怀疑它是否因为try / catch不允许程序继续,因为游戏中有未处理的异常?
答案 0 :(得分:0)
如果您真的想使用此方法,则需要先关闭连接,然后才能再次使用。
static void DbConnect()
{
// Connects to the Db using a simplified Connection string.
try
{
mySqlConnect = new MySqlConnection("server=" + Setup_Controller.server +
";uid=root;");
mySqlConnect.Open();
}
// If a password is required, tries again using the password provided.
catch (MySqlException)
{
//make sure connection is close
if(mySqlConnection != null)
mySqlConnect.Close();
}
/* i don't know what this class looks like but most data class expose some method
to see if the connection is still open or close. If there is not an IsOpen check
for IsClose or something like this
*/
if(!mySqlConnect.IsOpen())
{
try
{
mySqlConnect = new MySqlConnection("server=" + Setup_Controller.server +
";uid=root;password='" + Setup_Controller.password + "';");
mySqlConnect.Open();
}
catch (MySqlException)
{
//make sure connection is close
mySqlConnect.Close();
}
}