简体   繁体   English

C#MySQL连接问题

[英]C# MySQL Connection problems

I'm trying to connect a C# application (using Visual C# 2008 Express Edition) to a remote MySQL server. 我正在尝试将C#应用程序(使用Visual C#2008 Express Edition)连接到远程MySQL服务器。 I have the drivers for this, but when I followed the tutorials (including adding the pooling and connection reset properties), I get an error that: Object reference not set to an instance of an object. 我有用于此的驱动程序,但是当我按照教程进行(包括添加池化和连接重置属性)时,出现以下错误: 对象引用未设置为对象的实例。 I've included the two lines of code that should be making a connection. 我已经包括了应该建立连接的两行代码。 The error is thrown on the second line. 该错误将引发第二行。

        MySqlConnection connect = new MySqlConnection("database=d*******;Data Source=mysql.netfirms.com;user id=*******;pwd=*****;pooling=false;connection reset=false");
        connect.Open();

I'd try setting the connection string outside of the constructor to help narrow down the issue: 我会尝试在构造函数外部设置连接字符串,以帮助缩小问题范围:

MySqlConnection connect = new MySqlConnection();
//Do you get the null exception in this next line?
connect.ConnectionString = "your conn string here";
connect.Open(); //-> If you get the exception here then the problem is with the connection string and not the MySqlConnection constructor.

If you do get the exception in the connect.ConnectionString = ... line, then the problem is with the driver and sounds like you need to reinstall it. 如果确实在connect.ConnectionString = ...行中获得了异常,则问题出在驱动程序,听起来您需要重新安装它。

I would also try a simpler connection string, without the pooling and reset keys. 我还将尝试一个更简单的连接字符串,而不使用池键和重置键。

Can you post more code? 您可以发布更多代码吗? The exception line is probably a bit off due to compiler optimization or related. 由于编译器优化或相关原因,异常行可能有点偏离。 Constructors must return an object or throw an exception. 构造函数必须返回一个对象或引发异常。 It is impossible to say 不可能说

MyType item = new MyType();
Debug.Fail(item == null); // will never fail.

The null reference is probably on the line just above your instantiation. 空引用可能位于实例化上方的行上。

可能与错误有关吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM