MySQL to C# Connection Error

Mohamed Rafi N 106 Reputation points
2022-03-19T04:18:03.63+00:00

184791-image.png

Developer technologies | Windows Forms
Azure Database for MySQL
SQL Server | Other
0 comments No comments
{count} votes

Answer accepted by question author
  1. Anurag Sharma 17,636 Reputation points
    2022-03-19T07:22:54.527+00:00

    Hi @Mohamed Rafi N , welcome to Microsoft Q&A forum.

    As I understand you want to connect to on-premise MySQL Database through C# application.

    Firstly before we make code change I would request you to check to connect to the MySQL Server using MySQL workbench or any other tool and notice the connection string parameters mentioned in your code works.

    Now, you are using the SQLClient classes to connect to MySQL Database. You need to install MySQL Client. You can add the NuGet Package by going through:

    Tools -> NuGet Package Manager -> Manage NuGet Package for Solution and search for 'MySql.Data.MySqlClient' as shown in below screenshot:

    184772-image.png

    After installing it, please add the reference of it at the top using:

    using MySql.Data.MySqlClient;  
    

    Then we need to make changes to code as mentioned below:

    string connectionString;  
                MySqlConnection cnn;  
                connectionString = "server=localhost;database=test;uid=root;pwd=pwwdtest;";  
                cnn = new MySqlConnection(connectionString);  
                cnn.Open();  
    

    Note the format of connection string and try using it same way.

    Please let us know if this works with you else we can discuss further on the same.

    ----------

    If answer is helpful please please click on 184680-image.png as it could help other members of the Microsoft Q&A community who have similar questions and are looking for solutions. Thank you for helping to improve Microsoft Q&A!


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.