forked from sergiisyrovatchenko/SQLIndexManager
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConnectionBox.cs
More file actions
235 lines (190 loc) · 6 KB
/
Copy pathConnectionBox.cs
File metadata and controls
235 lines (190 loc) · 6 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
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
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Controls;
using SQLIndexManager.Properties;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Windows.Forms;
namespace SQLIndexManager {
public partial class ConnectionBox : XtraForm {
public ConnectionBox() {
InitializeComponent();
boxServer.Properties.Items.AddRange(Settings.Hosts.Select(p => p.Server).ToList());
boxServer.SelectedIndex = 0;
}
public Host GetHost() {
return new Host {
Server = _server,
AuthType = _authType,
User = _user,
Password = _password,
SavePassword = _savePassword,
Databases = _databases,
IsUserConnection = true,
ServerInfo = _serverInfo
};
}
#region Connection
private ThreadWorker _worker;
private void TryOpenConnection() {
UpdateControlUsage(false);
_worker = new ThreadWorker();
_worker.DoWork += OpenConnection;
_worker.RunWorkerCompleted += CheckConnection;
_worker.RunWorkerAsync();
}
private void CancelConnection() {
if (_worker != null && _worker.IsBusy) {
_worker.RunWorkerCompleted -= CheckConnection;
_worker.Abort();
_worker.Dispose();
UpdateControlUsage(true);
boxServer.Focus();
}
else {
DialogResult = DialogResult.Cancel;
}
}
private void OpenConnection(object sender, DoWorkEventArgs e) {
SqlConnection connection = Connection.Create(GetHost());
connection.Open();
e.Result = connection;
}
private void CheckConnection(object sender, RunWorkerCompletedEventArgs e) {
UpdateControlUsage(true);
if (e.Error != null) {
string errorMsg = e.Error.Message.Replace(". ", "." + Environment.NewLine);
Output.Current.Add("Error", errorMsg);
XtraMessageBox.Show(this, errorMsg, e.Error.Source, MessageBoxButtons.OK, MessageBoxIcon.Error);
boxServer.Focus();
return;
}
using (SqlConnection connection = (SqlConnection)e.Result) {
try {
_serverInfo = QueryEngine.GetServerInfo(connection);
}
catch (Exception ex) {
string errorMsg = ex.Message.Replace(". ", "." + Environment.NewLine);
Output.Current.Add("Error", errorMsg);
XtraMessageBox.Show(errorMsg, ex.Source, MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
finally {
connection.Close();
}
}
if (_serverInfo.MajorVersion < 10) {
XtraMessageBox.Show(Resources.MinVersionMessage, "", MessageBoxButtons.OK, MessageBoxIcon.Warning);
boxServer.Focus();
}
else {
DialogResult = DialogResult.OK;
}
}
#endregion
#region Properties
private string _server {
get { return boxServer.Text; }
set { boxServer.Text = value; }
}
private AuthTypes _authType {
get { return (AuthTypes)boxAuthType.SelectedIndex; }
set { boxAuthType.SelectedIndex = (int)value; }
}
private string _user {
get { return boxUser.Text; }
set { boxUser.Text = value; }
}
private string _password {
get { return boxPassword.Text; }
set { boxPassword.Text = value; }
}
private bool _savePassword {
get { return boxSavePassword.Checked; }
set { boxSavePassword.Checked = value; }
}
private List<string> _databases;
private ServerInfo _serverInfo;
#endregion
#region Controls
private void ButtonOkClick(object sender, EventArgs e) {
TryOpenConnection();
}
private void ButtonCancelClick(object sender, EventArgs e) {
CancelConnection();
}
private void BoxServerPropertiesClick(object sender, ButtonPressedEventArgs e) {
if (e.Button.Kind == ButtonPredefines.Search) {
using (NetworkBox form = new NetworkBox()) {
DialogResult dr = form.ShowDialog(this);
if (dr == DialogResult.OK) {
string host = form.GetNetworkHost();
if (!string.IsNullOrEmpty(host)) {
boxServer.Text = host;
}
}
}
}
else {
((ComboBoxEdit)sender).ShowPopup();
}
}
private void BoxServerSelectionChanged(object sender, EventArgs e) {
if (boxServer.SelectedIndex != -1) {
Host host = Settings.Hosts[boxServer.SelectedIndex];
_server = host.Server;
_authType = host.AuthType;
_user = host.User;
_password = host.Password;
_savePassword = host.SavePassword;
_databases = host.Databases;
}
}
private void BoxAuthTypeSelectionChanged(object sender, EventArgs e) {
bool sqlAuth = (_authType == AuthTypes.SqlServer);
boxUser.Enabled =
boxPassword.Enabled =
boxSavePassword.Enabled = sqlAuth;
if (!sqlAuth) {
_user = _password = null;
_savePassword = false;
}
else {
_user = "sa";
}
}
private void UpdateControlUsage(bool enabled) {
foreach(Control c in this.Controls) {
c.Enabled = enabled;
}
progressBar.Visible = !enabled;
buttonCancel.Enabled = true;
if (enabled) {
boxUser.Enabled =
boxPassword.Enabled =
boxSavePassword.Enabled = (_authType == AuthTypes.SqlServer);
}
}
private void EditValueChanged(object sender, EventArgs e) {
buttonOK.Enabled =
!string.IsNullOrEmpty(_server) && !(string.IsNullOrEmpty(_user) && _authType == AuthTypes.SqlServer);
}
#endregion
#region Override Methods
protected override bool ProcessDialogKey(Keys keyData) {
if (keyData == Keys.Escape) {
CancelConnection();
return true;
}
if (keyData == Keys.Enter && buttonOK.Enabled) {
TryOpenConnection();
return true;
}
return base.ProcessDialogKey(keyData);
}
#endregion
}
}