简体   繁体   中英

How to connect to mysql with computer name in C#

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);
            }

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM