/*@" * SELECT * [SOADB].[dbo].[Local_SSI_ErrorLogDetail].[OBJECT_NAME], * [SOADB].[dbo].[Local_SSI_ErrorLogDetail].[Error_Section], * [SOADB].[dbo].[Local_SSI_ErrorLogDetail].[ERROR_MESSAGE], * [SOADB].[dbo].[Local_SSI_ErrorLogDetail].[TimeStamp], * [SOADB].[dbo].[Local_SSI_ErrorSeverityLevel].[Severity_Level_Desc] * FROM[SOADB].[dbo].[Local_SSI_ErrorLogDetail] * WITH (NOLOCK) * INNER JOIN [SOADB].[dbo].[Local_SSI_ErrorSeverityLevel] * ON[SOADB].[dbo].[Local_SSI_ErrorSeverityLevel].Severity_Level_Id = [SOADB].[dbo].[Local_SSI_ErrorLogDetail].Error_Severity_Level * ORDER BY [SOADB].[dbo].[Local_SSI_ErrorLogDetail].[TimeStamp]";*/ #endregion #region Public methods public static async Task <ServiceOperationResult> LoadLogEntriesIntoSourceCache(Database database, SourceCache <LogEntry, string> logEntriesSourceCache) { var serviceOperationHelper = new ServiceOperationHelper(typeof(LogEntry), Plurality.Plural, ServiceOperation.Load, $"{typeof(Database).Name} {database.Name}"); await Task.Run(() => { try { serviceOperationHelper.LogServiceOperation(ServiceOperationStatus.Attempting); var numberOfOldLogEntries = logEntriesSourceCache.Keys.Where(k => k.Contains(database.Identifier)).Count(); var addDatabaseInfo = new AddDatabaseInfo(database.Name); var sqlConnection = new SqlConnection(addDatabaseInfo.ToConnectionString()); sqlConnection.Open(); using (SqlCommand sqlCommand = new SqlCommand(GetSQLCommandText(addDatabaseInfo.DatabaseName), sqlConnection)) { using (var sqlDataReader = sqlCommand.ExecuteReader()) { long logEntryIndex = 0; logEntriesSourceCache.Edit(innerCache => { while (sqlDataReader.Read()) { if (++logEntryIndex > numberOfOldLogEntries) { //If the log entry is new var newLogEntry = ParseDatabaseLogEntry(sqlDataReader, database.Name, database, LogEntry.GetIdentifier(database.Identifier, logEntryIndex)); newLogEntry.IsNew = true; innerCache.AddOrUpdate(newLogEntry); } } }); } } sqlConnection.Close(); sqlConnection.Dispose(); serviceOperationHelper.LogServiceOperation(ServiceOperationStatus.Succeeded); } catch (Exception ex) { serviceOperationHelper.LogServiceOperation(ex.Message); } }); return(serviceOperationHelper.ServiceOperationResult); }
public static async Task AddDatabaseRoutine(IDialogCoordinator dialogCoordinator, object dialogContext, SourceCache <Database, string> databasesSourceCache) { var maxCharLength = 100; var keepPrompting = true; var dialogSettings1 = new MetroDialogSettings() { AffirmativeButtonText = "Add", }; while (keepPrompting) { var newDatabaseName = dialogCoordinator.ShowModalInputExternal(dialogContext, "Add Database", $"Enter the database name in the format {_databaseNameFormat}.{Environment.NewLine + _databaseNameFormatNote}", dialogSettings1); dialogSettings1.DefaultText = newDatabaseName; if (newDatabaseName == null) { keepPrompting = false; } else if (string.IsNullOrWhiteSpace(newDatabaseName)) { await dialogCoordinator.ShowMessageAsync(dialogContext, "Invalid Database Name", "The new database name cannot be empty or whitespace."); } else if (newDatabaseName.Length > maxCharLength) { await dialogCoordinator.ShowMessageAsync(dialogContext, "Invalid Database Name", $"The new database name cannot be greater than {maxCharLength} characters."); dialogSettings1.DefaultText = newDatabaseName.Substring(0, maxCharLength); } else if (!newDatabaseName.Contains(AddDatabaseInfo.DatabaseInfoSplitter) || newDatabaseName.Contains("{") || newDatabaseName.Contains("}")) { await dialogCoordinator.ShowMessageAsync(dialogContext, "Invalid Database Name", $"The new database name must be in the format {_databaseNameFormat}.{Environment.NewLine + _databaseNameFormatNote}" + Environment.NewLine + $"Example: 198.163.22.10\\MSSQLSERVER,1433{AddDatabaseInfo.DatabaseInfoSplitter}CustomerInfo*username*password"); } else { var addDatabaseInfo = new AddDatabaseInfo(newDatabaseName); if (SettingsService.SettingsContainsDatabaseName(addDatabaseInfo.ToFormattedString())) { await dialogCoordinator.ShowMessageAsync(dialogContext, "Database Already Added", $"Database \"{newDatabaseName}\" has already been added."); keepPrompting = false; } else { //Show progress dialog while searching for new database var progressController = await dialogCoordinator.ShowProgressAsync(dialogContext, "Looking for Database", $"Trying to find database \"{newDatabaseName}\"..."); progressController.SetIndeterminate(); var databaseExists = false; await Task.Run(() => { databaseExists = TestDatabaseConnection(addDatabaseInfo.ToConnectionString()); }); await progressController.CloseAsync(); //If database can't be found, ask user if they want to continue adding database var dialogSettings = new MetroDialogSettings() { AffirmativeButtonText = "Yes", NegativeButtonText = "No", DefaultButtonFocus = MessageDialogResult.Affirmative, }; if (!databaseExists && await dialogCoordinator.ShowMessageAsync(dialogContext, "Database Not Found", $"The database \"{newDatabaseName}\" could not be found. Would you still like to add it?", MessageDialogStyle.AffirmativeAndNegative, dialogSettings) != MessageDialogResult.Affirmative) { return; } var newDatabase = new Database(addDatabaseInfo.ToFormattedString()); var serviceOperationHelper = new ServiceOperationHelper(typeof(Database), Plurality.Single, ServiceOperation.Add, "databases source cache", addDatabaseInfo.ToFormattedString()); await Task.Run(() => { try { serviceOperationHelper.LogServiceOperation(ServiceOperationStatus.Attempting); databasesSourceCache.AddOrUpdate(newDatabase); serviceOperationHelper.LogServiceOperation(ServiceOperationStatus.Succeeded); } catch (Exception ex) { serviceOperationHelper.LogServiceOperation(ex.Message); } }); keepPrompting = false; if (serviceOperationHelper.ServiceOperationResult.OperationSuceeded) { serviceOperationHelper.ServiceOperationResult = await SettingsService.AddOrUpdateDatabase(newDatabase, true); } if (serviceOperationHelper.ServiceOperationResult.OperationFailed) { await serviceOperationHelper.ServiceOperationResult.ShowUserErrorMessage(dialogCoordinator, dialogContext); } } } } }