How to verify that you are connected to a MySQL database in C#?

后端 未结 2 1333
再見小時候
再見小時候 2021-01-23 18:53

I added the reference for the MySQL server in C#. I thought I have this code right. I know the connection values are right for testing. Here is my error:

{\"A network

2条回答
  •  花落未央
    2021-01-23 19:22

    You can do:

    using(SqlConnection connection = new MySqlConnection(connectionString))
    {
        try
        {
            conn.Open();
        }
        catch(SqlException ex)
        {
            switch(ex.Number) 
            { 
                case 18456: // Can't login
                    // Do something
                    break;
                default:
                    break;
            } 
        }
    }
    

    To get the complete list of codes run:

    SELECT * FROM master.dbo.sysmessages
    

提交回复
热议问题