-
Notifications
You must be signed in to change notification settings - Fork 3
/
DataConnectionDialog.cs
286 lines (253 loc) · 10.5 KB
/
DataConnectionDialog.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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Security.Principal;
namespace com.rusanu.dataconnectiondialog
{
/// <summary>
/// SQL Server connection dialog
/// </summary>
public partial class DataConnectionDialog : Form
{
/// <summary>
/// The connection string builder
/// </summary>
public SqlConnectionStringBuilder ConnectionStringBuilder { get; private set; }
/// <summary>
/// Internal class for form/connection properties
/// </summary>
internal class Properties : INotifyPropertyChanged
{
/// <summary>
/// Strings used in code
/// </summary>
internal class Tags
{
public static readonly string DataSource = "DataSource";
public static readonly string UserName = "UserName";
public static readonly string Password = "Password";
public static readonly string IntegratedSecurity = "IntegratedSecurity";
public static readonly string DataSourceValid = "DataSourceValid";
public static readonly string UserNameEnabled = "UserNameEnabled";
public static readonly string WindowsAuthentication = "Windows Authentication";
public static readonly string SQLServerAuthentication = "SQL Server Authentication";
public static readonly string AuthenticationMode = "AuthenticationMode";
public static readonly string TestingEnabled = "TestingEnabled";
public static readonly string TestResult = "TestResult";
};
/// <summary>
/// From INotifyPropertyChanged
/// </summary>
public event PropertyChangedEventHandler PropertyChanged;
/// <summary>
/// The current connection string builder
/// </summary>
public SqlConnectionStringBuilder ConnectionStringBuilder { get; private set; }
/// <summary>
/// ConnectionStreingBuilder.DataSource + change notifications
/// </summary>
public string DataSource
{
get { return ConnectionStringBuilder.DataSource; }
set
{
ConnectionStringBuilder.DataSource = value;
NotifyPropertyChanged(Tags.DataSource);
NotifyPropertyChanged(Tags.DataSourceValid);
NotifyPropertyChanged(Tags.TestingEnabled);
TestResult = String.Empty;
}
}
/// <summary>
/// True when the data source is not empty (for Enabled data binding)
/// </summary>
public bool DataSourceValid
{
get { return !string.IsNullOrEmpty(DataSource); }
}
/// <summary>
/// ConnectionStringBuilder.IntegratedSecurity + change notifications
/// </summary>
public bool IntegratedSecurity
{
get { return ConnectionStringBuilder.IntegratedSecurity; }
set
{
if (value != ConnectionStringBuilder.IntegratedSecurity)
{
ConnectionStringBuilder.IntegratedSecurity = value;
NotifyPropertyChanged(Tags.IntegratedSecurity);
NotifyPropertyChanged(Tags.UserNameEnabled);
NotifyPropertyChanged(Tags.AuthenticationMode);
TestResult = String.Empty;
}
}
}
/// <summary>
/// ConnectionStringBuilder.UserID + change notifications
/// For IntegratedSecurity it returns current Windows identity
/// </summary>
public string UserName
{
get { return IntegratedSecurity ? WindowsUserName : ConnectionStringBuilder.UserID; }
set
{
ConnectionStringBuilder.UserID = value;
NotifyPropertyChanged(Tags.UserName);
TestResult = String.Empty;
}
}
/// <summary>
/// ConnectionStringBuilder.Password + change notifications
/// For IntegratedSecurity will return ""
/// </summary>
public string Password
{
get { return IntegratedSecurity ? String.Empty : ConnectionStringBuilder.Password; }
set
{
ConnectionStringBuilder.Password = value;
NotifyPropertyChanged(Tags.Password);
TestResult = String.Empty;
}
}
/// <summary>
/// True when not ConnectionStringBuilder.IntegratedSecurity (for Enabled binding)
/// </summary>
public bool UserNameEnabled
{
get { return !IntegratedSecurity; }
}
/// <summary>
/// ConnectionStringBuilder.IntegratedSecurity mapped to values for the combo data binding
/// </summary>
public string AuthenticationMode
{
get { return IntegratedSecurity ? Tags.WindowsAuthentication : Tags.SQLServerAuthentication; }
set
{
IntegratedSecurity = (value == Tags.WindowsAuthentication);
}
}
private bool _isTesting = false;
/// <summary>
/// Set to true when the connection is being tested
/// </summary>
public bool IsTesting
{
get { return _isTesting; }
set
{
_isTesting = value;
NotifyPropertyChanged(Tags.TestingEnabled);
}
}
/// <summary>
/// True when Testing is allowed (for Rnabled binding)
/// </summary>
public bool TestingEnabled
{
get { return DataSourceValid && !IsTesting; }
}
private string _testResult = "";
/// <summary>
/// Connection test result
/// </summary>
public string TestResult
{
get {return _testResult;}
set
{
_testResult = value;
NotifyPropertyChanged(Tags.TestResult);
}
}
/// <summary>
/// The current Windows user, cached
/// </summary>
internal string WindowsUserName { get; set; }
/// <summary>
/// Notification for INotifyPropertyChanged
/// </summary>
/// <param name="propertyName"></param>
internal void NotifyPropertyChanged(string propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
/// <summary>
/// CTOR
/// </summary>
/// <param name="scsb">Preconfigured SqlConnectionStringBuilder</param>
public Properties(SqlConnectionStringBuilder scsb)
{
ConnectionStringBuilder = scsb;
WindowsUserName = WindowsIdentity.GetCurrent().Name;
}
}
/// <summary>
/// The current properties
/// </summary>
private Properties FormProperties { get; set; }
/// <summary>
/// CTOR
/// </summary>
/// <param name="connectionStringBuilder">The preconfigured connection string builder</param>
public DataConnectionDialog(SqlConnectionStringBuilder connectionStringBuilder)
{
InitializeComponent();
authenticationMode.Items.Add(Properties.Tags.WindowsAuthentication);
authenticationMode.Items.Add(Properties.Tags.SQLServerAuthentication);
ConnectionStringBuilder = connectionStringBuilder ?? new SqlConnectionStringBuilder()
{
IntegratedSecurity = true,
DataSource = "."
};
FormProperties = new Properties(ConnectionStringBuilder);
serverName.DataBindings.Add("Text", FormProperties, Properties.Tags.DataSource, false, DataSourceUpdateMode.OnPropertyChanged);
userName.DataBindings.Add("Text", FormProperties, Properties.Tags.UserName);
userPassword.DataBindings.Add("Text", FormProperties, Properties.Tags.Password);
authenticationMode.DataBindings.Add("Text", FormProperties, Properties.Tags.AuthenticationMode, false, DataSourceUpdateMode.OnPropertyChanged);
userName.DataBindings.Add("Enabled", FormProperties, Properties.Tags.UserNameEnabled);
userPassword.DataBindings.Add("Enabled", FormProperties, Properties.Tags.UserNameEnabled);
lblUserName.DataBindings.Add("Enabled", FormProperties, Properties.Tags.UserNameEnabled);
lblPassword.DataBindings.Add("Enabled", FormProperties, Properties.Tags.UserNameEnabled);
btnConnect.DataBindings.Add("Enabled", FormProperties, Properties.Tags.DataSourceValid);
btnTest.DataBindings.Add("Enabled", FormProperties, Properties.Tags.TestingEnabled);
lblStatus.DataBindings.Add("Text", FormProperties, Properties.Tags.TestResult);
}
internal void TestConnection()
{
using (SqlConnection conn = new SqlConnection(ConnectionStringBuilder.ConnectionString))
{
try
{
FormProperties.IsTesting = true;
FormProperties.TestResult = "Testing...";
conn.Open();
FormProperties.TestResult = "Success!";
}
catch (Exception ex)
{
FormProperties.TestResult = ex.Message;
}
finally
{
FormProperties.IsTesting = false;
}
}
}
private void btnTest_Click(object sender, EventArgs e)
{
TestConnection();
}
}
}