-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainForm.cs
1567 lines (1274 loc) · 56.3 KB
/
MainForm.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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System;
using System.ComponentModel;
using System.Data.SqlClient;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
namespace WindowsFormsApp1
{
public partial class MainForm : Form
{
private const string Cs = @"Data Source = LAPTOP-SETO9IMR\SQLEXPRESS; Initial Catalog = World; Integrated Security = true;";
public MainForm()
{
InitializeComponent();
}
public void OpenLoginForm()
{
LoginForm lf = new LoginForm();
lf.ShowDialog();
if (lf.DialogResult == DialogResult.Cancel)
{
Close();
}
}
private void Form1_Load(object sender, EventArgs e)
{
// TODO: данная строка кода позволяет загрузить данные в таблицу "worldDataSet.Oper". При необходимости она может быть перемещена или удалена.
this.operTableAdapter.Fill(this.worldDataSet.Oper);
}
private void DownloadFromDb()
{
using (var cn = new SqlConnection(Cs))
{
cn.Open();
const string sql = @"SELECT Country.country,Area.area, City.city, District.district,
Country.id_country , Area.id_area, City.id_city, District.id_district
FROM (Country LEFT JOIN Area ON Area.id_country = Country.id_country
LEFT JOIN City ON City.id_area = Area.id_area LEFT JOIN District ON District.id_city = City.id_city)
ORDER BY Country.country, Country.id_country, Area.area, Area.id_area,City.city,City.id_city, District.district, District.id_district";
var cmd = new SqlCommand(sql, cn);
if (cmd == null) return;
var dr = cmd.ExecuteReader();
// while (dr.Read())
// {
// TreeNode n = new TreeNode(dr["country"].ToString(),0,0);
// treeView1.Nodes.Add(n);
// LoadCities((int)dr["id_country"], n);
// }
var lastIdCountry = -1;
var lastIdCity = -1;
var lastIdArea = -1;
var lastIdDistrict = -1;
TreeNode lastAddedCity = null;
TreeNode lastAddedCountry = null;
TreeNode lastAddedArea = null;
while (dr.Read())
{
var countryName = (string)dr["country"];
var idCountry = (int)dr["id_country"];
if (idCountry != lastIdCountry)
{
lastAddedCountry = new TreeNode(countryName, 0, 0);
treeView1.Nodes.Add(lastAddedCountry);
lastIdCountry = idCountry;
lastAddedCountry.Tag = dr["id_country"];
}
if (dr["area"] is DBNull) continue;
var areaName = (string)dr["area"];
var idArea = (int)dr["id_area"];
if (idArea != lastIdArea)
{
lastAddedArea = new TreeNode(areaName, 1, 1);
lastAddedCountry?.Nodes.Add(lastAddedArea);
lastIdArea = idArea;
lastAddedArea.Tag = dr["id_area"];
}
if (dr["city"] is DBNull) continue;
var cityName = (string)dr["city"];
var idCity = (int)dr["id_city"];
if (idCity != lastIdCity)
{
lastAddedCity = new TreeNode(cityName, 2, 2);
lastAddedArea?.Nodes.Add(lastAddedCity);
lastIdCity = idCity;
lastAddedCity.Tag = dr["id_city"];
}
if (dr["district"] is DBNull) continue;
var districtName = (string)dr["district"];
var idDistrict = (int)dr["id_district"];
if (idDistrict == lastIdDistrict) continue;
var lastAddedDistrict = new TreeNode(districtName, 3, 3);
lastAddedCity?.Nodes.Add(lastAddedDistrict);
lastIdDistrict = idDistrict;
lastAddedDistrict.Tag = dr["id_district"];
}
}
}
private void button1_Click(object sender, EventArgs e)
{
treeView1.Nodes.Clear();
DownloadFromDb();
}
// void LoadCities(int id_country, TreeNode parent)
// {
// using (SqlConnection cn = new SqlConnection(cs))
// {
// cn.Open();
// var sql = @"SELECT city FROM City WHERE id_Country = @id_country";
// var cmd = new SqlCommand(sql, cn);
// cmd.Parameters.AddWithValue("id_Country", id_country);
// var dr = cmd.ExecuteReader();
// while (dr.Read())
// {
// TreeNode n = new TreeNode(dr["city"].ToString(),1,1);
// parent.Nodes.Add(n);
// }
// }
// }
private void Delete(object sender, EventArgs e)
{
switch (treeView1.SelectedNode.Level)
{
case 3:
DeleteDistrict();
break;
case 2:
DeleteCity();
break;
case 1:
DeleteArea();
break;
default:
DeleteCountry();
break;
}
}
private void DeleteDistrict()
{
using (var cn = new SqlConnection(Cs))
{
cn.Open();
const string sql = @"delete from District where id_district = @id";
var cmd = new SqlCommand(sql, cn);
cmd.Parameters.AddWithValue("@id", treeView1.SelectedNode.Tag);
cmd.ExecuteNonQuery();
treeView1.SelectedNode.Remove();
}
}
private void DeleteCity()
{
using (var cn = new SqlConnection(Cs))
{
cn.Open();
const string sql = @"delete from City where id_city = @id";
var cmd = new SqlCommand(sql, cn);
cmd.Parameters.AddWithValue("@id", treeView1.SelectedNode.Tag);
cmd.ExecuteNonQuery();
treeView1.SelectedNode.Remove();
}
}
private void DeleteArea()
{
using (var cn = new SqlConnection(Cs))
{
cn.Open();
const string sql = @"delete from Area where id_area = @id";
var cmd = new SqlCommand(sql, cn);
cmd.Parameters.AddWithValue("@id", treeView1.SelectedNode.Tag);
cmd.ExecuteNonQuery();
treeView1.SelectedNode.Remove();
}
}
private void DeleteCountry()
{
using (var cn = new SqlConnection(Cs))
{
cn.Open();
const string sql = @"delete from Country where id_country = @id";
var cmd = new SqlCommand(sql, cn);
cmd.Parameters.AddWithValue("@id", treeView1.SelectedNode.Tag);
cmd.ExecuteNonQuery();
treeView1.SelectedNode.Remove();
}
}
private void contextMenuStrip1_Opening(object sender, CancelEventArgs e)
{ if (LoginForm.Adminka == 0)
{
добавитьToolStripMenuItem.Enabled = false;
удалитьToolStripMenuItem.Enabled = false;
updateToolStripMenuItem.Enabled = false;
insertTheAreaToolStripMenuItem.Enabled = false;
добавитьГородToolStripMenuItem.Enabled = false;
добавитьToolStripMenuItem1.Enabled = false;
}
else
{
if (treeView1.SelectedNode == null)
{
добавитьToolStripMenuItem.Enabled = true;
удалитьToolStripMenuItem.Enabled = false;
updateToolStripMenuItem.Enabled = false;
insertTheAreaToolStripMenuItem.Enabled = false;
добавитьГородToolStripMenuItem.Enabled = false;
добавитьToolStripMenuItem1.Enabled = false;
}
else
switch (treeView1.SelectedNode.Level)
{
case 0:
удалитьToolStripMenuItem.Enabled = true;
добавитьToolStripMenuItem.Enabled = true;
insertTheAreaToolStripMenuItem.Enabled = true;
updateToolStripMenuItem.Enabled = true;
добавитьГородToolStripMenuItem.Enabled = false;
добавитьToolStripMenuItem1.Enabled = false;
break;
case 1:
удалитьToolStripMenuItem.Enabled = true;
insertTheAreaToolStripMenuItem.Enabled = false;
добавитьГородToolStripMenuItem.Enabled = true;
updateToolStripMenuItem.Enabled = true;
добавитьToolStripMenuItem.Enabled = false;
добавитьToolStripMenuItem1.Enabled = false;
break;
case 2:
удалитьToolStripMenuItem.Enabled = true;
insertTheAreaToolStripMenuItem.Enabled = false;
добавитьГородToolStripMenuItem.Enabled = false;
updateToolStripMenuItem.Enabled = true;
добавитьToolStripMenuItem.Enabled = false;
добавитьToolStripMenuItem1.Enabled = true;
break;
case 3:
удалитьToolStripMenuItem.Enabled = true;
insertTheAreaToolStripMenuItem.Enabled = false;
updateToolStripMenuItem.Enabled = true;
добавитьГородToolStripMenuItem.Enabled = false;
добавитьToolStripMenuItem.Enabled = false;
добавитьToolStripMenuItem1.Enabled = false;
break;
default:
удалитьToolStripMenuItem.Enabled = false;
добавитьГородToolStripMenuItem.Enabled = false;
добавитьToolStripMenuItem.Enabled = false;
updateToolStripMenuItem.Enabled = false;
добавитьToolStripMenuItem1.Enabled = false;
insertTheAreaToolStripMenuItem.Enabled = false;
break;
}
}
}
private void InsertDistrict()
{
var form = new DistrictForm();
form.ShowDialog();
District district = null;
if (form.DialogResult == DialogResult.OK)
{
district = form.D;
}
using (var cn = new SqlConnection(Cs))
{
cn.Open();
if (district == null) return;
if(district.DistrictName == "")
{
MessageBox.Show("Вы заполнили не все поля"); return;
}
var sql = @"INSERT INTO District OUTPUT INSERTED.id_district VALUES (@name, @id)";
var cmd = new SqlCommand(sql, cn);
cmd.Parameters.AddWithValue("@name", district.DistrictName);
cmd.Parameters.AddWithValue("@id", treeView1.SelectedNode.Tag);
var dr = cmd.ExecuteReader();
dr.Read();
var districtId = (int)dr["id_district"];
dr.Close();
var newDistrict = new TreeNode(district.DistrictName, 3, 3)
{
Tag = districtId
};
treeView1.SelectedNode.Nodes.Add(newDistrict);
}
}
private void InsertArea()
{
var form = new AreaForm();
form.ShowDialog();
Area area = null;
if (form.DialogResult == DialogResult.OK)
{
area = form.Ar;
}
using (var cn = new SqlConnection(Cs))
{
cn.Open();
if (area == null) return;
if (area.AreaName == "" || area.Okrug=="" || area.Autocod =="" || area.Capital == "")
{
MessageBox.Show("Вы заполнили не все поля"); return;
}
var sql = @"INSERT INTO Area OUTPUT INSERTED.id_area VALUES (@name, @okrug, @autocod, @capital, @iso, @id)";
var cmd = new SqlCommand(sql, cn);
cmd.Parameters.AddWithValue("@name", area.AreaName);
cmd.Parameters.AddWithValue("@id", treeView1.SelectedNode.Tag);
cmd.Parameters.AddWithValue("@okrug", area.Okrug);
cmd.Parameters.AddWithValue("@autocod", area.Autocod);
cmd.Parameters.AddWithValue("@capital", area.Capital);
cmd.Parameters.AddWithValue("@iso", area.Iso);
var dr = cmd.ExecuteReader();
dr.Read();
var areaId = (int)dr["id_area"];
dr.Close();
var newCity = new TreeNode(area.AreaName, 1, 1)
{
Tag = areaId
};
treeView1.SelectedNode.Nodes.Add(newCity);
}
}
private void InsertCity()
{
var form = new CityForm();
form.ShowDialog();
City city = null;
if (form.DialogResult == DialogResult.OK)
{
city = form.C;
}
using (var cn = new SqlConnection(Cs))
{
cn.Open();
if (city == null) return;
if (city.CityName == "" || city.Telcod =="" || city.Post == "")
{
MessageBox.Show("Вы заполнили не все поля"); return;
}
var sql = @"INSERT INTO City OUTPUT INSERTED.id_city VALUES (@name, @num, @tz, @lat, @long, @id, @telcod, @level, @iso, @vid, @post)";
var cmd = new SqlCommand(sql, cn);
cmd.Parameters.AddWithValue("@name", city.CityName);
cmd.Parameters.AddWithValue("@id", treeView1.SelectedNode.Tag);
cmd.Parameters.AddWithValue("@num", city.NumberOfDistricts);
cmd.Parameters.AddWithValue("@tz", city.TimeZone);
cmd.Parameters.AddWithValue("@lat", city.Latitude);
cmd.Parameters.AddWithValue("@long", city.Longitude);
cmd.Parameters.AddWithValue("@telcod", city.Telcod);
cmd.Parameters.AddWithValue("@level", city.Level);
cmd.Parameters.AddWithValue("@iso", city.Iso);
cmd.Parameters.AddWithValue("@vid", city.Vid);
cmd.Parameters.AddWithValue("@post", city.Post);
var dr = cmd.ExecuteReader();
dr.Read();
var cityId = (int)dr["id_city"];
dr.Close();
var newCity = new TreeNode(city.CityName, 2, 2)
{
Tag = cityId
};
treeView1.SelectedNode.Nodes.Add(newCity);
}
}
private void InsertCountry()
{
var form = new Form2();
form.ShowDialog();
Country country = null;
if (form.DialogResult == DialogResult.OK)
{
country = form.Cnt;
}
using (SqlConnection cn = new SqlConnection(Cs))
{
cn.Open();
if (country == null) return;
if (country.CountryName == "" || country.Location== "")
{
MessageBox.Show("Вы заполнили не все поля"); return;
}
var sql = @"INSERT INTO Country OUTPUT INSERTED.id_country VALUES (@name, @location, @iso, @telcode, @capital)";
var cmd = new SqlCommand(sql, cn);
cmd.Parameters.AddWithValue("@name", country.CountryName);
cmd.Parameters.AddWithValue("@location", country.Location);
cmd.Parameters.AddWithValue("@iso", country.Iso);
cmd.Parameters.AddWithValue("@telcode", country.Telcod);
cmd.Parameters.AddWithValue("@capital", country.Capital);
var dr = cmd.ExecuteReader();
dr.Read();
var countryId = (int)dr["id_country"];
dr.Close();
var newCountry = new TreeNode(country.CountryName, 0, 0)
{
Tag = countryId
};
treeView1.Nodes.Add(newCountry);
}
}
private void InsertTheCountry(object sender, EventArgs e)
{
InsertCountry();
}
private void InsertTheCity(object sender, EventArgs e)
{
InsertCity();
}
private void InsertTheDistrict(object sender, EventArgs e)
{
InsertDistrict();
}
private void UpdateDistrict()
{
var form = new DistrictForm();
using (SqlConnection cn = new SqlConnection(Cs))
{
cn.Open();
var sql = @"SELECT id_district, district FROM District WHERE id_district = @id";
var cmd = new SqlCommand(sql, cn);
cmd.Parameters.AddWithValue("@id", treeView1.SelectedNode.Tag);
cmd.ExecuteNonQuery();
var dr = cmd.ExecuteReader();
dr.Read();
form.D = new District()
{ Id = (int)dr["id_district"],
DistrictName = (string)dr["district"]
};
dr.Close();
form.WriteInForm();
}
form.ShowDialog();
District district = null;
if (form.DialogResult == DialogResult.OK)
{
district = form.D;
}
using (var cn = new SqlConnection(Cs))
{
cn.Open();
if (district == null) return;
if (district.DistrictName == "")
{
MessageBox.Show("Вы заполнили не все поля"); return;
}
var sql = @"UPDATE District
SET district = @name
WHERE id_district = @id";
var cmd = new SqlCommand(sql, cn);
cmd.Parameters.AddWithValue("@name", district.DistrictName);
cmd.Parameters.AddWithValue("@id", treeView1.SelectedNode.Tag);
cmd.ExecuteNonQuery();
treeView1.SelectedNode.Text = district.DistrictName;
}
}
private void UpdateCity()
{
var form = new CityForm();
using (SqlConnection cn = new SqlConnection(Cs))
{
cn.Open();
var sql = @"SELECT id_city, city , number_of_districts, time_zone, latitude, longitude, telcod, level, iso, vid, post FROM City WHERE id_city = @id";
var cmd = new SqlCommand(sql, cn);
cmd.Parameters.AddWithValue("@id", treeView1.SelectedNode.Tag);
cmd.ExecuteNonQuery();
var dr = cmd.ExecuteReader();
dr.Read();
form.C = new City()
{ Id = (int)dr["id_city"],
CityName = (string)dr["city"],
NumberOfDistricts = (int)dr["number_of_districts"],
TimeZone = (int)dr["time_zone"],
Latitude = (float)dr["latitude"],
Longitude = (float)dr["longitude"],
Telcod = (string)dr["telcod"],
Level = (int)dr["level"],
Iso = (int)dr["iso"],
Vid = (int)dr["vid"],
Post = (string)dr["post"]
};
dr.Close();
form.WriteInForm();
}
form.ShowDialog();
City city = null;
if (form.DialogResult == DialogResult.OK)
{
city = form.C;
}
using (var cn = new SqlConnection(Cs))
{
cn.Open();
if (city == null) return;
if (city.CityName == "" || city.Telcod == "" || city.Post == "")
{
MessageBox.Show("Вы заполнили не все поля"); return;
}
var sql = @"Update City SET city = @name ,number_of_districts = @num, time_zone = @tz, latitude = @lat,longitude = @long, telcod = @telcod, level=@level, iso=@iso, vid = @vid, post = @post WHERE id_city = @id";
var cmd = new SqlCommand(sql, cn);
cmd.Parameters.AddWithValue("@name", city.CityName);
cmd.Parameters.AddWithValue("@num", city.NumberOfDistricts);
cmd.Parameters.AddWithValue("@tz", city.TimeZone);
cmd.Parameters.AddWithValue("@lat", city.Latitude);
cmd.Parameters.AddWithValue("@long", city.Longitude);
cmd.Parameters.AddWithValue("@telcod", city.Telcod);
cmd.Parameters.AddWithValue("@level", city.Level);
cmd.Parameters.AddWithValue("@iso", city.Iso);
cmd.Parameters.AddWithValue("@vid", city.Vid);
cmd.Parameters.AddWithValue("@post", city.Post);
cmd.Parameters.AddWithValue("@id", treeView1.SelectedNode.Tag);
cmd.ExecuteNonQuery();
treeView1.SelectedNode.Text = city.CityName;
}
}
private void UpdateArea()
{
var form = new AreaForm();
using (SqlConnection cn = new SqlConnection(Cs))
{
cn.Open();
var sql = @"SELECT id_area, area , okrug, autocod, capital, iso FROM Area WHERE id_area = @id";
var cmd = new SqlCommand(sql, cn);
cmd.Parameters.AddWithValue("@id", treeView1.SelectedNode.Tag);
cmd.ExecuteNonQuery();
var dr = cmd.ExecuteReader();
dr.Read();
form.Ar = new Area()
{ Id = (int)dr["id_area"],
AreaName = (string)dr["area"],
Okrug = (string)dr["okrug"],
Autocod = (string)dr["autocod"],
Capital = (string)dr["capital"],
Iso = (int)dr["iso"]
};
dr.Close();
form.WriteInForm();
}
form.ShowDialog();
Area area = null;
if (form.DialogResult == DialogResult.OK)
{
area = form.Ar;
}
using (var cn = new SqlConnection(Cs))
{
cn.Open();
if (area == null) return;
if (area.AreaName == "" || area.Okrug == "" || area.Autocod == "" || area.Capital == "")
{
MessageBox.Show("Вы заполнили не все поля"); return;
}
var sql = @"Update Area SET area = @name ,okrug = @okrug, autocod = @autocod, capital = @capital,iso = @iso WHERE id_area = @id";
var cmd = new SqlCommand(sql, cn);
if (area == null) return;
cmd.Parameters.AddWithValue("@name", area.AreaName);
cmd.Parameters.AddWithValue("@okrug", area.Okrug);
cmd.Parameters.AddWithValue("@autocod", area.Autocod);
cmd.Parameters.AddWithValue("@capital", area.Capital);
cmd.Parameters.AddWithValue("@iso", area.Iso);
cmd.Parameters.AddWithValue("@id", treeView1.SelectedNode.Tag);
cmd.ExecuteNonQuery();
treeView1.SelectedNode.Text = area.AreaName;
}
}
public string RemoveSpecialCharacters(string str)
{
StringBuilder sb = new StringBuilder();
foreach (char c in str)
{
if ((c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || c == '.' || c == '_')
{
sb.Append(c);
}
}
return sb.ToString();
}
private void UpdateCountry()
{
var form = new Form2();
using (SqlConnection cn = new SqlConnection(Cs))
{
cn.Open();
var sql = @"SELECT id_country, country , location, iso, telcod, capital FROM Country WHERE id_country = @id";
var cmd = new SqlCommand(sql, cn);
cmd.Parameters.AddWithValue("@id", treeView1.SelectedNode.Tag);
cmd.ExecuteNonQuery();
var dr = cmd.ExecuteReader();
dr.Read();
form.Cnt = new Country()
{ Id = (int)dr["id_country"],
CountryName = (string)dr["country"],
Location = (string)dr["location"],
Iso = (int)dr["iso"],
Telcod = (int)dr["telcod"],
Capital = (string)dr["capital"]
};
dr.Close();
form.WriteInForm();
}
form.ShowDialog();
Country country = null;
if (form.DialogResult == DialogResult.OK)
{
country = form.Cnt;
}
using (SqlConnection cn = new SqlConnection(Cs))
{
cn.Open();
if (country == null) return;
if (country.CountryName == "" || country.Location == "")
{
MessageBox.Show("Вы заполнили не все поля"); return;
}
var sql = @"Update Country SET country = @name , location = @location, iso= @iso, telcod = @telcod, capital = @capital WHERE id_country = @id";
var cmd = new SqlCommand(sql, cn);
cmd.Parameters.AddWithValue("@name", country.CountryName);
cmd.Parameters.AddWithValue("@location", country.Location);
cmd.Parameters.AddWithValue("@iso", country.Iso);
cmd.Parameters.AddWithValue("@telcod", country.Telcod);
cmd.Parameters.AddWithValue("@capital", country.Capital);
cmd.Parameters.AddWithValue("@id", treeView1.SelectedNode.Tag);
cmd.ExecuteNonQuery();
treeView1.SelectedNode.Text = country.CountryName;
}
}
private void updateToolStripMenuItem_Click(object sender, EventArgs e)
{ if (treeView1.SelectedNode == null) return;
else
if (treeView1.SelectedNode.Level == 3)
{
UpdateDistrict();
}
else if (treeView1.SelectedNode.Level == 2)
{
UpdateCity();
}
else if (treeView1.SelectedNode.Level == 1)
{
UpdateArea();
}
else UpdateCountry();
}
private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
{
}
private void insertTheAreaToolStripMenuItem_Click(object sender, EventArgs e)
{
InsertArea();
}
private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
{
}
private void button2_Click(object sender, EventArgs e)
{
OperLoad();
}
private void OperLoad() {
listView1.Items.Clear();
using (var cn = new SqlConnection(Cs))
{
cn.Open();
const string sql = @"SELECT name, mobile, mvno, mnc, brand, url, id_country, id_oper
FROM Oper
ORDER BY name, mobile, mvno, mnc, brand, url, id_country, id_oper";
var cmd = new SqlCommand(sql, cn);
if (cmd == null) return;
var dr = cmd.ExecuteReader();
listView1.View = View.Details;
string[] items = new string[8];
while (dr.Read())
{
items[0] = ((int)dr["id_oper"]).ToString();
items[1] = (string)dr["name"];
items[2] = ((int)dr["mobile"]).ToString();
items[3] = ((int)dr["mvno"]).ToString();
items[4] = ((int)dr["id_country"]).ToString();
items[5] = ((int)dr["mnc"]).ToString();
items[6] = (string)dr["brand"];
items[7] = (string)dr["url"];
var item = new ListViewItem(items);
item.Tag = (int)dr["id_oper"];
listView1.Items.Add(item);
}
}
}
private void listView1_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void добавитьToolStripMenuItem2_Click(object sender, EventArgs e)
{
listView1.View = View.Details;
var form = new OperForm();
form.ShowDialog();
Oper oper = null;
if (form.DialogResult == DialogResult.OK)
{
oper = form.Op;
}
using (SqlConnection cn = new SqlConnection(Cs))
{
cn.Open();
if (oper == null) return;
if (oper.OperName == "" || oper.Brand == "" || oper.Url == "")
{
MessageBox.Show("Вы заполнили не все поля"); return;
}
var sql = @"INSERT INTO Oper OUTPUT INSERTED.id_oper VALUES (@name, @mobile, @mvno, @id_country, @mnc, @brand, @url)";
var cmd = new SqlCommand(sql, cn);
cmd.Parameters.AddWithValue("@name", oper.OperName);
cmd.Parameters.AddWithValue("@mobile", oper.Mobile);
cmd.Parameters.AddWithValue("@mvno", oper.Mvno);
cmd.Parameters.AddWithValue("@id_country", oper.CountryId);
cmd.Parameters.AddWithValue("@mnc", oper.Mnc);
cmd.Parameters.AddWithValue("@brand", oper.Brand);
cmd.Parameters.AddWithValue("@url", oper.Url);
var dr = cmd.ExecuteReader();
try
{
dr.Read();
}
catch
{
MessageBox.Show("Такой страны нет");
return;
}
string[] items = new string[8];
items[0] = ((int)dr["id_oper"]).ToString();
items[1] = oper.OperName;
items[2] = oper.Mobile.ToString();
items[3] = oper.Mvno.ToString();
items[4] = oper.CountryId.ToString();
items[5] = oper.Mnc.ToString();
items[6] = oper.Brand;
items[7] = oper.Url;
var item = new ListViewItem(items);
item.Tag = (int)dr["id_oper"];
listView1.Items.Add(item);
}
}
private void contextMenuStrip2_Opening(object sender, CancelEventArgs e)
{if (LoginForm.Adminka == 0)
{
изменитьToolStripMenuItem.Enabled = false;
добавитьToolStripMenuItem2.Enabled = false;
удалитьToolStripMenuItem1.Enabled = false;
}
else
{
if (listView1.SelectedItems.Count == 0)
{
изменитьToolStripMenuItem.Enabled = false;
добавитьToolStripMenuItem2.Enabled = true;
удалитьToolStripMenuItem1.Enabled = false;
}
else
{
изменитьToolStripMenuItem.Enabled = true;
добавитьToolStripMenuItem2.Enabled = true;
удалитьToolStripMenuItem1.Enabled = true;
}
}
}
private void удалитьToolStripMenuItem1_Click(object sender, EventArgs e)
{
using (var cn = new SqlConnection(Cs))
{
cn.Open();
const string sql = @"delete from Oper where id_oper = @id";
var cmd = new SqlCommand(sql, cn);
ListViewItem lw = new ListViewItem();
cmd.Parameters.AddWithValue("@id", listView1.SelectedItems[0].Tag);
cmd.ExecuteNonQuery();
listView1.SelectedItems[0].Remove();
}
}
private void изменитьToolStripMenuItem_Click(object sender, EventArgs e)
{
var form = new OperForm();
using (SqlConnection cn = new SqlConnection(Cs))
{
cn.Open();
var sql = @"SELECT id_oper, name, mobile, mvno, id_country, mnc, brand, url FROM Oper WHERE id_oper = @id";
var cmd = new SqlCommand(sql, cn);
cmd.Parameters.AddWithValue("@id", listView1.SelectedItems[0].Tag);
cmd.ExecuteNonQuery();
var dr = cmd.ExecuteReader();
dr.Read();
form.Op = new Oper()
{ OperId = (int)dr["id_oper"],
OperName = (string)dr["name"],
Mobile = (int)dr["mobile"],
Mvno = (int)dr["mvno"],
CountryId = (int)dr["id_country"],
Mnc = (int)dr["mnc"],
Brand = (string)dr["brand"],
Url = (string)dr["url"]
};
dr.Close();
form.WriteInForm();
}
form.ShowDialog();
Oper oper = null;
if (form.DialogResult == DialogResult.OK)
{
oper = form.Op;
}
using (var cn = new SqlConnection(Cs))
{
cn.Open();
if (oper == null) return;
if (oper.OperName == "" || oper.Brand == "" || oper.Url == "" )
{
MessageBox.Show("Вы заполнили не все поля"); return;
}
var sql = @"UPDATE Oper
SET name = @opname, mobile = @mobile, mvno = @mvno, id_country = @idcntr, mnc = @mnc, brand= @brand, url = @url
WHERE id_oper = @id";
var cmd = new SqlCommand(sql, cn);
cmd.Parameters.AddWithValue("@opname", oper.OperName);
cmd.Parameters.AddWithValue("@mobile", oper.Mobile);
cmd.Parameters.AddWithValue("@mvno", oper.Mvno);
cmd.Parameters.AddWithValue("@idcntr", oper.CountryId);
cmd.Parameters.AddWithValue("@mnc", oper.Mnc);
cmd.Parameters.AddWithValue("@brand", oper.Brand);
cmd.Parameters.AddWithValue("@url", oper.Url);
cmd.Parameters.AddWithValue("@id", listView1.SelectedItems[0].Tag);
try
{
cmd.ExecuteNonQuery();
} catch
{
MessageBox.Show("Такой страны нет");
return;
}
string[] items = new string[8];
items[0] = (oper.OperId).ToString();
items[1] = oper.OperName;
items[2] = oper.Mobile.ToString();
items[3] = oper.Mvno.ToString();
items[4] = oper.CountryId.ToString();
items[5] = oper.Mnc.ToString();
items[6] = oper.Brand;
items[7] = oper.Url;
var item = new ListViewItem(items);
item.Tag = oper.OperId;
listView1.SelectedItems[0].ListView.Items.Add( item);