-
Notifications
You must be signed in to change notification settings - Fork 1
/
MainForm.cs
298 lines (260 loc) · 10.8 KB
/
MainForm.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CommonLibrary;
using Newtonsoft.Json;
using ScaffoldDbContextHelper.Classes;
using ScaffoldDbContextHelper.Forms;
using static FormControlLibrary.KarenDialogs;
namespace ScaffoldDbContextHelper
{
public partial class MainForm : Form
{
/// <summary>
/// Change server name to your server name.
/// </summary>
private ScaffoldBuilder _scaffoldBuilder = new ScaffoldBuilder("KARENS-PC");
private string _applicationSettingsFile = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "AppSettings.json");
public MainForm()
{
InitializeComponent();
Shown += Form1_Shown;
ListBoxSearchTextBox.TextChanged += ListBoxSearchTextBox_TextChanged;
SolutionFileDialog1.InitialDirectory = AppDomain.CurrentDomain.BaseDirectory.CurrentSolutionFolder();
ServerNameTextBox.Text = _scaffoldBuilder.ServerName;
ReadApplicationSettingsFile();
}
/// <summary>
/// Read setting from application setting json file (decided not to do StartupProject)
/// </summary>
private void ReadApplicationSettingsFile()
{
using (var file = File.OpenText(_applicationSettingsFile))
{
var serializer = new JsonSerializer();
var applicationSettings = (ApplicationSettings)serializer.Deserialize(file, typeof(ApplicationSettings));
if (!string.IsNullOrWhiteSpace(applicationSettings.LastServerName))
{
ServerNameTextBox.Text = applicationSettings.LastServerName;
}
var dataProvider = new DatabaseProviders();
ProviderComboBox.DataSource = dataProvider.List;
if (!string.IsNullOrWhiteSpace(applicationSettings.DataProvider))
{
if (ProviderComboBox.DataSource != null)
{
var index = ProviderComboBox.FindString(applicationSettings.DataProvider);
if (index > -1)
{
ProviderComboBox.SelectedIndex = index;
}
}
}
_scaffoldBuilder = new ScaffoldBuilder(applicationSettings.LastServerName);
}
}
/// <summary>
/// Update application json file
/// </summary>
private void SaveApplicationSettings()
{
var applicationSettings = new ApplicationSettings()
{
LastServerName = ServerNameTextBox.Text,
StartupProject = StartupProjectTextBox.Text
};
ProviderComboBox.Invoke(new Action(() => applicationSettings.DataProvider = ProviderComboBox.Text));
using (var file = File.CreateText(_applicationSettingsFile))
{
var serializer = new JsonSerializer { Formatting = Formatting.Indented };
serializer.Serialize(file, applicationSettings);
}
}
/// <summary>
/// Find text/database starting on second character
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ListBoxSearchTextBox_TextChanged(object sender, EventArgs e)
{
var searchText = ListBoxSearchTextBox.Text;
for (var index = 0; index <= DatabaseListBox.Items.Count - 1; index++)
{
if (!DatabaseListBox.Items[index].ToString().ToLower().Contains(searchText)) continue;
DatabaseListBox.SetSelected(index, true);
break;
}
}
/// <summary>
/// Get databases from ServerName form level variable above
/// </summary>
private void Form1_Shown(object sender, EventArgs e)
{
if (!string.IsNullOrWhiteSpace(_scaffoldBuilder.ServerName))
{
var ops = new SqlServerDatabaseInformation(_scaffoldBuilder.ServerName);
var result = ops.DatabaseNames();
if (ops.IsSuccessFul)
{
DatabaseListBox.DataSource = result;
}
else
{
MessageBox.Show($"Error: {ops.LastExceptionMessage}");
}
}
}
private void GetDatabaseNamesButton_Click(object sender, EventArgs e)
{
if (ProviderComboBox.DataSource == null)
{
var dataProvider = new DatabaseProviders();
ProviderComboBox.DataSource = dataProvider.List;
}
if (!string.IsNullOrWhiteSpace(ServerNameTextBox.Text))
{
_scaffoldBuilder.ServerName = ServerNameTextBox.Text;
}
var ops = new SqlServerDatabaseInformation(_scaffoldBuilder.ServerName);
var result = ops.DatabaseNames();
if (ops.IsSuccessFul)
{
DatabaseListBox.DataSource = result;
}
else
{
MessageBox.Show($"Error: {ops.LastExceptionMessage}");
}
}
private void TableNamesButton_Click(object sender, EventArgs e)
{
GetTablesForSelectedDatabase();
}
private void GetTablesForSelectedDatabase()
{
if (DatabaseListBox.DataSource == null) return;
if (!string.IsNullOrWhiteSpace(ServerNameTextBox.Text))
{
_scaffoldBuilder.ServerName = ServerNameTextBox.Text;
}
var ops = new SqlServerDatabaseInformation(_scaffoldBuilder.ServerName);
ContextNameTextBox.Text = $"{DatabaseListBox.Text}Context";
var tableNames = ops.TableNames(DatabaseListBox.Text);
if (ops.IsSuccessFul == false)
{
MessageBox.Show($"Error getting tables\n{ops.LastExceptionMessage}");
return;
}
TablesCheckedListBox.DataSource = tableNames;
for (var index = 0; index < TablesCheckedListBox.Items.Count; index++)
{
TablesCheckedListBox.SetItemCheckState(index, CheckState.Checked);
}
}
private void DatabaseListBox_MouseDoubleClick(object sender, MouseEventArgs e)
{
GetTablesForSelectedDatabase();
}
/// <summary>
/// Create Scaffold script
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void GenerateButton_Click(object sender, EventArgs e)
{
if (DatabaseListBox.DataSource == null || TablesCheckedListBox.DataSource == null || TablesCheckedListBox.CheckedItems.Count <= 0 || string.IsNullOrWhiteSpace(StartupProjectTextBox.Text))
{
MessageBox.Show("Requires a database, one or more tables along with a startup folder!");
return;
}
ScriptTextBox.Text = "";
var configuration = new SqlServerScaffoldConfigurationItem
{
DatabaseName = DatabaseListBox.Text,
Provider = (DatabaseProvider)ProviderComboBox.SelectedItem,
TableNames = TablesCheckedListBox.CheckedItems.Cast<string>(),
ContextName = ContextNameTextBox.Text,
ContextDirectory = ContextFolderTextBox.Text,
FolderName = ModelFolderTextBox.Text,
StartupProject = StartupProjectTextBox.Text,
Switches =
{
Force = VerboseCheckBox.Checked,
Verbose = VerboseCheckBox.Checked,
UseDataAnnotations = DataAnnotationsCheckBox.Checked
}
};
if (!string.IsNullOrWhiteSpace(ServerNameTextBox.Text))
{
_scaffoldBuilder.ServerName = ServerNameTextBox.Text;
}
ScriptTextBox.Text = _scaffoldBuilder.Generate(configuration);
SaveApplicationSettings();
}
/// <summary>
/// Copy Scaffold script to windows clipboard
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void CopyToClipboardButton_Click(object sender, EventArgs e)
{
if (string.IsNullOrWhiteSpace(ScriptTextBox.Text)) return;
if (Question("Copy command to Windows Clipboard?"))
{
Clipboard.SetText(ScriptTextBox.Text);
}
}
/// <summary>
/// Get base project names for a selected solution
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void SolutionButton_Click(object sender, EventArgs e)
{
if (SolutionFileDialog1.ShowDialog() == DialogResult.OK)
{
var ops = new SolutionHelper();
var results = ops.ProjectNames(SolutionFileDialog1.FileName);
if (ops.IsSuccessFul)
{
var f = new ProjectNamesForm(results);
if (f.ShowDialog() == DialogResult.OK)
{
StartupProjectTextBox.Text = f.ProjectName;
}
}
else
{
MessageBox.Show(ops.LastExceptionMessage);
}
}
}
private async void ServerButton_Click(object sender, EventArgs e)
{
var columnName = _scaffoldBuilder.ServerName.ToLower().Contains("sqlexpress") ? "InstanceName" : "ServerName";
var ops = new SqlServerUtilities();
var serverDataTable = await ops.SqlServerInstances().ConfigureAwait(false);
var serverNameList = serverDataTable.AsEnumerable()
.Where(row => !string.IsNullOrWhiteSpace(row.Field<string>(columnName)))
.Select(row => row.Field<string>(columnName)).ToList();
var serverForm = new ServersForm(serverNameList);
if (serverForm.ShowDialog() == DialogResult.OK)
{
if (string.IsNullOrWhiteSpace(serverForm.ServerName)) return;
ServerNameTextBox.Invoke(serverForm.ServerName == "SQLEXPRESS"
? new Action(() => ServerNameTextBox.Text = $@".\{serverForm.ServerName}")
: new Action(() => ServerNameTextBox.Text = $"{serverForm.ServerName}"));
SaveApplicationSettings();
}
}
private void ExitButton_Click(object sender, EventArgs e)
{
Close();
}
}
}