How to make connection to MySQL database in C#?

mysql

(Tas Dever) #1

Hello everybody!
I just created an application with C# that is used to manage 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)

that 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


(Yassine) #2

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.


(ivan.norin) #3

You must check File Hosts (C:\Windows\System32\drivers\etc\hosts)
and Fix problem with string 127.0.0.1
it must be looks like
127.0.0.1 localhost
or
#127.0.0.1 localhost