forked from DevExpress/DevExtreme.AspNet.Data
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSqlServerTestDbHelper.cs
More file actions
51 lines (39 loc) · 1.56 KB
/
Copy pathSqlServerTestDbHelper.cs
File metadata and controls
51 lines (39 loc) · 1.56 KB
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
using System.Data.SqlClient;
using System.IO;
using System.Reflection;
namespace DevExtreme.AspNet.Data.Tests {
public class SqlServerTestDbHelper {
const string LOCAL_DB = "(localdb)\\MSSQLLocalDB";
readonly string _dbName;
readonly string _dbFilePath;
public SqlServerTestDbHelper(string id) {
_dbName = $"DevExtreme_AspNet_Data_Tests_{id}_DB";
_dbFilePath = Path.Combine(
Path.GetDirectoryName(typeof(SqlServerTestDbHelper).GetTypeInfo().Assembly.Location),
_dbName + ".mdf"
);
}
public string ConnectionString {
get { return $"Data Source={LOCAL_DB}; AttachDbFileName={_dbFilePath}; Initial Catalog={_dbName}"; }
}
public void ResetDatabase() {
// Possibly related: https://stackoverflow.com/a/46142857
using(var conn = new SqlConnection($"Data Source={LOCAL_DB}")) {
conn.Open();
void Exec(string sql) {
#pragma warning disable DX0024 // this is assembly with tests
using(var cmd = conn.CreateCommand()) {
cmd.CommandText = sql;
cmd.ExecuteNonQuery();
}
#pragma warning restore DX0024 // this is assembly with tests
}
try {
Exec($"drop database [{_dbName}]");
} catch {
}
Exec($"create database [{_dbName}] on (name='{_dbName}', filename='{_dbFilePath}')");
}
}
}
}