|
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