Stopbyte

How to make a connection to a MYSQL database in C#?

Hello everybody! I just created an application with the C# that is used to manage a MySQL database, I tested the connection, while it works well, but when connecting this exception launches:

A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified)

This is my source code:

try {
    string connectionString = "SERVER=127.0.0.1; DATABASE=mli; UID=root; PASSWORD=";
    SqlConnection cn = new SqlConnection(connectionString);
    cn.Open();
} catch (Exception ex)
{
    MessageBox.Show(ex.Message);
}

please, is there anyone who can help me

1 Like

I think the Error message you got, explains to you all that you have to know to start debugging the problem.

Here are some few tips to assist you in your debugging process:

  • Check that MySQL Server is running on the localhost 127.0.0.1.

  • Check that the MySQL Instance service is running in your Local computer.

  • Mainly the best method to do that, is using the tools provided by MySql itself, such as MySQL Workbench, if the Connection doesn’t work there then it wont be working anywhere else.

  • Finally; I don’t think it’s a password, Database or User ID mistake, since the error you are getting is exclusively Server instance related.

Hope that those tips helps.