简体   繁体   English

C#connection.Open()异常

[英]C# connection.Open() exception

I am trying to connect to my database on localhost but for some reason, it throws an exception, and I can't find out why and where. 我试图连接到我的数据库localhost但由于某种原因,它抛出一个异常,我无法找出原因和地点。

private void Initialize()
    {
        server = "localhost";
        database = "dbname";
        uid = "uname";
        password = "pass";
        string connectionString;
        connectionString = "SERVER=" + server + ";" + "DATABASE=" +
        database + ";" + "UID=" + uid + ";" + "PASSWORD=" + password + ";";

        connection = new SqlConnection(connectionString);
    }

This is my initialize function; 这是我的初始化函数; my problem is with: 我的问题是:

    private bool OpenConnection()
    {
        try
        {
            connection.Open();
            return true;
        }
             ...

When I debug the program, it stays on "connection.Open();" 当我调试程序时,它保持在“connection.Open();” line for a while, then I see "Exception thrown: 'System.Data.SqlClient.SqlException' in System.Data.dll" on console. 行了一段时间,然后我在控制台上看到“System.Data.dll中的异常抛出:'System.Data.SqlClient.SqlException'”。 But it's not crashing, just skipping. 但它不会崩溃,只是跳过。

If you want to open a connection with MySql, then you don't use the classes from System.Data.SqlClient namespace but with the classes from MySql.Data.MySqlClient 如果要打开与MySql的连接,则不要使用System.Data.SqlClient命名空间中的类,而是使用MySql.Data.MySqlClient的类

These classes are MySqlConnection , MySqlCommand , MySqlDataReader etc... 这些类是MySqlConnectionMySqlCommandMySqlDataReader等......
If you try to use the SqlConnection class you receive this error if the database to open is MySql because it can only work with the Sql Server, Sql Server Express or LocalDB database systems. 如果您尝试使用SqlConnection类,则如果要打开的数据库是MySql,则会收到此错误,因为它只能与Sql Server,Sql Server Express或LocalDB数据库系统一起使用。

connection = new MySqlConnection(connectionString);

Of course, to use the MySql.Data.MySqlClient namespace you need to download and install the MySql NET Connector and then add a reference into your project to the assembly MySql.Data.dll (and add the using MySql.Data.MySqlClient to the top of each cs file that needs to use these classes) 当然,要使用MySql.Data.MySqlClient命名空间,您需要下载并安装MySql NET Connector ,然后项目中的引用添加到程序集MySql.Data.dll(并将使用MySql.Data.MySqlClient添加到需要使用这些类的每个cs文件的顶部)

It is not crashing because you are opening the connection inside of a try-catch block, so the program thinks you are handling the exception. 它没有崩溃,因为你在try-catch块中打开连接,所以程序认为你正在处理异常。 If you want it to crash, get rid of the try-catch block. 如果你想让它崩溃,摆脱try-catch块。 (This would be a bad user experience.) (这将是一个糟糕的用户体验。)

Alternatively, after your try-catch, you could check the connection is valid and then do something if it isn't. 或者,在try-catch之后,您可以检查连接是否有效,然后执行某些操作(如果不是)。

if ((connection == null) || (connection.State != ConnectionState.Open)) { 
    /* do something here to indicate to the user there was an issue.
}

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

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