First I'd like to precise that I've been searching on the internet for a solution to my problem and all I find is the classic
if(connection.open) return true;
solution that is in my case not working for my needs.
I'm working on an application. Before it runs I need to check connectivity to the MySQL database, if the connection isn't done, a new window is opened prompting the user for connection settings(username, server address, password...). In this window I have a test connection button to see if the connection is established, this is the code of the button_click event :
private void Button_Click_1(object sender, RoutedEventArgs e)
{
ApplicationSettings applicationSettings = new ApplicationSettings();
applicationSettings.ServerDatabase = tbdbName.Text;
applicationSettings.ServerIp = tbServer.Text;
applicationSettings.ServerUserName = tbUsername.Text;
//applicationSettings.ServerPassword = pbPassword.SecurePassword;
applicationSettings.MakeConnectionString();
MySqlConnection connection = new MySqlConnection(applicationSettings.ConnectionString);
try
{
connection.Open();
MessageBox.Show(this, "connection string: "+applicationSettings.ConnectionString+"connection OK!", "OK !", MessageBoxButton.OK, MessageBoxImage.Information);
}
catch (Exception ee)
{
MessageBox.Show(this, "connection string : "+applicationSettings.ConnectionString+"error : " + ee.Message, "connection ERROOOOR", MessageBoxButton.OK,
MessageBoxImage.Error);
}
finally
{
if(connection.State == ConnectionState.Open)
connection.Close();
}
}
If I enter only the server address "applicationSettings.ServerIp" and click the test connection button, the messageBox OK is shown and the connection string is :
Server=127.0.0.1;Database=;Password=;
I guess this is perfectly logical, but I need to test if the connection is established to the database, also if I fill the login text box with any random value, the connection is established.
question : How can I use this to test if the connection is established with the database ?