I’m trying to write a function to check whether the connection details given on a Form are valid, so it returns True otherwise it returns False :
How can i achieve this ?
My code below :
private bool checkConnectionSuccess(string host, string user, string pass, string dbname) {
string strProvider = "Data Source=" + host + ";Database=" + dbname + ";User ID=" + user + ";Password=" + pass;
MySqlConnection myConn = new MySqlConnection(strProvider);
myConn.Open();
if (________) return true; else return false;
}
What should I do in order to Check for the connection wether it was succeded based on the given details (user, pass, dbname, host)
Thankyou
Use a try catch block:
This is possible because MySQLConnection will likely fail on most exceptions, including if the credentials are wrong or the connection string is invalid.