与c#on .isPasswordExpired和服务器线程,版本

时间:2016-05-22 17:56:28

标签: c# mysql database nullreferenceexception

真的很奇怪。我只是使用默认连接一切,这是我第一次得到这个错误已经尝试了几个小时来修复它并在线找到解决方案没有成功。我有这段代码:

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 adn conn.ServerVersion上的空引用异常,这些参数只有来自mysqlconnection对象的所有参数,因此它确实没有任何意义。 任何帮助将不胜感激!

2 个答案:

答案 0 :(得分:0)

con.IsPasswordExpired;

之后立即调用

MySqlConnection con = null;

显然,当您致电con时,null仍为con.IsPasswordExpired;

您应该考虑在con = new MySqlConnection(str);

之后的任何地方移动该声明

答案 1 :(得分:0)

你应该试试

MySqlConnection con = new MySqlConnection();

因此它会初始化对象con,然后它不会是null