Skip to content

Add connection test and command-line display #8

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
8 changes: 8 additions & 0 deletions SqlcmdGuiApp/CommandLineWindow.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<Window x:Class="SqlcmdGuiApp.CommandLineWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Command Line" Height="200" Width="600">
<Grid Margin="5">
<TextBox x:Name="CommandTextBox" IsReadOnly="True" TextWrapping="Wrap" VerticalScrollBarVisibility="Auto"/>
</Grid>
</Window>
15 changes: 15 additions & 0 deletions SqlcmdGuiApp/CommandLineWindow.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System.Windows;

namespace SqlcmdGuiApp
{
public partial class CommandLineWindow : Window
{
public CommandLineWindow(string commandLine)
{
InitializeComponent();
CommandTextBox.Text = commandLine;
CommandTextBox.Focus();
CommandTextBox.SelectAll();
}
}
}
6 changes: 5 additions & 1 deletion SqlcmdGuiApp/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@
</ScrollViewer>
</GroupBox>

<Button Content="Execute" Grid.Row="3" Grid.ColumnSpan="2" Height="30" Click="ExecuteButton_Click"/>
<StackPanel Grid.Row="3" Grid.ColumnSpan="2" Orientation="Horizontal" HorizontalAlignment="Center" Margin="0 5 0 0">
<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"/>
</StackPanel>
</Grid>
</Window>
129 changes: 97 additions & 32 deletions SqlcmdGuiApp/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,68 +66,94 @@ private void LoadParameters(string path)
}
}

private void ExecuteButton_Click(object sender, RoutedEventArgs e)
private List<string> BuildSqlcmdArguments(bool includeInputFile)
{
if (!File.Exists(FilePathTextBox.Text))
{
MessageBox.Show("SQL file not found.");
return;
}
var args = new List<string> { "-S", ServerTextBox.Text };

var psi = new ProcessStartInfo("sqlcmd")
{
RedirectStandardOutput = true,
RedirectStandardError = true,
UseShellExecute = false,
CreateNoWindow = true
};
psi.ArgumentList.Add("-S");
psi.ArgumentList.Add(ServerTextBox.Text);
if (!string.IsNullOrEmpty(DatabaseTextBox.Text))
{
psi.ArgumentList.Add("-d");
psi.ArgumentList.Add(DatabaseTextBox.Text);
args.Add("-d");
args.Add(DatabaseTextBox.Text);
}

if (AuthComboBox.SelectedIndex == 1)
{
psi.ArgumentList.Add("-U");
psi.ArgumentList.Add(UserTextBox.Text);
psi.ArgumentList.Add("-P");
psi.ArgumentList.Add(PasswordBox.Password);
args.Add("-U");
args.Add(UserTextBox.Text);
args.Add("-P");
args.Add(PasswordBox.Password);
}
else
{
psi.ArgumentList.Add("-E");
args.Add("-E");
}

var encrypt = (EncryptComboBox.SelectedItem as ComboBoxItem)?.Content?.ToString()?.ToLower();
if (!string.IsNullOrEmpty(encrypt))
{
psi.ArgumentList.Add("-N");
psi.ArgumentList.Add(encrypt);
args.Add("-N");
args.Add(encrypt);
}

if (TrustServerCertificateCheckBox.IsChecked == true)
{
psi.ArgumentList.Add("-C");
args.Add("-C");
}

if (ReadOnlyIntentCheckBox.IsChecked == true)
{
psi.ArgumentList.Add("-K");
psi.ArgumentList.Add("ReadOnly");
args.Add("-K");
args.Add("ReadOnly");
}


foreach (var p in Parameters)
{
psi.ArgumentList.Add("-v");
psi.ArgumentList.Add($"{p.Name}={p.Value}");
args.Add("-v");
args.Add($"{p.Name}={p.Value}");
}

if (includeInputFile)
{
args.Add("-i");
args.Add(FilePathTextBox.Text);
}

return args;
}

private ProcessStartInfo BuildSqlcmdProcessInfo(bool includeInputFile)
{
var psi = new ProcessStartInfo("sqlcmd")
{
RedirectStandardOutput = true,
RedirectStandardError = true,
UseShellExecute = false,
CreateNoWindow = true
};
foreach (var arg in BuildSqlcmdArguments(includeInputFile))
{
psi.ArgumentList.Add(arg);
}
return psi;
}

private static string Quote(string arg) => arg.Contains(' ') ? $"\"{arg}\"" : arg;

private string BuildCommandLine()
{
var args = BuildSqlcmdArguments(true).Select(Quote);
return "sqlcmd " + string.Join(" ", args);
}

private void ExecuteButton_Click(object sender, RoutedEventArgs e)
{
if (!File.Exists(FilePathTextBox.Text))
{
MessageBox.Show("SQL file not found.");
return;
}

psi.ArgumentList.Add("-i");
psi.ArgumentList.Add(FilePathTextBox.Text);
var psi = BuildSqlcmdProcessInfo(true);

try
{
Expand All @@ -149,6 +175,45 @@ private void ExecuteButton_Click(object sender, RoutedEventArgs e)
MessageBox.Show("Failed to execute sqlcmd. See error.log for details.");
}
}

private void TestConnectionButton_Click(object sender, RoutedEventArgs e)
{
var psi = BuildSqlcmdProcessInfo(false);
psi.ArgumentList.Add("-Q");
psi.ArgumentList.Add("SELECT 1");

try
{
var process = Process.Start(psi);
if (process == null)
{
throw new InvalidOperationException("Failed to start sqlcmd process.");
}
process.WaitForExit();
var error = process.StandardError.ReadToEnd();

if (process.ExitCode == 0)
{
MessageBox.Show("Connection successful.");
}
else
{
MessageBox.Show(string.IsNullOrWhiteSpace(error) ? "Connection failed." : error);
}
}
catch (Exception ex)
{
App.LogError(ex.ToString());
MessageBox.Show("Failed to execute sqlcmd. See error.log for details.");
}
}

private void ViewCommandLineButton_Click(object sender, RoutedEventArgs e)
{
var cmd = BuildCommandLine();
var window = new CommandLineWindow(cmd);
window.ShowDialog();
}
}

public class SqlParameter
Expand Down