hi all ! I have the following code that returns a bool value. If there is no connection it should be false. The problem is that I always get the exception and get out of the program. Is there a way to avoid it?
- private static bool IsMariaDBServer(string computerName)
- {
- Debug.WriteLine($"IsMariaDBServer on: {computerName}");
- string connectionString = $"Server={computerName};Database={database};User Id={userId};Password={password};License Key={License};charset={Charset}";
- try
- {
- using (MySqlConnection conn = new MySqlConnection(connectionString))
- {
- conn.Open(); // Attempt to open a connection
- using (MySqlCommand cmd = new MySqlCommand("SELECT VERSION()", conn))
- {
- string version = cmd.ExecuteScalar()?.ToString();
- Console.WriteLine($"Connected to MariaDB on {computerName}, Version: {version}");
- }
- }
- return true; // Connection succeeded
- }
- catch
- {
- // Suppress the exception and log it if needed for debugging purposes
- Console.WriteLine($"Connection failed for {computerName}");
- return false; // Return false for any connection failure
- }
- }
Any help is appreciated.