/
DataOperations.cs
406 lines (315 loc) · 13.3 KB
/
DataOperations.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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using LocalDbLibrary.Models;
namespace LocalDbLibrary.Classes
{
/// <summary>
/// Code to create a new localDb SQL-Server database which is placed under the
/// current executable folder, one level done in a folder named Data.
///
/// <see cref="DetachDatabase"/> is only needed if there is a need to recreate the
/// database during development.
/// </summary>
public class DataOperations
{
/// <summary>
/// Folder where database will reside
/// </summary>
public const string DB_DIRECTORY = "Data";
/// <summary>
/// Name of database
/// </summary>
public const string APP_DATA_DB_NAME = "AppData";
public const string NORTH_DB_NAME = "NorthWind2020";
/// <summary>
/// Connection string for <see cref="APP_DATA_DB_NAME"/>
/// </summary>
private const string _connectionStringAppData =
"Data Source=(LocalDB)\\MSSQLLocalDB;Initial Catalog=" + APP_DATA_DB_NAME + ";Integrated Security=True";
private const string _connectionStringNorthWind =
"Data Source=(LocalDB)\\MSSQLLocalDB;Initial Catalog=" + NORTH_DB_NAME + ";Integrated Security=True";
private const string _connectionStringMaster =
"Data Source=(LocalDB)\\MSSQLLocalDB;Initial Catalog=master;Integrated Security=True";
public static bool DatabaseFolderExists => Directory.Exists(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, DataOperations.DB_DIRECTORY));
public static string DatabaseFolder => Path.Combine(AppDomain.CurrentDomain.BaseDirectory, DataOperations.DB_DIRECTORY);
/// <summary>
/// Validate the database exists
/// </summary>
/// <returns></returns>
/// <remarks>
/// Or call this
/// DECLARE @DbName AS nvarchar(50) = 'AppData'
/// SELECT name FROM sys.databases WHERE Name = @DbName
/// </remarks>
public static async Task<bool> AppDataDatabaseExists()
{
var databaseNames = await LocalDatabaseNames();
return databaseNames.Any(name => name == APP_DATA_DB_NAME);
}
/// <summary>
/// Get list of database names
/// </summary>
/// <returns></returns>
public static async Task<List<string>> LocalDatabaseNames()
{
List<string> list = new();
return await Task.Run(async () =>
{
await using var cn = new SqlConnection(_connectionStringMaster);
await using var cmd = new SqlCommand("SELECT name FROM sys.databases WHERE Name NOT IN ('master','model','msdb') ORDER BY Name;", cn);
cn.Open();
var reader = await cmd.ExecuteReaderAsync();
while (reader.Read())
{
list.Add(reader.GetString(0));
}
return list;
});
}
#region Read operations
/// <summary>
/// Read Customer data into a <see cref="DataTable"/>
/// </summary>
public static DataTable ReadAppDataDataTable()
{
DataTable table = new ();
using var cn = new SqlConnection(_connectionStringAppData);
using var cmd = new SqlCommand($"SELECT Identifier, CompanyName, ContactName FROM Customer", cn);
cn.Open();
table.Load(cmd.ExecuteReader());
return table;
}
/// <summary>
/// Read <see cref="Customer"/> into a list
/// </summary>
public static List<Customer> ReadAppDataList()
{
List<Customer> list = new();
using var cn = new SqlConnection(_connectionStringAppData);
using var cmd = new SqlCommand($"SELECT Identifier, CompanyName, ContactName FROM Customer", cn);
cn.Open();
var reader = cmd.ExecuteReader();
while (reader.Read())
{
list.Add(new Customer()
{
Identifier = reader.GetInt32(0),
CompanyName = reader.GetString(1),
ContactName = reader.GetString(2)
});
}
return list;
}
#endregion
/// <summary>
/// Add a few customer records to present
/// </summary>
private static void PopulateCustomers()
{
List<string> insertList = new()
{
"INSERT INTO Customer (CompanyName,ContactName) VALUES ('Alfreds Futterkiste', 'Maria Anders')",
"INSERT INTO Customer (CompanyName,ContactName) VALUES ('Princesa Isabel Vinhos', 'Isabel de Castro')",
"INSERT INTO Customer (CompanyName,ContactName) VALUES ('Bottom-Dollar Markets', 'Elizabeth Lincoln')"
};
using var cn = new SqlConnection( _connectionStringAppData );
using var cmd = new SqlCommand() { Connection = cn };
cn.Open();
foreach (var customer in insertList)
{
cmd.CommandText = customer;
cmd.ExecuteNonQuery();
}
}
public static List<Product> NorthWindProducts()
{
List<Product> list = new();
//_connectionStringNorthWind
using var cn = new SqlConnection(_connectionStringNorthWind);
using var cmd = new SqlCommand() { Connection = cn };
cmd.CommandText =
"SELECT P.ProductID, P.ProductName, S.CompanyName " +
"FROM Products AS P INNER JOIN Suppliers AS S ON P.SupplierID = S.SupplierID " +
"WHERE P.CategoryID = 2 AND P.ProductID < 9";
cn.Open();
var reader = cmd.ExecuteReader();
while (reader.Read())
{
list.Add(new Product() {ProductID = reader.GetInt32(0), ProductName = reader.GetString(1), SupplierName = reader.GetString(2)});
}
return list;
}
public static async Task<(SqlResult result, Exception exception)> CreateNorthWindDatabase()
{
string dbName = "NorthWind2020";
// ReSharper disable once AssignNullToNotNullAttribute
string outputFolder = @"C:\OED\Dotnetland\SqlServerLocalDatabases";
if (!Directory.Exists(outputFolder))
{
Directory.CreateDirectory(outputFolder);
}
string mdfFilename = $"{dbName}.mdf";
string dbFileName = Path.Combine(outputFolder, mdfFilename);
if (File.Exists(dbFileName))
{
return (SqlResult.AlreadyExists, null);
}
string createTablesCommand = await File.ReadAllTextAsync(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Scripts", "NorthWindCreateTables.sql"));
try
{
// ReSharper disable once UseAwaitUsing
using var cn = new SqlConnection(_connectionStringMaster);
// ReSharper disable once UseAwaitUsing
using var cmd = new SqlCommand($"CREATE DATABASE {dbName} ON (NAME = N'{dbName}', FILENAME = '{dbFileName}')", cn);
cn.Open();
cmd.ExecuteNonQuery();
await Task.Delay(1000);
cn.Close();
cn.ConnectionString = _connectionStringNorthWind;
cmd.CommandText = createTablesCommand;
cn.Open();
cmd.ExecuteNonQuery();
string populateTablesCommand = await File.ReadAllTextAsync(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Scripts", "NorthWindPopulateTables.sql"));
cmd.CommandText = populateTablesCommand;
cmd.ExecuteNonQuery();
return (SqlResult.Success, null);
}
catch (Exception error)
{
return (SqlResult.Failed, error);
}
}
public static void DropNorthWindDatabase()
{
string dbName = "NorthWind2020";
using var cn = new SqlConnection(_connectionStringMaster);
using var cmd = new SqlCommand();
var sqlCommandText = $"ALTER DATABASE {dbName} SET SINGLE_USER WITH ROLLBACK IMMEDIATE;DROP DATABASE [{dbName}]";
cmd.Connection = cn;
cmd.CommandText = sqlCommandText;
try
{
cn.Open();
cmd.ExecuteNonQuery();
Debug.WriteLine("Dropped");
}
catch (Exception exception)
{
//Debug.WriteLine($"Well we failed with\n{exception.Message}");
}
}
/// <summary>
/// Create database in Data folder under the application folder
/// ASSUMES Data folder exists
///
/// In the calling project there is a post build event to create the folder if not exists
/// </summary>
/// <returns></returns>
public static async Task<bool> CreateDatabase()
{
string dbName = APP_DATA_DB_NAME;
// ReSharper disable once AssignNullToNotNullAttribute
string outputFolder = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), DB_DIRECTORY);
if (!Directory.Exists(outputFolder))
{
Directory.CreateDirectory(outputFolder);
}
string mdfFilename = $"{dbName}.mdf";
string dbFileName = Path.Combine(outputFolder, mdfFilename);
string logFileName = Path.Combine(outputFolder, $"{dbName}_log.ldf");
string createTableCommand = @"
CREATE TABLE [dbo].[Customer](
[Identifier] [int] IDENTITY(1,1) NOT NULL,
[CompanyName] [nvarchar](255) NULL,
[ContactName] [nvarchar](255) NULL,
CONSTRAINT [Customer$PrimaryKey] PRIMARY KEY CLUSTERED
(
[Identifier] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
";
try
{
if (File.Exists(dbFileName))
{
if (await DetachDatabase(dbFileName, logFileName) == false)
{
return false;
}
}
// ReSharper disable once UseAwaitUsing
using var cn = new SqlConnection(_connectionStringMaster);
// ReSharper disable once UseAwaitUsing
using var cmd = new SqlCommand($"CREATE DATABASE {dbName} ON (NAME = N'{dbName}', FILENAME = '{dbFileName}')", cn);
cn.Open();
cmd.ExecuteNonQuery();
cn.Close();
cn.ConnectionString = _connectionStringAppData;
cmd.CommandText = createTableCommand;
cn.Open();
cmd.ExecuteNonQuery();
cn.Close();
await Task.Delay(500);
PopulateCustomers();
return true;
}
catch (Exception)
{
return false;
}
}
/// <summary>
/// Drop database and delete physical files
/// </summary>
/// <param name="dbName">database name including path</param>
/// <param name="logFile">log name including path</param>
/// <returns>success</returns>
public static async Task<bool> DetachDatabase(string dbName, string logFile)
{
return await Task.Run(async () =>
{
try
{
if (File.Exists(dbName))
{
// ReSharper disable once UseAwaitUsing
using var cn = new SqlConnection(_connectionStringMaster);
cn.Open();
var cmd = cn.CreateCommand();
cmd.CommandText =
$"ALTER DATABASE {Path.GetFileNameWithoutExtension(dbName)}" +
" SET SINGLE_USER WITH ROLLBACK IMMEDIATE";
await cmd.ExecuteNonQueryAsync();
cmd.CommandText = $"exec sp_detach_db '{Path.GetFileNameWithoutExtension(dbName)}'";
await cmd.ExecuteNonQueryAsync();
cn.Close();
if (File.Exists(dbName))
{
File.Delete(dbName);
}
if (File.Exists(logFile))
{
File.Delete(logFile);
}
return true;
}
else
{
return true;
}
}
catch
{
return false;
}
});
}
}
}