Skip to content

Add config save/load support #11

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions SqlcmdGuiApp/Configuration.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System.Collections.Generic;

namespace SqlcmdGuiApp
{
public class Configuration
{
public string FilePath { get; set; } = string.Empty;
public string Server { get; set; } = string.Empty;
public string Database { get; set; } = string.Empty;
public bool UseSqlAuth { get; set; }
public string User { get; set; } = string.Empty;
public string Password { get; set; } = string.Empty;
public bool Encrypt { get; set; }
public bool TrustServerCertificate { get; set; }
public bool ReadOnlyIntent { get; set; }
public List<SqlParameter> Parameters { get; set; } = new();
}
}
2 changes: 2 additions & 0 deletions SqlcmdGuiApp/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@
</GroupBox>

<StackPanel Grid.Row="3" Grid.ColumnSpan="2" Orientation="Horizontal" HorizontalAlignment="Center" Margin="0 5 0 0">
<Button Content="Load Config" Margin="5" Width="100" Click="LoadConfigButton_Click"/>
<Button Content="Save Config" Margin="5" Width="100" Click="SaveConfigButton_Click"/>
<Button Content="Test Connection" Margin="5" Width="120" Click="TestConnectionButton_Click"/>
<Button Content="View Command-Line" Margin="5" Width="120" Click="ViewCommandLineButton_Click"/>
<Button Content="Execute" Margin="5" Width="80" Click="ExecuteButton_Click"/>
Expand Down
66 changes: 66 additions & 0 deletions SqlcmdGuiApp/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using System.Linq;
using System.Text.RegularExpressions;
using System.Security.Principal;
using System.Text.Json;

namespace SqlcmdGuiApp
{
Expand Down Expand Up @@ -143,6 +144,71 @@ private string BuildCommandLine()
return "sqlcmd " + string.Join(" ", args);
}

private void SaveConfigButton_Click(object sender, RoutedEventArgs e)
{
var dlg = new SaveFileDialog { Filter = "JSON Files (*.json)|*.json|All Files (*.*)|*.*" };
if (dlg.ShowDialog() != true) return;

var config = new Configuration
{
FilePath = FilePathTextBox.Text,
Server = ServerTextBox.Text,
Database = DatabaseTextBox.Text,
UseSqlAuth = AuthComboBox.SelectedIndex == 1,
User = UserTextBox.Text,
Password = PasswordBox.Password,
Encrypt = EncryptCheckBox.IsChecked == true,
TrustServerCertificate = TrustServerCertificateCheckBox.IsChecked == true,
ReadOnlyIntent = ReadOnlyIntentCheckBox.IsChecked == true,
Parameters = Parameters.Select(p => new SqlParameter { Name = p.Name, Value = p.Value }).ToList()
};

try
{
var json = JsonSerializer.Serialize(config, new JsonSerializerOptions { WriteIndented = true });
File.WriteAllText(dlg.FileName, json);
}
catch (Exception ex)
{
App.LogError(ex.ToString());
MessageBox.Show("Failed to save configuration. See error.log for details.");
}
}

private void LoadConfigButton_Click(object sender, RoutedEventArgs e)
{
var dlg = new OpenFileDialog { Filter = "JSON Files (*.json)|*.json|All Files (*.*)|*.*" };
if (dlg.ShowDialog() != true) return;

try
{
var json = File.ReadAllText(dlg.FileName);
var config = JsonSerializer.Deserialize<Configuration>(json);
if (config == null) throw new InvalidOperationException("Invalid configuration file.");

FilePathTextBox.Text = config.FilePath;
ServerTextBox.Text = config.Server;
DatabaseTextBox.Text = config.Database;
AuthComboBox.SelectedIndex = config.UseSqlAuth ? 1 : 0;
UserTextBox.Text = config.User;
PasswordBox.Password = config.Password;
EncryptCheckBox.IsChecked = config.Encrypt;
TrustServerCertificateCheckBox.IsChecked = config.TrustServerCertificate;
ReadOnlyIntentCheckBox.IsChecked = config.ReadOnlyIntent;

Parameters.Clear();
foreach (var p in config.Parameters)
{
Parameters.Add(new SqlParameter { Name = p.Name, Value = p.Value });
}
}
catch (Exception ex)
{
App.LogError(ex.ToString());
MessageBox.Show("Failed to load configuration. See error.log for details.");
}
}

private async void ExecuteButton_Click(object sender, RoutedEventArgs e)
{
if (!File.Exists(FilePathTextBox.Text))
Expand Down