Skip to content

Commit

Permalink
Update and test paths better
Browse files Browse the repository at this point in the history
  • Loading branch information
cjerrington authored Feb 6, 2024
1 parent 3b4092d commit 8e24781
Show file tree
Hide file tree
Showing 31 changed files with 1,166 additions and 1,083 deletions.
12 changes: 6 additions & 6 deletions App.config
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
</startup>
</configuration>
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.8"/>
</startup>
</configuration>
18 changes: 9 additions & 9 deletions App.xaml
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<Application x:Class="SQLBuilder.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:SQLBuilder"
StartupUri="MainWindow.xaml">
<Application.Resources>

</Application.Resources>
</Application>
<Application x:Class="SQLBuilder.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:SQLBuilder"
StartupUri="MainWindow.xaml">
<Application.Resources>

</Application.Resources>
</Application>
34 changes: 17 additions & 17 deletions App.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;

namespace SQLBuilder
{
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : Application
{
}
}
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;

namespace SQLBuilder
{
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : Application
{
}
}
50 changes: 25 additions & 25 deletions MainWindow.xaml
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
<Window x:Class="SQLBuilder.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:SQLBuilder"
mc:Ignorable="d"
Title="SQL Builder" Height="450" Width="800">
<Grid>
<TextBox x:Name="txt_querybox" HorizontalAlignment="Left" Height="327" Margin="309,51,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="450" VerticalScrollBarVisibility="Visible"/>
<Label Content="SQL Query" HorizontalAlignment="Left" Margin="309,24,0,0" VerticalAlignment="Top"/>
<Label Content="MDF Location" HorizontalAlignment="Left" Margin="57,93,0,0" VerticalAlignment="Top"/>
<Label Content="LDF Location" HorizontalAlignment="Left" Margin="57,160,0,0" VerticalAlignment="Top"/>
<Label Content="File Prefix" HorizontalAlignment="Left" Margin="57,24,0,0" VerticalAlignment="Top"/>
<TextBox x:Name="txt_prefix" HorizontalAlignment="Left" Height="23" Margin="57,55,0,0" Text="" VerticalAlignment="Top" Width="120"/>
<TextBox x:Name="txt_mdfloc" HorizontalAlignment="Left" Height="23" Margin="57,124,0,0" Text="" VerticalAlignment="Top" Width="120" AutomationProperties.IsRequiredForForm="True"/>
<TextBox x:Name="txt_ldfloc" HorizontalAlignment="Left" Height="23" Margin="57,191,0,0" Text="" VerticalAlignment="Top" Width="120" AutomationProperties.IsRequiredForForm="True"/>
<Button Content="Browse..." HorizontalAlignment="Left" Margin="187,124,0,0" VerticalAlignment="Top" Width="87" Height="23" Click="ChooseFolder_mdf"/>
<Button Content="Browse..." HorizontalAlignment="Left" Margin="187,191,0,0" VerticalAlignment="Top" Width="87" Height="23" Click="ChooseFolder_ldf"/>
<Button Content="Submit" HorizontalAlignment="Left" Margin="44,351,0,0" VerticalAlignment="Top" Width="102" Height="27" Click="Submit_Click"/>
<Button Content="Export" HorizontalAlignment="Left" Margin="167,351,0,0" VerticalAlignment="Top" Width="102" Height="27" Click="Export_Query"/>


</Grid>
</Window>
<Window x:Class="SQLBuilder.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:SQLBuilder"
mc:Ignorable="d"
Title="SQL Builder" Height="450" Width="800">
<Grid>
<TextBox x:Name="txt_querybox" HorizontalAlignment="Left" Height="327" Margin="309,51,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="450" VerticalScrollBarVisibility="Visible"/>
<Label Content="SQL Query" HorizontalAlignment="Left" Margin="309,24,0,0" VerticalAlignment="Top"/>
<Label Content="MDF Location" HorizontalAlignment="Left" Margin="57,93,0,0" VerticalAlignment="Top"/>
<Label Content="LDF Location" HorizontalAlignment="Left" Margin="57,160,0,0" VerticalAlignment="Top"/>
<Label Content="File Prefix" HorizontalAlignment="Left" Margin="57,24,0,0" VerticalAlignment="Top"/>
<TextBox x:Name="txt_prefix" HorizontalAlignment="Left" Height="23" Margin="57,55,0,0" Text="" VerticalAlignment="Top" Width="120"/>
<TextBox x:Name="txt_mdfloc" HorizontalAlignment="Left" Height="23" Margin="57,124,0,0" Text="" VerticalAlignment="Top" Width="120" AutomationProperties.IsRequiredForForm="True"/>
<TextBox x:Name="txt_ldfloc" HorizontalAlignment="Left" Height="23" Margin="57,191,0,0" Text="" VerticalAlignment="Top" Width="120" AutomationProperties.IsRequiredForForm="True"/>
<Button Content="Browse..." HorizontalAlignment="Left" Margin="187,124,0,0" VerticalAlignment="Top" Width="87" Height="23" Click="ChooseFolder_mdf"/>
<Button Content="Browse..." HorizontalAlignment="Left" Margin="187,191,0,0" VerticalAlignment="Top" Width="87" Height="23" Click="ChooseFolder_ldf"/>
<Button Content="Submit" HorizontalAlignment="Left" Margin="44,351,0,0" VerticalAlignment="Top" Width="102" Height="27" Click="Submit_Click"/>
<Button Content="Export" HorizontalAlignment="Left" Margin="167,351,0,0" VerticalAlignment="Top" Width="102" Height="27" Click="Export_Query"/>


</Grid>
</Window>
272 changes: 152 additions & 120 deletions MainWindow.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,120 +1,152 @@
using System;
using System.Linq;
using System.Windows;
using System.Windows.Forms;
using System.IO;


namespace SQLBuilder
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}

// Icon by www.flaticon.com/authors/smashicons

private void ChooseFolder_mdf(object sender, RoutedEventArgs e)
{
using (var folderDialog = new FolderBrowserDialog())
{
if (folderDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
txt_mdfloc.Text = folderDialog.SelectedPath;
// folderDialog.SelectedPath -- your result
}
}
}

private void ChooseFolder_ldf(object sender, RoutedEventArgs e)
{
using (var folderDialog = new FolderBrowserDialog())
{
if (folderDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
txt_ldfloc.Text = folderDialog.SelectedPath;
// folderDialog.SelectedPath -- your result
}
}
}

private void Submit_Click(object sender, RoutedEventArgs e)
{
// Process needed inputs
if (String.IsNullOrWhiteSpace(txt_mdfloc.Text))
{
System.Windows.Forms.MessageBox.Show("MDF file location cannot be blank.");
return;
}

if (String.IsNullOrWhiteSpace(txt_ldfloc.Text))
{
System.Windows.Forms.MessageBox.Show("LDF file location cannot be blank.");
return;
}

// Clear output box to allow for new content
txt_querybox.Text = "";

// create Tab character
char tab = '\u0009';

// Get all MDF files from the input field
string[] mdf_files;
mdf_files = Directory.GetFiles(txt_mdfloc.Text, $"*{txt_prefix.Text}*.mdf", SearchOption.TopDirectoryOnly);

// Get all LDF files from the input field
string[] ldf_files;
ldf_files = Directory.GetFiles(txt_ldfloc.Text, $"*{txt_prefix.Text}*.ldf", SearchOption.TopDirectoryOnly);

// Zip arrays together
var filenames = mdf_files.Zip(ldf_files, (m, l) => new { MDF = m, LDF = l });

foreach (var filename in filenames)
{
// Parse base name of file for our database name
string DBName = System.IO.Path.GetFileNameWithoutExtension(filename.MDF);

// Update our text box by adding to the content
// Accessing MDF file path with file.MDF
// Accessing LDF file path with file.LDF
txt_querybox.Text += $@"CREATE DATABASE {DBName}{Environment.NewLine}"
+ $@"{tab}ON (FILENAME = '{filename.MDF}'),{Environment.NewLine}"
+ $@"{tab}(FILENAME = '{filename.LDF}'){Environment.NewLine}"
+ $@"{tab}FOR ATTACH;"
+ Environment.NewLine + Environment.NewLine;
}
}

private void Export_Query(object sender, RoutedEventArgs e)
{
string cwd = Directory.GetCurrentDirectory();
string filename = null;

if (String.IsNullOrEmpty(txt_prefix.Text))
{
filename = $@"{cwd}\MassAttach.sql";
}
else
{
filename = $@"{cwd}\{txt_prefix.Text}_MassAttach.sql";
}

try
{
File.WriteAllText(filename, txt_querybox.Text);
System.Windows.Forms.MessageBox.Show($@"File saved to {filename}");
}
catch (Exception ex)
{
System.Windows.Forms.MessageBox.Show($@"Problem saving to {filename}" + Environment.NewLine + ex);
}

}
}
}
using System;
using System.Linq;
using System.Windows;
using System.Windows.Forms;
using System.IO;
using System.Windows.Shapes;


namespace SQLBuilder
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}

// Icon by www.flaticon.com/authors/smashicons

private void ChooseFolder_mdf(object sender, RoutedEventArgs e)
{
using (var folderDialog = new FolderBrowserDialog())
{
if (folderDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
txt_mdfloc.Text = folderDialog.SelectedPath;
// folderDialog.SelectedPath -- your result
}
}
}

private void ChooseFolder_ldf(object sender, RoutedEventArgs e)
{
using (var folderDialog = new FolderBrowserDialog())
{
if (folderDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
txt_ldfloc.Text = folderDialog.SelectedPath;
// folderDialog.SelectedPath -- your result
}
}
}

private void Submit_Click(object sender, RoutedEventArgs e)
{
// Process needed inputs
if (String.IsNullOrWhiteSpace(txt_mdfloc.Text))
{
System.Windows.Forms.MessageBox.Show("MDF file location cannot be blank.");
return;
}

if (String.IsNullOrWhiteSpace(txt_ldfloc.Text))
{
System.Windows.Forms.MessageBox.Show("LDF file location cannot be blank.");
return;
}

// Clear output box to allow for new content
txt_querybox.Text = "";

// create Tab character
char tab = '\u0009';

// Get all MDF files from the input field
string[] mdf_files;
if (Directory.Exists(txt_mdfloc.Text)) {
mdf_files = Directory.GetFiles(txt_mdfloc.Text, $"*{txt_prefix.Text}*.mdf", SearchOption.TopDirectoryOnly);
// Check if the path(s) have files
if (mdf_files.Length < 1)
{
System.Windows.Forms.MessageBox.Show("MDF file location could not find files.");
return;
}
}
else
{
System.Windows.Forms.MessageBox.Show("MDF directory could not be found.");
return;
}

// Get all LDF files from the input field
string[] ldf_files;
if (Directory.Exists(txt_ldfloc.Text))
{
ldf_files = Directory.GetFiles(txt_ldfloc.Text, $"*{txt_prefix.Text}*.ldf", SearchOption.TopDirectoryOnly);
// Check if the path(s) have files
if (ldf_files.Length < 1)
{
System.Windows.Forms.MessageBox.Show("LDF file location could not find files.");
return;
}
}
else
{
System.Windows.Forms.MessageBox.Show("LDF directory could not be found.");
return;
}




// Zip arrays together
var filenames = mdf_files.Zip(ldf_files, (m, l) => new { MDF = m, LDF = l });

foreach (var filename in filenames)
{
// Parse base name of file for our database name
string DBName = System.IO.Path.GetFileNameWithoutExtension(filename.MDF);
DBName = DBName.Replace("_Data", "");

// Update our text box by adding to the content
// Accessing MDF file path with file.MDF
// Accessing LDF file path with file.LDF
txt_querybox.Text += $@"CREATE DATABASE [{DBName}]{Environment.NewLine}"
+ $@"{tab}ON (FILENAME = '{filename.MDF}'),{Environment.NewLine}"
+ $@"{tab}(FILENAME = '{filename.LDF}'){Environment.NewLine}"
+ $@"{tab}FOR ATTACH;"
+ Environment.NewLine + Environment.NewLine;
}
}

private void Export_Query(object sender, RoutedEventArgs e)
{
string cwd = Directory.GetCurrentDirectory();
string filename = null;

if (String.IsNullOrEmpty(txt_prefix.Text))
{
filename = $@"{cwd}\MassAttach.sql";
}
else
{
filename = $@"{cwd}\{txt_prefix.Text}_MassAttach.sql";
}

try
{
File.WriteAllText(filename, txt_querybox.Text);
System.Windows.Forms.MessageBox.Show($@"File saved to {filename}");
}
catch (Exception ex)
{
System.Windows.Forms.MessageBox.Show($@"Problem saving to {filename}" + Environment.NewLine + ex);
}

}
}
}
Loading

0 comments on commit 8e24781

Please sign in to comment.