Home > Enterprise >  How to connect to mysql with computer name in C#
How to connect to mysql with computer name in C#

Time:11-06

I want to connect to mysql with my computer name as host. I get this error :

unable to connect to any of the specified mysql hosts

When i change the host with localhost or 127.0.0.1. it's work fine.

I test the connection directly in mysql workbench with my computer name : COMP-18TN. MySQL Workbench could connect to that server. But not my c# code.

You find as bellow my code :

 string host = "COMP-18TN";
 string DatabaseName = "Cars_DB";
 string UserName ="root_name";
 string Password ="pass";
 string connString = string.Format("Server={0}; database={1}; UID={2}; password={3}",
                host, DatabaseName, UserName, Password);
            MySqlConnection conn = new MySqlConnection(connString);
            try
            {
                conn.Open();
                
            }
            catch (Exception e)
            {
                MessageBox.Show(e.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

CodePudding user response:

You can try adding an entry in your hosts file located in C:\Windows\System32\drivers\etc\hosts:

127.0.0.1 COMP-18TN

This way your computer name will resolve to the local IP which you know works.

  • Related