2017-09-28 519 views
0

VS 2017用户在这里。我试图将C#控制台应用程序连接到MySQL 5.7。但是没有建立连接。一个建议的解决方案是查看未声明的对象,但我不认为这是这种情况。请告知是否。MySQL C#连接到localhost:'System.NullReferenceException'

MySqlConnection conn; 
string connStr = ConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString; 

public Connection() 
{ 
    conn = new MySqlConnection(connStr); 
} 

<connectionStrings> 
<add name="myConnectionString" 
    connectionString="server=localhostdatabase=store;user=root; 
    password=pass;port=3306" /> 
</connectionStrings> 

如果我在调试器中运行此我得到conn后续信息:

IsPasswordExpired' threw an exception of type 'System.NullReferenceException' 
ServerThread' threw an exception of type 'System.NullReferenceException' 
ServerVersion' threw an exception of type 'System.NullReferenceException' 

有(我很少要在此发现,只是在设置密码一年前不真的相信会导致这个问题),还有一些人通过重新安装MySQL服务器来解决这个问题。我这样做,无济于事。

从命令行一切工作正常。

+1

可能重复[什么是NullReferenceException,以及如何解决它?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-doi-i-fix - ) – Progman

+0

对不起,第二个公共连接()不应该在那里 – ADBF

+1

感谢progman的参考,但原因表明,通常有对象之前没有宣布使用,我不认为这是在这里的情况。 – ADBF

回答

0

如果您确认您的密码已过期,您可以尝试创建或更改:

CREATE USER 'jeffrey'@'localhost' PASSWORD EXPIRE NEVER; 
ALTER USER 'jeffrey'@'localhost' PASSWORD EXPIRE NEVER; 

你的向导是here

+0

感谢红人花时间回答我。请看我上面的评论。现在将在我的石头下爬回来。 – ADBF