真的很奇怪我只是在所有情况下都使用默认连接,这是我第一次收到这个错误,试图修复它并在网上没有成功地找到解决方案,这是第一次。我有以下代码:
class DbConnect
{
//get values of name and customerid using the bracelet id ( a equijoin using customer, ticketpurchase and ticket)
//every ticket is assigned a bracelet id - and i have 6 bracelets to add so also 6 dummy profiles in database
//method to look for customerid using bracelet id and return only one string
public int CustomerId(string braceletId)
{
String str = @"server=localhost;database=dbi340001;userid=xxxxxxxxxxx;password=xxxxxxxxxxxxxxx;";
MySqlConnection con = new MySqlConnection(str);
try
{
con.Open(); //open the connection
MessageBox.Show("welcome");
return 1;
}
catch (MySqlException err) //We will capture and display any MySql errors that will occur
{
Console.WriteLine("Error: " + err.ToString());
}
finally
{
if (con != null)
{
con.Close(); //safely close the connection
}
} //remember to safely close the connection after accessing the database
return 0;
这是一个非常简单的代码,我从另一个类调用了这个代码,我只是编写了代码以查看是否会使用默认代码进行连接。错误是conn.isPasswordExpired和conn.ServerThread和conn.ServerVersion上的null引用异常,它们都是仅从mysqlconnection对象获取的参数,因此它实际上没有任何意义。
任何帮助,将不胜感激!
最佳答案
在con.IsPasswordExpired;
之后立即调用MySqlConnection con = null;
显然,当您呼叫con
时null
仍然是con.IsPasswordExpired;
您应该考虑在con = new MySqlConnection(str);
之后将该语句移动到任何地方