-
Notifications
You must be signed in to change notification settings - Fork 3
/
SqlConnectionDialog.cs
178 lines (152 loc) · 5.33 KB
/
SqlConnectionDialog.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
/*
* LICENSE: https://github.com/BeginTry/XEventHandler/blob/master/LICENSE
* GitHub repository: https://github.com/BeginTry/XEventHandler
*/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace XEventHandler
{
public partial class SqlConnectionDialog : Form
{
SqlConnection conn = new SqlConnection();
SqlConnectionStringBuilder csb;
public SqlConnectionStringBuilder ConnectionStringBuilder
{
get { return csb; }
set { csb = value; }
}
private enum AuthenticationType
{
WindowsAuth = 0,
SqlAuth = 1,
}
public SqlConnectionDialog()
{
InitializeComponent();
}
private void SqlConnectionDialog_Load(object sender, EventArgs e)
{
cboAuthentication.SelectedIndex = 0;
if (csb == null)
{
csb = new SqlConnectionStringBuilder();
}
}
private void btnTest_Click(object sender, EventArgs e)
{
if (ValidConnection())
{
MessageBox.Show("Connection succeeded.", "", MessageBoxButtons.OK);
}
}
private void btnOK_Click(object sender, EventArgs e)
{
if (ValidConnection())
{
this.DialogResult = DialogResult.OK;
this.Close();
}
}
private void btnCancel_Click(object sender, EventArgs e)
{
this.DialogResult = DialogResult.Cancel;
this.Close();
}
private bool FormValidated()
{
if (string.IsNullOrEmpty(txtServerName.Text))
{
MessageBox.Show("Server Name must be specified.", "", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return false;
}
else if (cboAuthentication.SelectedIndex == (int)AuthenticationType.SqlAuth &&
string.IsNullOrEmpty(txtLoginName.Text))
{
MessageBox.Show("Login Name must be specified.", "", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return false;
}
return true;
}
private void cboAuthentication_SelectedIndexChanged(object sender, EventArgs e)
{
txtLoginName.Enabled = cboAuthentication.SelectedIndex == (int)AuthenticationType.SqlAuth;
txtPassword.Enabled = cboAuthentication.SelectedIndex == (int)AuthenticationType.SqlAuth;
lblUser.Enabled = cboAuthentication.SelectedIndex == (int)AuthenticationType.SqlAuth;
lblPassword.Enabled = cboAuthentication.SelectedIndex == (int)AuthenticationType.SqlAuth;
if (cboAuthentication.SelectedIndex == (int)AuthenticationType.WindowsAuth)
{
lblUser.Text = "User Name:";
txtLoginName.Text = Environment.UserName;
}
else if (cboAuthentication.SelectedIndex == (int)AuthenticationType.SqlAuth)
{
lblUser.Text = "Login Name:";
txtLoginName.Text = "";
}
}
private bool ValidConnection()
{
if (!FormValidated())
return false;
bool retVal;
this.Cursor = Cursors.WaitCursor;
csb = new SqlConnectionStringBuilder();
csb.DataSource = txtServerName.Text;
csb.InitialCatalog = "master";
csb.ConnectTimeout = 3;
if (cboAuthentication.SelectedIndex == (int)AuthenticationType.WindowsAuth)
{
csb.IntegratedSecurity = true;
}
else if (cboAuthentication.SelectedIndex == (int)AuthenticationType.SqlAuth)
{
csb.IntegratedSecurity = false;
csb.UserID = txtLoginName.Text;
csb.Password = txtPassword.Text;
}
try
{
conn.ConnectionString = csb.ConnectionString;
conn.Open();
conn.Close();
retVal = true;
}
catch (Exception ex)
{
retVal = false;
MessageBox.Show(ex.ToString(), "Exception", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
this.Cursor = Cursors.Default;
}
return retVal;
}
public static bool ValidConnection(SqlConnectionStringBuilder csb)
{
//No need to wait the full 15 seconds (default behavior).
csb.ConnectTimeout = 3;
using (SqlConnection cn = new SqlConnection())
{
cn.ConnectionString = csb.ConnectionString;
try
{
cn.Open();
}
catch
{
return false;
}
cn.Close();
}
return true;
}
}
}