private void LoadEventsFromDatabase()
        {
            const string eventSelectQueryText =
                @"select * from FileSystemEvent where FolderTraceGUID = @FolderTraceGUID order by FileSystemEventDate desc";

            gridEvents.Rows.Clear();
            if (ActiveFolderTrace != null)
            {
                QueryParameterSet queryParameters = new QueryParameterSet();
                queryParameters.Add("@FolderTraceGUID", ActiveFolderTrace.GUID.ToString());
                IQueryResult queryResult = DataProvider.Database.Query(eventSelectQueryText, queryParameters, true);

                int rowCount = queryResult.Rows.Count;
                for (int i = 0; i < gridTraces.Rows.Count; i++)
                {
                    if (gridTraces.Rows[i].Tag == ActiveFolderTrace)
                    {
                        gridTraces[colTraceEventCount.Index, i].Value = rowCount;
                    }
                }

                for (int i = 0; i < rowCount; i++)
                {
                    int eventType = Convert.ToInt32(queryResult[i]["FileSystemEventType"]);
                    DateTime eventDate = Convert.ToDateTime(queryResult[i]["FileSystemEventDate"]);
                    string oldName = Convert.ToString(queryResult[i]["FileSystemEventOldName"]);
                    string newName = Convert.ToString(queryResult[i]["FileSystemEventNewName"]);

                    FileSystemEvent fsEvent = new FileSystemEvent((FileSystemEventType)eventType, oldName, newName, eventDate);
                    AddEventGridEntry(fsEvent);
                }
            }
        }
        private void RegisterEvent(FileSystemEventType eventType, string oldFullPath, string newFullPath, Guid traceGuid)
        {
            try
            {
                QueryParameterSet parameters = new QueryParameterSet();
                parameters.Add("@FolderTraceGUID", traceGuid.ToString());
                parameters.Add("@FileSystemEventType", (int)eventType);
                parameters.Add("@FileSystemEventDate", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                parameters.Add("@FileSystemEventOldName", oldFullPath);
                parameters.Add("@FileSystemEventNewName", newFullPath);
                DataProvider.Database.Query(eventInsertQueryText, parameters, false);

                if (eventType != FileSystemEventType.Renamed)
                {
                    EventLogger.RecordEvent(Constants.NTServiceEventSource, LogEventType.Information, string.Format("{0}: {1}", eventType, newFullPath));
                }
                else
                {
                    EventLogger.RecordEvent(Constants.NTServiceEventSource, LogEventType.Information, string.Format("{0}: {1} to {2}", eventType, oldFullPath, newFullPath));
                }
            }
            catch (Exception ex)
            {
                EventLogger.RecordException(Constants.NTServiceEventSource, ex, "Failed to register event: ");
            }
        }
        private void CleanupDatabase(int unitCount, FormDatabaseCleanup.TimeUnitType unitType)
        {
            DateTime maxDateTime = DateTime.Now;
            switch (unitType)
            {
                case FormDatabaseCleanup.TimeUnitType.Year:
                    maxDateTime = maxDateTime.AddYears(-unitCount);
                    break;
                case FormDatabaseCleanup.TimeUnitType.Month:
                    maxDateTime = maxDateTime.AddMonths(-unitCount);
                    break;
                case FormDatabaseCleanup.TimeUnitType.Day:
                    maxDateTime = maxDateTime.AddDays(-unitCount);
                    break;
                case FormDatabaseCleanup.TimeUnitType.Hour:
                    maxDateTime = maxDateTime.AddHours(-unitCount);
                    break;
                case FormDatabaseCleanup.TimeUnitType.Minute:
                    maxDateTime = maxDateTime.AddMinutes(-unitCount);
                    break;
                case FormDatabaseCleanup.TimeUnitType.Second:
                    maxDateTime = maxDateTime.AddSeconds(-unitCount);
                    break;
                default:
                    throw new ArgumentOutOfRangeException("unitType");
            }

            const string dbCleanupQuery = "delete from FileSystemEvent where FileSystemEventDate < @maxDateTime";
            try
            {
                QueryParameterSet queryParameterSet = new QueryParameterSet();
                queryParameterSet.Add("@maxDateTime", maxDateTime.ToString("yyyy-MM-dd HH:mm:ss"));
                DataProvider.Database.Query(dbCleanupQuery, queryParameterSet, false);
            }
            catch (Exception ex)
            {
                EventLogger.RecordException(Constants.AdminApplicationEventSource, ex, "Failed to clean up database: ");
                FormHelper.ShowErrorMessage("Failed to clean up database: " + ex.Message, "Error");
            }
        }