-
Notifications
You must be signed in to change notification settings - Fork 0
/
WTConnection.cs
143 lines (124 loc) · 4.75 KB
/
WTConnection.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
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Data.SqlClient;
using System.Windows;
using WTUSA;
namespace WTUSA
{
/// <summary>
/// A singleton that keeps a single connection to the WTData database.
/// Queries should be performed via dapper and its' IDbConnection.Query<...>() extension.
/// </summary>
public sealed class WTConnection : System.Data.IDbConnection
{
private static readonly WTConnection _instance = new WTConnection();
private SqlConnection WTSQLConnection;
//! private SqlCommand sc;
private WTRegistry WTReg;
public static WTConnection GetConnection() { return _instance; }
public static SqlCommand GetCommand() { return _instance.WTSQLConnection.CreateCommand(); }
private WTConnection()
{
WTReg = new WTRegistry();
string connectionString = "";
SqlConnectionStringBuilder csb = new SqlConnectionStringBuilder();
try
{
connectionString = System.Text.Encoding.ASCII.GetString(WTReg.GetBytesFromRegistry());
csb = new SqlConnectionStringBuilder(connectionString);
csb.MultipleActiveResultSets = true;
csb.AsynchronousProcessing = true;
WTSQLConnection = new SqlConnection(csb.ConnectionString);
}
catch (Exception)
{
//! TODO: Handle this!!!
//throw;
var msgResult =
MessageBox.Show(
"No valid WinTool SQL Connection was found." + Environment.NewLine +
"Show configuration screen?", "Error!", MessageBoxButton.YesNo);
if (msgResult == MessageBoxResult.Yes)
{
frmSelectSQLServer frmSqlSelection = new frmSelectSQLServer();
while (WTSQLConnection == null)
{
frmSqlSelection.ShowDialog();
csb = new SqlConnectionStringBuilder();
csb.MultipleActiveResultSets = true;
csb.AsynchronousProcessing = true;
csb.DataSource = frmSqlSelection.cboxServers.SelectedItem.ToString();
csb.IntegratedSecurity = frmSqlSelection.radioWinLogin.Checked;
csb.InitialCatalog = "WTData";
WTSQLConnection = new SqlConnection(csb.ConnectionString);
}
}
else
{
throw new System.TimeoutException("Could not connect to the database.");
}
}
//nothing was in the registry, that's bad
//Let's find from the user how to create connection
WTSQLConnection.Open();
(new WTRegistry()).WriteBytesToRegistry(csb);
}
#region "IDBConnection Support"
IDbTransaction IDbConnection.BeginTransaction(IsolationLevel il)
{
return WTConnection._instance.WTSQLConnection.BeginTransaction(il);
}
IDbTransaction IDbConnection.BeginTransaction()
{
return WTConnection._instance.WTSQLConnection.BeginTransaction();
}
void IDbConnection.ChangeDatabase(string databaseName)
{
WTConnection._instance.WTSQLConnection.ChangeDatabase(databaseName);
}
void IDbConnection.Close()
{
WTConnection._instance.WTSQLConnection.Close();
}
string IDbConnection.ConnectionString
{
get
{
return WTConnection._instance.WTSQLConnection.ConnectionString;
}
set
{
WTConnection._instance.WTSQLConnection.ConnectionString = value;
}
}
int IDbConnection.ConnectionTimeout
{
get { return WTConnection._instance.WTSQLConnection.ConnectionTimeout; }
}
IDbCommand IDbConnection.CreateCommand()
{
return WTConnection._instance.WTSQLConnection.CreateCommand();
}
string IDbConnection.Database
{
get { return WTConnection._instance.WTSQLConnection.Database; }
}
void IDbConnection.Open()
{
WTConnection._instance.WTSQLConnection.Open();
}
ConnectionState IDbConnection.State
{
get { return WTConnection._instance.WTSQLConnection.State; }
}
void IDisposable.Dispose()
{
WTConnection._instance.WTSQLConnection.Dispose();
}
#endregion
}
}