//Tests the given connection string to see whether a connection can be made with the SQL Server. public static async Task <bool> TestConnection(string connectionString, bool silent = false, bool popup = false) { //Gets the server name from the connection string, but only if the data source is at the start of the string. string serverName = connectionString.Split(';')[0].Split('=')[1].Trim(); try { using (SqlConnection conn = new SqlConnection(connectionString)) { await conn.OpenAsync(); if (!silent) { if (!popup) { FormControls.OutputToApp($"Connection to \"{serverName}\" successful!"); } else { MessageBox.Show($"Connection to \"{serverName}\" successful!"); } } FormControls.ToggleAllElements(true); return(true); } } catch (Exception e) { MessageBox.Show($"Connection to \"{serverName}\" failed. {e.Message}"); FormControls.ToggleAllElements(true); FormControls.ToggleStartButton(true); FormControls.ClearOutputToApp(); return(false); } }
//Used to export the database backup from LIVE to a local folder. public async Task <bool> ExportDatabases(List <string> databases, string connectionString) { string folderPath = $"{_backupFolderPath}{DateTime.Now.ToString("dd-MM-yyyy")}\\"; FormControls.ToggleAllElements(false); Directory.CreateDirectory(folderPath); for (int i = 0; i < databases.Count; i++) { if (!await SQLPackage.Run("Export", databases[i], connectionString, folderPath)) { FormControls.ClearOutputToApp(); return(false); } } FormControls.OutputToApp("------------------"); FormControls.OutputToApp("Finished Exporting"); return(true); }