c# - MySQLException when trying to connect to a database in visual studio: Cannot connect to Server. Contact Administrator -


i try connect mysql database in windows application program, keep coming across mysqlexception, goes case 0 in open connection. , should right. i'm wrong name of server should localhost. check if server name right , if is, else causing problem?

using mysql.data.mysqlclient;  using system; using system.collections.generic; using system.componentmodel; using system.data; using system.drawing; using system.linq; using system.text; using system.threading.tasks; using system.windows.forms;  namespace plantsclientserverapp {     public partial class allplants : form     {         private string server;         private string database;         private string uid;         private string password;         private mysqlconnection connection;         private mysqldataadapter mysqldataadapter;          public allplants()         {             initializecomponent();         }          private void exittoolstripmenuitem_click(object sender, eventargs e)         {             this.hide();         }          private void allplants_load(object sender, eventargs e)         {             server = "localhost";             database = "plants only";             uid = "root";             password = "k1326629ktcj5423";             string connectionstring;             connectionstring = "server=" + server + ";" + "database=" + database + ";" + "uid=" + uid + ";" + "password=" + password + ";";              connection = new mysqlconnection(connectionstring);              if (this.openconnection() == true)             {                 mysqldataadapter = new mysqldataadapter("select * plants_database2", connection);                 dataset ds = new dataset();                 mysqldataadapter.fill(ds);                 datagridview1.datasource = ds.tables[0];                  //close connection                 this.closeconnection();             }           }           private bool openconnection()         {             try             {                 connection.open();                 return true;             }             catch (mysqlexception ex)             {                 //when handling errors, can application's response based on error number.                 //the 2 common error numbers when connecting follows:                 //0: cannot connect server.                 //1045: invalid user name and/or password.                 switch (ex.number)                 {                     case 0:                         messagebox.show("cannot connect server. contact administrator");                         break;                     case 1045:                         messagebox.show("invalid username/password, please try again");                         break;                     default:                         messagebox.show(ex.message);                         break;                 }                 return false;             }         }          private bool closeconnection()         {             try             {                 connection.close();                 return true;             }             catch (mysqlexception ex)             {                 messagebox.show(ex.message);                 return false;             }         }     } } 


Comments