From c23e688e59ae891d1dc9d7031a37416911ce58d2 Mon Sep 17 00:00:00 2001 From: Ryan Poole Date: Sat, 24 Feb 2018 15:55:54 +0000 Subject: [PATCH] Global setting for command timeouts. --- RPForEachDB/App.config | 11 ++++ RPForEachDB/AppState.cs | 36 +++++++++++++ RPForEachDB/ConnectionManager.xaml | 36 ------------- RPForEachDB/MainWindow.xaml | 4 +- RPForEachDB/MainWindow.xaml.cs | 9 ++-- RPForEachDB/Properties/Settings.Designer.cs | 12 +++++ RPForEachDB/Properties/Settings.settings | 3 ++ RPForEachDB/RPForEachDB.csproj | 8 +-- RPForEachDB/SQLTasks.cs | 9 ++++ RPForEachDB/SettingsWindow.xaml | 53 +++++++++++++++++++ ...Manager.xaml.cs => SettingsWindow.xaml.cs} | 35 ++++++++++-- RPForEachDB/StateManager.cs | 25 --------- 12 files changed, 166 insertions(+), 75 deletions(-) create mode 100644 RPForEachDB/AppState.cs delete mode 100644 RPForEachDB/ConnectionManager.xaml create mode 100644 RPForEachDB/SettingsWindow.xaml rename RPForEachDB/{ConnectionManager.xaml.cs => SettingsWindow.xaml.cs} (80%) delete mode 100644 RPForEachDB/StateManager.cs diff --git a/RPForEachDB/App.config b/RPForEachDB/App.config index 786e004..2a16068 100644 --- a/RPForEachDB/App.config +++ b/RPForEachDB/App.config @@ -2,6 +2,10 @@ + + +
+ @@ -20,4 +24,11 @@ + + + + 30 + + + \ No newline at end of file diff --git a/RPForEachDB/AppState.cs b/RPForEachDB/AppState.cs new file mode 100644 index 0000000..34184aa --- /dev/null +++ b/RPForEachDB/AppState.cs @@ -0,0 +1,36 @@ +using RPForEachDB.Properties; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace RPForEachDB +{ + public interface IAppState + { + IEnumerable Servers { get; set; } + int CommandTimeout { get; set; } + void Save(); + } + public class AppState: IAppState + { + public IEnumerable Servers { get; set; } + public int CommandTimeout + { + get => Settings.Default.CommandTimeout; + set => Settings.Default.CommandTimeout = value; + } + + public AppState() + { + Servers = Settings.Default.Servers ?? new IServerModel[0]; + } + + public void Save() + { + Settings.Default.Servers = Servers.Cast().ToArray(); + Settings.Default.Save(); + } + } +} diff --git a/RPForEachDB/ConnectionManager.xaml b/RPForEachDB/ConnectionManager.xaml deleted file mode 100644 index c3b5667..0000000 --- a/RPForEachDB/ConnectionManager.xaml +++ /dev/null @@ -1,36 +0,0 @@ - - - - - - - - diff --git a/RPForEachDB/MainWindow.xaml.cs b/RPForEachDB/MainWindow.xaml.cs index 4fc4086..42303b1 100644 --- a/RPForEachDB/MainWindow.xaml.cs +++ b/RPForEachDB/MainWindow.xaml.cs @@ -4,7 +4,6 @@ using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; -//using Dapper; using System.Data.SqlClient; using System.ComponentModel; using System.IO; @@ -16,7 +15,7 @@ namespace RPForEachDB { public partial class MainWindow : Window, INotifyPropertyChanged { - private readonly StateManager _appState; + private readonly IAppState _appState; public ObservableCollection Databases { get; set; } public ObservableCollection Servers { get; set; } private IServerModel _currentServer { get; set; } @@ -76,8 +75,8 @@ private void NotifyPropertyChanged(string propertyName) public MainWindow() { - _sqlTasks = new SQLTasks(); - _appState = new StateManager(); + _appState = new AppState(); + _sqlTasks = new SQLTasks(_appState); Servers = new ObservableCollection(_appState.Servers); Databases = new ObservableCollection(); DataContext = this; @@ -186,7 +185,7 @@ protected override void OnClosing(CancelEventArgs e) private void OnManageServersBtnClick(object sender, RoutedEventArgs e) { - new ConnectionManager(_appState) + new SettingsWindow(_appState) { Owner = this }.ShowDialog(); diff --git a/RPForEachDB/Properties/Settings.Designer.cs b/RPForEachDB/Properties/Settings.Designer.cs index 4d3a71b..1fdf06c 100644 --- a/RPForEachDB/Properties/Settings.Designer.cs +++ b/RPForEachDB/Properties/Settings.Designer.cs @@ -33,5 +33,17 @@ public RPForEachDB.ServerModel[] Servers { this["Servers"] = value; } } + + [global::System.Configuration.UserScopedSettingAttribute()] + [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] + [global::System.Configuration.DefaultSettingValueAttribute("30")] + public int CommandTimeout { + get { + return ((int)(this["CommandTimeout"])); + } + set { + this["CommandTimeout"] = value; + } + } } } diff --git a/RPForEachDB/Properties/Settings.settings b/RPForEachDB/Properties/Settings.settings index 0ff9246..29e20f1 100644 --- a/RPForEachDB/Properties/Settings.settings +++ b/RPForEachDB/Properties/Settings.settings @@ -5,5 +5,8 @@ + + 30 + \ No newline at end of file diff --git a/RPForEachDB/RPForEachDB.csproj b/RPForEachDB/RPForEachDB.csproj index c098b73..c08f419 100644 --- a/RPForEachDB/RPForEachDB.csproj +++ b/RPForEachDB/RPForEachDB.csproj @@ -74,8 +74,8 @@ - - + + Designer MSBuild:Compile @@ -88,8 +88,8 @@ Code - - ConnectionManager.xaml + + SettingsWindow.xaml diff --git a/RPForEachDB/SQLTasks.cs b/RPForEachDB/SQLTasks.cs index 752ed15..084ec5f 100644 --- a/RPForEachDB/SQLTasks.cs +++ b/RPForEachDB/SQLTasks.cs @@ -9,11 +9,19 @@ namespace RPForEachDB { public class SQLTasks { + private readonly IAppState _appState; + + public SQLTasks(IAppState appState) + { + _appState = appState; + } + public IEnumerable GetAllDatabases(IDbConnection connection) { var command = connection.CreateCommand(); command.CommandType = CommandType.Text; command.CommandText = "SELECT Name FROM master.dbo.sysdatabases WHERE DATABASEPROPERTYEX(Name, 'Status') = 'ONLINE'"; + command.CommandTimeout = _appState.CommandTimeout; using (var reader = command.ExecuteReader()) { while (reader.Read()) @@ -26,6 +34,7 @@ public void Execute(IDbConnection connection, string commandText) var command = connection.CreateCommand(); command.CommandType = CommandType.Text; command.CommandText = commandText; + command.CommandTimeout = _appState.CommandTimeout; command.ExecuteNonQuery(); } } diff --git a/RPForEachDB/SettingsWindow.xaml b/RPForEachDB/SettingsWindow.xaml new file mode 100644 index 0000000..dc57d9a --- /dev/null +++ b/RPForEachDB/SettingsWindow.xaml @@ -0,0 +1,53 @@ + + + + + + + + + + + + + +