Skip to content

Commit 8e24781

Browse files
authored
Update and test paths better
1 parent 3b4092d commit 8e24781

31 files changed

Lines changed: 1166 additions & 1083 deletions

App.config

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
<?xml version="1.0" encoding="utf-8" ?>
2-
<configuration>
3-
<startup>
4-
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
5-
</startup>
6-
</configuration>
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<configuration>
3+
<startup>
4+
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.8"/>
5+
</startup>
6+
</configuration>

App.xaml

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
<Application x:Class="SQLBuilder.App"
2-
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3-
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4-
xmlns:local="clr-namespace:SQLBuilder"
5-
StartupUri="MainWindow.xaml">
6-
<Application.Resources>
7-
8-
</Application.Resources>
9-
</Application>
1+
<Application x:Class="SQLBuilder.App"
2+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4+
xmlns:local="clr-namespace:SQLBuilder"
5+
StartupUri="MainWindow.xaml">
6+
<Application.Resources>
7+
8+
</Application.Resources>
9+
</Application>

App.xaml.cs

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Configuration;
4-
using System.Data;
5-
using System.Linq;
6-
using System.Threading.Tasks;
7-
using System.Windows;
8-
9-
namespace SQLBuilder
10-
{
11-
/// <summary>
12-
/// Interaction logic for App.xaml
13-
/// </summary>
14-
public partial class App : Application
15-
{
16-
}
17-
}
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Configuration;
4+
using System.Data;
5+
using System.Linq;
6+
using System.Threading.Tasks;
7+
using System.Windows;
8+
9+
namespace SQLBuilder
10+
{
11+
/// <summary>
12+
/// Interaction logic for App.xaml
13+
/// </summary>
14+
public partial class App : Application
15+
{
16+
}
17+
}

MainWindow.xaml

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,25 @@
1-
<Window x:Class="SQLBuilder.MainWindow"
2-
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3-
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4-
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
5-
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
6-
xmlns:local="clr-namespace:SQLBuilder"
7-
mc:Ignorable="d"
8-
Title="SQL Builder" Height="450" Width="800">
9-
<Grid>
10-
<TextBox x:Name="txt_querybox" HorizontalAlignment="Left" Height="327" Margin="309,51,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="450" VerticalScrollBarVisibility="Visible"/>
11-
<Label Content="SQL Query" HorizontalAlignment="Left" Margin="309,24,0,0" VerticalAlignment="Top"/>
12-
<Label Content="MDF Location" HorizontalAlignment="Left" Margin="57,93,0,0" VerticalAlignment="Top"/>
13-
<Label Content="LDF Location" HorizontalAlignment="Left" Margin="57,160,0,0" VerticalAlignment="Top"/>
14-
<Label Content="File Prefix" HorizontalAlignment="Left" Margin="57,24,0,0" VerticalAlignment="Top"/>
15-
<TextBox x:Name="txt_prefix" HorizontalAlignment="Left" Height="23" Margin="57,55,0,0" Text="" VerticalAlignment="Top" Width="120"/>
16-
<TextBox x:Name="txt_mdfloc" HorizontalAlignment="Left" Height="23" Margin="57,124,0,0" Text="" VerticalAlignment="Top" Width="120" AutomationProperties.IsRequiredForForm="True"/>
17-
<TextBox x:Name="txt_ldfloc" HorizontalAlignment="Left" Height="23" Margin="57,191,0,0" Text="" VerticalAlignment="Top" Width="120" AutomationProperties.IsRequiredForForm="True"/>
18-
<Button Content="Browse..." HorizontalAlignment="Left" Margin="187,124,0,0" VerticalAlignment="Top" Width="87" Height="23" Click="ChooseFolder_mdf"/>
19-
<Button Content="Browse..." HorizontalAlignment="Left" Margin="187,191,0,0" VerticalAlignment="Top" Width="87" Height="23" Click="ChooseFolder_ldf"/>
20-
<Button Content="Submit" HorizontalAlignment="Left" Margin="44,351,0,0" VerticalAlignment="Top" Width="102" Height="27" Click="Submit_Click"/>
21-
<Button Content="Export" HorizontalAlignment="Left" Margin="167,351,0,0" VerticalAlignment="Top" Width="102" Height="27" Click="Export_Query"/>
22-
23-
24-
</Grid>
25-
</Window>
1+
<Window x:Class="SQLBuilder.MainWindow"
2+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4+
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
5+
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
6+
xmlns:local="clr-namespace:SQLBuilder"
7+
mc:Ignorable="d"
8+
Title="SQL Builder" Height="450" Width="800">
9+
<Grid>
10+
<TextBox x:Name="txt_querybox" HorizontalAlignment="Left" Height="327" Margin="309,51,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="450" VerticalScrollBarVisibility="Visible"/>
11+
<Label Content="SQL Query" HorizontalAlignment="Left" Margin="309,24,0,0" VerticalAlignment="Top"/>
12+
<Label Content="MDF Location" HorizontalAlignment="Left" Margin="57,93,0,0" VerticalAlignment="Top"/>
13+
<Label Content="LDF Location" HorizontalAlignment="Left" Margin="57,160,0,0" VerticalAlignment="Top"/>
14+
<Label Content="File Prefix" HorizontalAlignment="Left" Margin="57,24,0,0" VerticalAlignment="Top"/>
15+
<TextBox x:Name="txt_prefix" HorizontalAlignment="Left" Height="23" Margin="57,55,0,0" Text="" VerticalAlignment="Top" Width="120"/>
16+
<TextBox x:Name="txt_mdfloc" HorizontalAlignment="Left" Height="23" Margin="57,124,0,0" Text="" VerticalAlignment="Top" Width="120" AutomationProperties.IsRequiredForForm="True"/>
17+
<TextBox x:Name="txt_ldfloc" HorizontalAlignment="Left" Height="23" Margin="57,191,0,0" Text="" VerticalAlignment="Top" Width="120" AutomationProperties.IsRequiredForForm="True"/>
18+
<Button Content="Browse..." HorizontalAlignment="Left" Margin="187,124,0,0" VerticalAlignment="Top" Width="87" Height="23" Click="ChooseFolder_mdf"/>
19+
<Button Content="Browse..." HorizontalAlignment="Left" Margin="187,191,0,0" VerticalAlignment="Top" Width="87" Height="23" Click="ChooseFolder_ldf"/>
20+
<Button Content="Submit" HorizontalAlignment="Left" Margin="44,351,0,0" VerticalAlignment="Top" Width="102" Height="27" Click="Submit_Click"/>
21+
<Button Content="Export" HorizontalAlignment="Left" Margin="167,351,0,0" VerticalAlignment="Top" Width="102" Height="27" Click="Export_Query"/>
22+
23+
24+
</Grid>
25+
</Window>

MainWindow.xaml.cs

Lines changed: 152 additions & 120 deletions
Original file line numberDiff line numberDiff line change
@@ -1,120 +1,152 @@
1-
using System;
2-
using System.Linq;
3-
using System.Windows;
4-
using System.Windows.Forms;
5-
using System.IO;
6-
7-
8-
namespace SQLBuilder
9-
{
10-
/// <summary>
11-
/// Interaction logic for MainWindow.xaml
12-
/// </summary>
13-
public partial class MainWindow : Window
14-
{
15-
public MainWindow()
16-
{
17-
InitializeComponent();
18-
}
19-
20-
// Icon by www.flaticon.com/authors/smashicons
21-
22-
private void ChooseFolder_mdf(object sender, RoutedEventArgs e)
23-
{
24-
using (var folderDialog = new FolderBrowserDialog())
25-
{
26-
if (folderDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
27-
{
28-
txt_mdfloc.Text = folderDialog.SelectedPath;
29-
// folderDialog.SelectedPath -- your result
30-
}
31-
}
32-
}
33-
34-
private void ChooseFolder_ldf(object sender, RoutedEventArgs e)
35-
{
36-
using (var folderDialog = new FolderBrowserDialog())
37-
{
38-
if (folderDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
39-
{
40-
txt_ldfloc.Text = folderDialog.SelectedPath;
41-
// folderDialog.SelectedPath -- your result
42-
}
43-
}
44-
}
45-
46-
private void Submit_Click(object sender, RoutedEventArgs e)
47-
{
48-
// Process needed inputs
49-
if (String.IsNullOrWhiteSpace(txt_mdfloc.Text))
50-
{
51-
System.Windows.Forms.MessageBox.Show("MDF file location cannot be blank.");
52-
return;
53-
}
54-
55-
if (String.IsNullOrWhiteSpace(txt_ldfloc.Text))
56-
{
57-
System.Windows.Forms.MessageBox.Show("LDF file location cannot be blank.");
58-
return;
59-
}
60-
61-
// Clear output box to allow for new content
62-
txt_querybox.Text = "";
63-
64-
// create Tab character
65-
char tab = '\u0009';
66-
67-
// Get all MDF files from the input field
68-
string[] mdf_files;
69-
mdf_files = Directory.GetFiles(txt_mdfloc.Text, $"*{txt_prefix.Text}*.mdf", SearchOption.TopDirectoryOnly);
70-
71-
// Get all LDF files from the input field
72-
string[] ldf_files;
73-
ldf_files = Directory.GetFiles(txt_ldfloc.Text, $"*{txt_prefix.Text}*.ldf", SearchOption.TopDirectoryOnly);
74-
75-
// Zip arrays together
76-
var filenames = mdf_files.Zip(ldf_files, (m, l) => new { MDF = m, LDF = l });
77-
78-
foreach (var filename in filenames)
79-
{
80-
// Parse base name of file for our database name
81-
string DBName = System.IO.Path.GetFileNameWithoutExtension(filename.MDF);
82-
83-
// Update our text box by adding to the content
84-
// Accessing MDF file path with file.MDF
85-
// Accessing LDF file path with file.LDF
86-
txt_querybox.Text += $@"CREATE DATABASE {DBName}{Environment.NewLine}"
87-
+ $@"{tab}ON (FILENAME = '{filename.MDF}'),{Environment.NewLine}"
88-
+ $@"{tab}(FILENAME = '{filename.LDF}'){Environment.NewLine}"
89-
+ $@"{tab}FOR ATTACH;"
90-
+ Environment.NewLine + Environment.NewLine;
91-
}
92-
}
93-
94-
private void Export_Query(object sender, RoutedEventArgs e)
95-
{
96-
string cwd = Directory.GetCurrentDirectory();
97-
string filename = null;
98-
99-
if (String.IsNullOrEmpty(txt_prefix.Text))
100-
{
101-
filename = $@"{cwd}\MassAttach.sql";
102-
}
103-
else
104-
{
105-
filename = $@"{cwd}\{txt_prefix.Text}_MassAttach.sql";
106-
}
107-
108-
try
109-
{
110-
File.WriteAllText(filename, txt_querybox.Text);
111-
System.Windows.Forms.MessageBox.Show($@"File saved to {filename}");
112-
}
113-
catch (Exception ex)
114-
{
115-
System.Windows.Forms.MessageBox.Show($@"Problem saving to {filename}" + Environment.NewLine + ex);
116-
}
117-
118-
}
119-
}
120-
}
1+
using System;
2+
using System.Linq;
3+
using System.Windows;
4+
using System.Windows.Forms;
5+
using System.IO;
6+
using System.Windows.Shapes;
7+
8+
9+
namespace SQLBuilder
10+
{
11+
/// <summary>
12+
/// Interaction logic for MainWindow.xaml
13+
/// </summary>
14+
public partial class MainWindow : Window
15+
{
16+
public MainWindow()
17+
{
18+
InitializeComponent();
19+
}
20+
21+
// Icon by www.flaticon.com/authors/smashicons
22+
23+
private void ChooseFolder_mdf(object sender, RoutedEventArgs e)
24+
{
25+
using (var folderDialog = new FolderBrowserDialog())
26+
{
27+
if (folderDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
28+
{
29+
txt_mdfloc.Text = folderDialog.SelectedPath;
30+
// folderDialog.SelectedPath -- your result
31+
}
32+
}
33+
}
34+
35+
private void ChooseFolder_ldf(object sender, RoutedEventArgs e)
36+
{
37+
using (var folderDialog = new FolderBrowserDialog())
38+
{
39+
if (folderDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
40+
{
41+
txt_ldfloc.Text = folderDialog.SelectedPath;
42+
// folderDialog.SelectedPath -- your result
43+
}
44+
}
45+
}
46+
47+
private void Submit_Click(object sender, RoutedEventArgs e)
48+
{
49+
// Process needed inputs
50+
if (String.IsNullOrWhiteSpace(txt_mdfloc.Text))
51+
{
52+
System.Windows.Forms.MessageBox.Show("MDF file location cannot be blank.");
53+
return;
54+
}
55+
56+
if (String.IsNullOrWhiteSpace(txt_ldfloc.Text))
57+
{
58+
System.Windows.Forms.MessageBox.Show("LDF file location cannot be blank.");
59+
return;
60+
}
61+
62+
// Clear output box to allow for new content
63+
txt_querybox.Text = "";
64+
65+
// create Tab character
66+
char tab = '\u0009';
67+
68+
// Get all MDF files from the input field
69+
string[] mdf_files;
70+
if (Directory.Exists(txt_mdfloc.Text)) {
71+
mdf_files = Directory.GetFiles(txt_mdfloc.Text, $"*{txt_prefix.Text}*.mdf", SearchOption.TopDirectoryOnly);
72+
// Check if the path(s) have files
73+
if (mdf_files.Length < 1)
74+
{
75+
System.Windows.Forms.MessageBox.Show("MDF file location could not find files.");
76+
return;
77+
}
78+
}
79+
else
80+
{
81+
System.Windows.Forms.MessageBox.Show("MDF directory could not be found.");
82+
return;
83+
}
84+
85+
// Get all LDF files from the input field
86+
string[] ldf_files;
87+
if (Directory.Exists(txt_ldfloc.Text))
88+
{
89+
ldf_files = Directory.GetFiles(txt_ldfloc.Text, $"*{txt_prefix.Text}*.ldf", SearchOption.TopDirectoryOnly);
90+
// Check if the path(s) have files
91+
if (ldf_files.Length < 1)
92+
{
93+
System.Windows.Forms.MessageBox.Show("LDF file location could not find files.");
94+
return;
95+
}
96+
}
97+
else
98+
{
99+
System.Windows.Forms.MessageBox.Show("LDF directory could not be found.");
100+
return;
101+
}
102+
103+
104+
105+
106+
// Zip arrays together
107+
var filenames = mdf_files.Zip(ldf_files, (m, l) => new { MDF = m, LDF = l });
108+
109+
foreach (var filename in filenames)
110+
{
111+
// Parse base name of file for our database name
112+
string DBName = System.IO.Path.GetFileNameWithoutExtension(filename.MDF);
113+
DBName = DBName.Replace("_Data", "");
114+
115+
// Update our text box by adding to the content
116+
// Accessing MDF file path with file.MDF
117+
// Accessing LDF file path with file.LDF
118+
txt_querybox.Text += $@"CREATE DATABASE [{DBName}]{Environment.NewLine}"
119+
+ $@"{tab}ON (FILENAME = '{filename.MDF}'),{Environment.NewLine}"
120+
+ $@"{tab}(FILENAME = '{filename.LDF}'){Environment.NewLine}"
121+
+ $@"{tab}FOR ATTACH;"
122+
+ Environment.NewLine + Environment.NewLine;
123+
}
124+
}
125+
126+
private void Export_Query(object sender, RoutedEventArgs e)
127+
{
128+
string cwd = Directory.GetCurrentDirectory();
129+
string filename = null;
130+
131+
if (String.IsNullOrEmpty(txt_prefix.Text))
132+
{
133+
filename = $@"{cwd}\MassAttach.sql";
134+
}
135+
else
136+
{
137+
filename = $@"{cwd}\{txt_prefix.Text}_MassAttach.sql";
138+
}
139+
140+
try
141+
{
142+
File.WriteAllText(filename, txt_querybox.Text);
143+
System.Windows.Forms.MessageBox.Show($@"File saved to {filename}");
144+
}
145+
catch (Exception ex)
146+
{
147+
System.Windows.Forms.MessageBox.Show($@"Problem saving to {filename}" + Environment.NewLine + ex);
148+
}
149+
150+
}
151+
}
152+
}

0 commit comments

Comments
 (0)