本文介绍了MySQL连接抛出null引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我的MySQL连接抛出null引用,尽管这个代码在一年前运行良好。 调试器指示异常的行包含 connection = new MySqlConnection(connectionString); :My MySQL connection throws null reference although this code worked well one year ago.The line where the debugger indicates the exception contains"connection = new MySqlConnection(connectionString);":DBConnect MySqlConnection = new DBConnect();string[] resultArray = MySqlConnection.GetValidUser( "tbl_user", tbEmail.Text, tbPassword.Text);//THROWS null reference exception in method 'private bool OpenConnection()'//A first chance exception of type 'System.ArgumentException' occurred in System.Data.dll这是我的DBConnect类:This is my DBConnect class:class DBConnect{ private MySqlConnection connection; private string server; private string database; private string uid; private string password; public DBConnect() { server = "x.x.x.x"; database = "..."; uid = "..."; password = "..."; string connectionString = "SERVER=" + server + ";" + "DATABASE=" + database + ";" + "UID=" + uid + ";" + "PASSWORD=" + password + ";"; connection = new MySqlConnection(connectionString); } private bool OpenConnection() { try { connection.Open(); return true; } catch (MySqlException ex) { switch (ex.Number) { case 0: MessageBox.Show("Cannot connect to MySQL server."); break; case 1045: MessageBox.Show("Invalid username or password."); break; } return false; } } public string[] GetValidUser(string dbTable, string dbUsername, string dbPassword) { string query = "SELECT id,email,password FROM " + dbTable + " WHERE email='" + dbUsername + "' AND password='" + dbPassword + "'"; string[] resultArray = new string[3]; if (this.OpenConnection() == true) { MySqlCommand cmd = new MySqlCommand(query, connection); MySqlDataReader dataReader = cmd.ExecuteReader(); while (dataReader.Read()) { resultArray[0] = dataReader.GetInt32(0).ToString(); resultArray[1] = dataReader.GetString(1); resultArray[2] = dataReader.GetString(2); } dataReader.Close(); this.CloseConnection(); } return resultArray; }}可以找到数据库类的原始代码此处。The original code for the database class can be found here.推荐答案其他事情,它听起来像你有连接字符串的问题 - 从注释:Among other things, it sounds like you have problems with the connection string - from comments: 而connection = new MySqlConnection(); connection.ConnectionString = connectionString;抛出一个异常语句connection = new MySqlConnection();不... ... While "connection = new MySqlConnection(); connection.ConnectionString = connectionString;" throws an exception the statement "connection = new MySqlConnection();" does not...这里的区别是:在后面的 em> - 所以它听起来像你的连接字符串不能正确转义的值(最有可能的密码);你可以尝试:The difference here is simply: in the latter you aren't setting the connection string - so it sounds like your connection string is not correctly escaping the values (most likely, the password); you could try:var cs = new DbConnectionStringBuilder();cs["SERVER"] = server;cs["DATABASE"] = database;cs["UID"] = uid;cs["PASSWORD"] = password;var connectionString = cs.ConnectionString; 这篇关于MySQL连接抛出null引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-28 11:40