我使用:連接到MySQL數據庫與C#
if (connection.State != ConnectionState.Open)
{
OpenConnection();
}
和
private bool OpenConnection()
{
try
{
connection.Open();
return true;
}
catch (MySqlException ex)
{
//When handling errors, you can your application's response based
//on the error number.
//The two most common error numbers when connecting are as follows:
//0: Cannot connect to server.
//1045: Invalid user name and/or password.
switch (ex.Number)
{
case 0:
Console.WriteLine("Cannot connect to server. Contact administrator");
Console.Read();
break;
case 1045:
Console.WriteLine("Invalid username/password, please try again");
Console.Read();
break;
}
return false;
}
}
我總是System.InvalidOperationException: The connection is already open
。它沒有意義,因爲我檢查它是否已經打開。
在此先感謝。
仍然得到相同的錯誤..... – user1335122 2012-04-15 22:02:02
@ user1335122打印if語句之前的連接狀態。這可能會給你一些有關國家的信息。 – Sandeep 2012-04-16 01:21:08