-
Notifications
You must be signed in to change notification settings - Fork 1
/
DialogWindowAddDatabase.xaml.cs
230 lines (184 loc) · 7.29 KB
/
DialogWindowAddDatabase.xaml.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
// This is an open source non-commercial project. Dear PVS-Studio, please check it.
// PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
namespace SCADA
{
/// <summary>
/// Логика взаимодействия для DialogWindowAddDatabase.xaml
/// </summary>
public partial class DialogWindowAddDatabase : Window
{
public Popup PopupMessage = new Popup();
public Label LPopupMessage = new Label();
public ComboBox CBDatabases;
public string ServerName = "";
public bool SSPI;
public string Password = "";
public string UserName = "";
public DialogWindowAddDatabase()
{
InitializeComponent();
LPopupMessage = new Label();
LPopupMessage.BorderThickness = new Thickness(1);
LPopupMessage.BorderBrush = Brushes.Red;
LPopupMessage.Background = Brushes.White;
PopupMessage.AllowsTransparency = true;
PopupMessage.Child = LPopupMessage;
PopupMessage.PopupAnimation = PopupAnimation.Fade;
PopupMessage.StaysOpen = false;
MenuItem menuItemPasteDatabaseName = new MenuItem();
menuItemPasteDatabaseName.Command = ApplicationCommands.Paste;
MenuItem menuItemCopyDatabaseName = new MenuItem();
menuItemCopyDatabaseName.Command = ApplicationCommands.Copy;
ContextMenu ContextMenuDatabaseName = new System.Windows.Controls.ContextMenu();
ContextMenuDatabaseName.Items.Add(menuItemPasteDatabaseName);
ContextMenuDatabaseName.Items.Add(menuItemCopyDatabaseName);
TBNameDatabase.CommandBindings.Add(new CommandBinding(ApplicationCommands.Cut, IgnoreCut));
TBNameDatabase.CommandBindings.Add(new CommandBinding(ApplicationCommands.Paste, TextBoxPaste));
TBNameDatabase.ContextMenu = ContextMenuDatabaseName;
TBNameDatabase.PreviewKeyDown += TextBox_PreviewKeyDown;
TBNameDatabase.PreviewTextInput += TextBox_PreviewTextInput;
}
void TextBox_PreviewTextInput(object sender, TextCompositionEventArgs e)
{
TextBox tb = (TextBox)sender;
if (tb.Text.Length + 1 > 40)
{
if (tb.SelectionLength > 0)
{
if ((tb.Text.Length + 1) - tb.SelectionLength <= 40)
{
return;
}
}
if (tb == TBNameDatabase)
{
LPopupMessage.Content = "Имя базы данных не может быть длинее 40 символов.";
}
PopupMessage.PlacementTarget = tb;
PopupMessage.IsOpen = true;
e.Handled = true;
}
}
private void IgnoreCut(object sender, ExecutedRoutedEventArgs e)
{
e.Handled = true;
}
private void TextBoxPaste(object sender, ExecutedRoutedEventArgs e)
{
TextBox tb = (TextBox)sender;
string s;
if (tb.SelectionLength > 0)
{
s = tb.Text.Remove(tb.SelectionStart, tb.SelectionLength);
s = s.Insert(tb.SelectionStart, Clipboard.GetText());
}
else
{
s = tb.Text.Insert(tb.Text.Length, Clipboard.GetText());
}
if (s.Length > 40)
{
if (tb == TBNameDatabase)
{
LPopupMessage.Content = "Имя базы данных не может быть длинее 40 символов.";
}
PopupMessage.PlacementTarget = tb;
PopupMessage.IsOpen = true;
e.Handled = true;
}
else
{
tb.Paste();
}
}
void TextBox_PreviewKeyDown(object sender, KeyEventArgs e)
{
TextBox tb = (TextBox)sender;
if (tb.Text.Length > 40 && e.Key == Key.Enter)
{
if (tb == TBNameDatabase)
{
LPopupMessage.Content = "Имя базы данных не может быть длинее 40 символов.";
}
PopupMessage.PlacementTarget = tb;
PopupMessage.IsOpen = true;
e.Handled = true;
}
else
{
PopupMessage.IsOpen = false;
}
}
private void BNewDatabase_Click(object sender, RoutedEventArgs e)
{
e.Handled = true;
AppWPF app = (AppWPF)Application.Current;
if (TBNameDatabase.Text.Length > 40)
{
LPopupMessage.Content = "Имя базы данных не может быть длинее 40 символов.";
PopupMessage.PlacementTarget = TBNameDatabase;
PopupMessage.IsOpen = true;
return;
}
SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder();
builder.DataSource = ServerName;
if (SSPI)
{
builder.IntegratedSecurity = true;
}
else
{
builder.UserID = app.ConfigProgramBin.SQLUserName;
builder.Password = app.ConfigProgramBin.SQLPassword;
}
string str = null;
string newSQLDatabaseName = TBNameDatabase.Text;
if (TBNameDatabase.Text == null || TBNameDatabase.Text.Length == 0)
{
LPopupMessage.Content = "Имя базы данных не может быть пустой.";
PopupMessage.PlacementTarget = TBNameDatabase;
PopupMessage.IsOpen = true;
return;
}
else
{
str = "CREATE DATABASE [" + TBNameDatabase.Text + "]";
}
using (SqlConnection cn = new SqlConnection())
{
cn.ConnectionString = builder.ConnectionString;
try
{
SqlCommand myCommand = new SqlCommand(str, cn);
cn.Open();
myCommand.ExecuteNonQuery();
CBDatabases.Items.Add(TBNameDatabase.Text);
CBDatabases.SelectedItem = TBNameDatabase.Text;
}
catch (SystemException ex)
{
LPopupMessage.Content = ex.Message;
PopupMessage.PlacementTarget = TBNameDatabase;
PopupMessage.IsOpen = true;
return;
}
}
this.Close();
}
}
}