MySQL to C# Connection Error

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

184791-image.png

Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,836 questions
Azure Database for MySQL
Azure Database for MySQL
An Azure managed MySQL database service for app development and deployment.
715 questions
SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
12,772 questions
0 comments No comments
{count} votes

Accepted answer
  1. Anurag Sharma 17,571 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