-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMSMain.cs
1484 lines (1401 loc) · 59.5 KB
/
MSMain.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.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Collections;
using Novacode;
using DBUtil;
using System.Text.RegularExpressions;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;
namespace CodeCreator
{
public partial class MSMain : Form
{
private TableStruct tblStruct = null;
public MSMain()
{
InitializeComponent();
this.FormClosed += Form4_FormClosed;
}
private void Form4_Load(object sender, EventArgs e)
{
dataGridView2.DataError += dataGridView2_DataError;
radTable.Checked = true;
EnableAllBtn();
dataGridView1.DoubleBuffered(true);
dataGridView2.DoubleBuffered(true);
dataGridView2.Visible = true;
richTextBox1.Visible = false;
CHKTable();
}
void dataGridView2_DataError(object sender, DataGridViewDataErrorEventArgs e)
{
//表格绑定数据错误时不做处理
}
private void CHKTable()
{
SqlServerIDbAccess iDb = DataTransfer.iDb as SqlServerIDbAccess;
string sql = @"
select
序号=ROW_NUMBER() OVER(order BY table_name),
表名=table_name,
说明=(SELECT value
FROM sys.extended_properties ds
LEFT JOIN sysobjects tbs ON ds.major_id=tbs.id
WHERE ds.minor_id=0 and
tbs.name=table_name )
from INFORMATION_SCHEMA.TABLES t where t.TABLE_TYPE='BASE TABLE'
";
DataSet ds = iDb.GetDataSet(sql);
dataGridView1.DataSource = ds.Tables[0];
int wid = dataGridView1.Width;
dataGridView1.Columns[0].Width = 50;
dataGridView1.Columns[1].Width = 100;
dataGridView1.Columns[2].Width = Convert.ToInt32((wid - 150 - 60) * 0.4);
dataGridView1.Columns[3].Width = Convert.ToInt32((wid - 150 - 60) * 0.6);
if (ds.Tables[0].Rows.Count > 0)
{
tblStruct = iDb.GetTableStruct(ds.Tables[0].Rows[0]["表名"].ToString());
InitTableInfo();
}
}
void Form4_FormClosed(object sender, FormClosedEventArgs e)
{
Application.Exit();
}
private void InitViewInfo(string viewName)
{
richTextBox1.Visible = true;
dataGridView2.Visible = false;
textBox1.Text = "";
textBox2.Text = "";
picLoad.Show();
System.Threading.Tasks.Task.Factory.StartNew(() =>
{
string viewSql = "";
if (!string.IsNullOrEmpty(viewName))
{
SqlServerIDbAccess iDb = IDBFactory.CreateIDB(DataTransfer.iDb.ConnectionString, "SQLSERVER") as SqlServerIDbAccess;
tblStruct = iDb.GetTableStruct(viewName);
viewSql = iDb.CreateViewSql(tblStruct.Name);
iDb.conn.Close();
iDb.conn.Dispose();
iDb = null;
}
this.Invoke(new MethodInvoker(() =>
{
richTextBox1.Text = viewSql;
DisableAllRad();
radStruct.Enabled = true;
radStruct.Checked = false;
radData.Enabled = true;
radData.Checked = false;
picLoad.Hide();
}));
});
}
private void InitTableInfo(string tableName = "")
{
richTextBox1.Visible = false;
dataGridView2.Visible = true;
textBox1.Text = tblStruct.Name;
label4.Text = tblStruct.Desc;
picLoad.Show();
System.Threading.Tasks.Task.Factory.StartNew(() =>
{
if (!string.IsNullOrEmpty(tableName) || tblStruct != null)
{
SqlServerIDbAccess iDb = IDBFactory.CreateIDB(DataTransfer.iDb.ConnectionString, "SQLSERVER") as SqlServerIDbAccess;
tableName = string.IsNullOrWhiteSpace(tableName) ? tblStruct.Name : tableName;
tblStruct = iDb.GetTableStruct(tableName);
iDb.conn.Close();
iDb.conn.Dispose();
iDb = null;
}
DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn("序号"));
dt.Columns.Add(new DataColumn("列名"));
dt.Columns.Add(new DataColumn("类型"));
dt.Columns.Add(new DataColumn("说明"));
dt.Columns.Add(new DataColumn("是否可空"));
dt.Columns.Add(new DataColumn("是否唯一"));
dt.Columns.Add(new DataColumn("自增说明"));
dt.Columns.Add(new DataColumn("默认值"));
for (int i = 0; i < tblStruct.Columns.Count; i++)
{
DataRow row = dt.NewRow();
row[0] = i + 1;
row[1] = tblStruct.Columns[i].Name;
row[2] = tblStruct.Columns[i].FinalType;
row[3] = tblStruct.Columns[i].Desc;
row[4] = tblStruct.Columns[i].IsNullable ? "是" : "否";
row[5] = tblStruct.Columns[i].IsUnique ? "是" : "否";
row[6] = tblStruct.Columns[i].FinalIdentity;
row[7] = tblStruct.Columns[i].Default;
dt.Rows.Add(row);
}
this.Invoke(new MethodInvoker(() =>
{
textBox1.Text = tblStruct.Name;
dataGridView2.DataSource = dt.DefaultView;
textBox2.Text = tblStruct.PrimaryKey;
EnableAllRad();
if (radView.Checked)
{
radConstraint.Enabled = false;
radTritbl.Enabled = false;
radIndex.Enabled = false;
}
radStruct.Checked = true;
picLoad.Hide();
}));
});
}
private void DisableAllRad()
{
radStruct.Enabled = false;
radData.Enabled = false;
radConstraint.Enabled = false;
radTritbl.Enabled = false;
radIndex.Enabled = false;
}
private void EnableAllRad()
{
radStruct.Enabled = true;
radData.Enabled = true;
radConstraint.Enabled = true;
radTritbl.Enabled = true;
radIndex.Enabled = true;
}
private void button4_Click(object sender, EventArgs e)
{
this.Close();
}
private void 添加连接ToolStripMenuItem_Click(object sender, EventArgs e)
{
new ConnAdd().ShowDialog();
}
private void button1_Click(object sender, EventArgs e)
{
string path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "TmpSolutions");
if (!Directory.Exists(path))
{
MessageBox.Show("TmpSolutions文件夹找不到!(" + path + ")");
return;
}
openFileDialog1.InitialDirectory = path;
openFileDialog1.FileName = "";
openFileDialog1.Filter = "生成方案文件|*.msln";
openFileDialog1.Multiselect = false;
openFileDialog1.ShowDialog();
string fileName = openFileDialog1.FileName;
textBox3.Text = fileName;
}
private void button3_Click(object sender, EventArgs e)
{
if (!PreData())
{
return;
}
Hashtable ht = (Hashtable)DataTransfer.data;
string mslnPath = ht["MslnPath"].ToString();
Hashtable htres = new Hashtable();
MslnConf mslnConf = new MslnConf(mslnPath);
foreach (var item in mslnConf.tmpItems)
{
htres.Add(item.OutName, DynamicCreator.Create(item.Path, item.ClassFullName, item.CreateMethod));
}
folderBrowserDialog1.ShowNewFolderButton = true;
folderBrowserDialog1.Description = "请选择文件路径";
folderBrowserDialog1.SelectedPath = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
if (folderBrowserDialog1.ShowDialog() == DialogResult.OK || folderBrowserDialog1.ShowDialog() == DialogResult.Yes)
{
string str = folderBrowserDialog1.SelectedPath;
str = Path.Combine(str, ((Hashtable)DataTransfer.data)["TableName"].ToString());
if (Directory.Exists(str))
{
if (MessageBox.Show("发现已存在文件夹" + str + "是否覆盖已存在的文件?", "文件输出选择", MessageBoxButtons.OKCancel) != DialogResult.OK) { return; }
}
else
{
Directory.CreateDirectory(str);
}
foreach (var key in htres.Keys)
{
File.WriteAllText(Path.Combine(str, key.ToString()), htres[key].ToString());
}
MessageBox.Show("生成成功!");
System.Diagnostics.Process.Start("Explorer.exe", "/select," + str);
}
}
/// <summary>
/// 处理数据
/// </summary>
/// <returns></returns>
private bool PreData()
{
string MslnPath = textBox3.Text;
Hashtable ht = new Hashtable();
if (string.IsNullOrEmpty(textBox1.Text))
{
MessageBox.Show("请选中要生成代码的表再操作!");
return false;
}
if (MessageBox.Show("选中表:" + textBox1.Text + "?", "确认表", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
{
return false;
}
if (string.IsNullOrEmpty(textBox3.Text))
{
MessageBox.Show("请选中要生成代码的方案文件!");
return false;
}
if (dataGridView2.Rows.Count == 0)
{
MessageBox.Show("选中的表 " + textBox1.Text + " 无列可生成!");
return false;
}
ht.Add("TableName", textBox1.Text);
ht.Add("TableInstructions", label4.Text);
List<string> colNames = new List<string>();
List<string> colTypes = new List<string>();
List<bool> colIsIdentities = new List<bool>();
List<string> colInstructions = new List<string>();
for (int i = 0; i < dataGridView2.Rows.Count; i++)
{
colNames.Add(dataGridView2.Rows[i].Cells[1].Value.ToString());
colTypes.Add(dataGridView2.Rows[i].Cells[2].Value.ToString());
colInstructions.Add(dataGridView2.Rows[i].Cells[3].Value.ToString());
colIsIdentities.Add(dataGridView2.Rows[i].Cells[6].Value.ToString() == "是");
}
ht.Add("listColumnNames", colNames);
ht.Add("listColumnTypes", colTypes);
ht.Add("listColumnInstructions", colInstructions);
ht.Add("listColumnIsIdentities", colIsIdentities);
ht.Add("PrimaryKey", textBox2.Text);
ht.Add("MslnPath", textBox3.Text);
DataTransfer.data = ht;
return true;
}
private void button2_Click(object sender, EventArgs e)
{
this.OnLoad(null);
}
private void dataGridView1_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
{
//点击表头直接返回
if (e.RowIndex == -1 || e.ColumnIndex == -1) { return; }
//数据区域
//如果是左键
if (e.Button == System.Windows.Forms.MouseButtons.Left)
{
if (e.ColumnIndex == 0)
{
//如果是第一列就选中复选框
DataGridViewCheckBoxCell chk = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex] as DataGridViewCheckBoxCell;
if (chk.Value == null || (chk.Value as bool?) == false)
{
chk.Value = true;
}
else
{
chk.Value = false;
}
return;
}
//刷新右侧
int rowIndex = e.RowIndex;
string objName = dataGridView1.Rows[rowIndex].Cells[2].Value.ToString();
if (radTable.Checked)
{
InitTableInfo(objName);
}
else if (radView.Checked)
{
InitViewInfo(objName);
}
else if (radProc.Checked)
{
InitProcInfo(objName);
}
else if (radFunc.Checked)
{
InitFuncInfo(objName);
}
}
else if (e.Button == System.Windows.Forms.MouseButtons.Right) //点击的是鼠标右键
{
//选中行的情况,排除在非选中行上鼠标右键
if (dataGridView1.SelectedRows.Count > 0)
{
if (!dataGridView1.SelectedRows.Contains(dataGridView1.Rows[e.RowIndex]))
{
return;
}
}
//多选情况下只提供复制功能
if (dataGridView1.SelectedCells.Count > 1)
{
//排除鼠标右键不在选中单元格上的情况
if (!dataGridView1.SelectedCells.Contains(dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex]))
{
return;
}
left_编辑.Visible = false;
left_删除表.Visible = false;
left_删除存储过程.Visible = false;
left_删除函数.Visible = false;
left_删除视图.Visible = false;
left_新建表.Visible = false;
left_truncate表.Visible = false;
this.tblContext.Show(MousePosition.X, MousePosition.Y);
return;
}
else
{
if (e.ColumnIndex < 2) { return; }
//单元情况下的右键菜单
tblEdit = e.ColumnIndex;//记录编辑的列索引
tblRowEdit = e.RowIndex;//记录编辑的行索引
tblOldval = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString();
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
dataGridView1.Rows[i].Selected = false;
}
this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Selected = true;
//先全部不显示
left_编辑.Visible = false;
left_删除表.Visible = false;
left_新建表.Visible = false;
left_删除函数.Visible = false;
left_删除视图.Visible = false;
left_删除存储过程.Visible = false;
left_truncate表.Visible = false;
if (radTable.Checked)
{
left_编辑.Visible = true;
left_删除表.Visible = true;
left_新建表.Visible = true;
left_truncate表.Visible = true;
}
else if (radView.Checked)
{
left_删除视图.Visible = true;
}
else if (radProc.Checked)
{
left_删除存储过程.Visible = true;
}
else if (radFunc.Checked)
{
left_删除函数.Visible = true;
}
this.tblContext.Show(MousePosition.X, MousePosition.Y); //MousePosition.X, MousePosition.Y 是为了让菜单在所选行的位置显示
}
}
}
private void toolStripMenuItem1_Click(object sender, EventArgs e)
{
this.dataGridView1.BeginEdit(false);
}
private void toolStripMenuItem2_Click(object sender, EventArgs e)
{
if (MessageBox.Show("是否删除这条数据?", "删除确认框", MessageBoxButtons.OKCancel) == System.Windows.Forms.DialogResult.OK)
{
MessageBox.Show("删除成功!");
}
}
int tblEdit = 0;
int tblRowEdit = 0;
string tblOldval = "";
private void dataGridView1_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
{
DataGridView dgv = (DataGridView)sender;
//是否可以进行编辑的条件检查
if (e.ColumnIndex == 0)
{
// 取消编辑
e.Cancel = true;
}
}
private void 复制ToolStripMenuItem_Click(object sender, EventArgs e)
{
Clipboard.SetDataObject(dataGridView1.GetClipboardContent());
}
private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
string newVal = dataGridView1.CurrentCell.Value.ToString();
SqlServerIDbAccess iDb = DataTransfer.iDb as SqlServerIDbAccess;
if (newVal != tblOldval)
{
if (System.Windows.Forms.DialogResult.OK == MessageBox.Show("是否更新到数据库?", "是否更新", MessageBoxButtons.OKCancel))
{
if (tblEdit == 2)
{
try
{
iDb.RenameTable(tblOldval, newVal);
MessageBox.Show("更新成功!");
}
catch (Exception ex)
{
MessageBox.Show("更新失败:" + ex.ToString());
}
dataGridView1.CurrentCell.ReadOnly = false;
}
else if (tblEdit == 3)
{
try
{
iDb.SaveTableDesc(dataGridView1.Rows[tblRowEdit].Cells[2].Value.ToString(), newVal);
MessageBox.Show("更新成功!");
}
catch (Exception ex)
{
MessageBox.Show("更新失败:" + ex.ToString());
}
dataGridView1.CurrentCell.ReadOnly = false;
}
}
else
{
dataGridView1.CurrentCell.Value = tblOldval;
}
}
}
private void dataGridView2_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Right && e.ColumnIndex > -1 && e.RowIndex > -1) //点击的是鼠标右键,并且不是表头
{
//选中行的情况,排除在非选中行上鼠标右键
if (dataGridView2.SelectedRows.Count > 0)
{
if (!dataGridView2.SelectedRows.Contains(dataGridView2.Rows[e.RowIndex]))
{
return;
}
}
//多选情况下只提供复制功能
if (dataGridView2.SelectedCells.Count > 1)
{
//排除鼠标右键不在选中单元格上的情况
if (!dataGridView2.SelectedCells.Contains(dataGridView2.Rows[e.RowIndex].Cells[e.ColumnIndex]))
{
return;
}
col_编辑.Visible = false;
col_删除列.Visible = false;
col_添加列.Visible = false;
this.colContext.Show(MousePosition.X, MousePosition.Y);
return;
}
//如果当前显示的是数据
if (!radStruct.Checked || radView.Checked)
{
for (int i = 0; i < dataGridView2.Rows.Count; i++)
{
dataGridView2.Rows[i].Selected = false;
}
this.dataGridView2.Rows[e.RowIndex].Cells[e.ColumnIndex].Selected = true;
col_编辑.Visible = false;
col_删除列.Visible = false;
col_添加列.Visible = false;
this.colContext.Show(MousePosition.X, MousePosition.Y); //MousePosition.X, MousePosition.Y 是为了让菜单在所选行的位置显示
return;
}
if (e.ColumnIndex == 1 || e.ColumnIndex == 2 || e.ColumnIndex == 3 || e.ColumnIndex == 7)
{
//右键选中单元格
colColIndex = e.ColumnIndex;//记录编辑的列索引
colRowIndex = e.RowIndex;//记录编辑的行索引
colOldval = dataGridView2.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString();
for (int i = 0; i < dataGridView2.Rows.Count; i++)
{
dataGridView2.Rows[i].Selected = false;
}
this.dataGridView2.Rows[e.RowIndex].Cells[e.ColumnIndex].Selected = true;
col_编辑.Visible = true;
col_删除列.Visible = true;
col_添加列.Visible = true;
this.colContext.Show(MousePosition.X, MousePosition.Y); //MousePosition.X, MousePosition.Y 是为了让菜单在所选行的位置显示
}
else if (e.ColumnIndex == 4 || e.ColumnIndex == 5)
{
//右键选中单元格
colColIndex = e.ColumnIndex;//记录编辑的列索引
colRowIndex = e.RowIndex;//记录编辑的行索引
for (int i = 0; i < dataGridView2.Rows.Count; i++)
{
dataGridView2.Rows[i].Selected = false;
}
this.dataGridView2.Rows[e.RowIndex].Cells[e.ColumnIndex].Selected = true;
this.alterContext.Show(MousePosition.X, MousePosition.Y); //MousePosition.X, MousePosition.Y 是为了让菜单在所选行的位置显示
}
}
}
int colColIndex = 0;//记录列框单元格列索引
int colRowIndex = 0;//记录列框单元格行
string colOldval = "";
private void toolStripMenuItem3_Click(object sender, EventArgs e)
{
this.dataGridView2.BeginEdit(false);
}
private void toolStripMenuItem5_Click(object sender, EventArgs e)
{
Clipboard.SetDataObject(dataGridView2.GetClipboardContent());
}
private void toolStripMenuItem6_Click(object sender, EventArgs e)
{
if (dataGridView2.CurrentCell == null)
return;
int insertRowIndex = dataGridView2.CurrentCell.RowIndex;
// 获取剪切板的内容,并按行分割
string pasteText = Clipboard.GetText();
dataGridView2.CurrentCell.Value = pasteText;
}
private void dataGridView2_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
string newVal = dataGridView2.CurrentCell.Value.ToString();
SqlServerIDbAccess iDb = DataTransfer.iDb as SqlServerIDbAccess;
if (newVal != colOldval)
{
if (System.Windows.Forms.DialogResult.OK == MessageBox.Show("是否更新到数据库?", "是否更新", MessageBoxButtons.OKCancel))
{
if (colColIndex == 1)
{
try
{
iDb.RenameColumn(textBox1.Text, colOldval, dataGridView2.CurrentCell.Value.ToString());
MessageBox.Show("更新成功!");
}
catch (Exception ex)
{
MessageBox.Show("更新失败:" + ex.ToString());
}
dataGridView2.CurrentCell.ReadOnly = false;
}
else if (colColIndex == 2)
{
try
{
iDb.AlterColumnType(textBox1.Text, dataGridView2.Rows[colRowIndex].Cells[1].Value.ToString(), dataGridView2.Rows[colRowIndex].Cells[2].Value.ToString(), false);
MessageBox.Show("更新成功!");
}
catch (Exception ex)
{
MessageBox.Show("更新失败:" + ex.ToString());
}
dataGridView2.CurrentCell.ReadOnly = false;
}
else if (colColIndex == 3)
{
try
{
iDb.SaveColumnDesc(textBox1.Text, dataGridView2.Rows[colRowIndex].Cells[1].Value.ToString(), newVal);
MessageBox.Show("更新成功!");
}
catch (Exception ex)
{
MessageBox.Show("更新失败:" + ex.ToString());
}
dataGridView2.CurrentCell.ReadOnly = false;
}
else if (colColIndex == 7)
{
try
{
newVal = newVal.Trim().Trim(new char[] { ' ', '(', ')' }).Trim();
iDb.SaveColumnDefault(textBox1.Text, dataGridView2.Rows[colRowIndex].Cells[1].Value.ToString(), newVal);
MessageBox.Show("更新成功!");
}
catch (Exception ex)
{
MessageBox.Show("更新失败:" + ex.ToString());
}
dataGridView2.CurrentCell.ReadOnly = false;
}
}
else
{
dataGridView2.CurrentCell.Value = colOldval;
}
}
}
private void 切换连接ToolStripMenuItem_Click(object sender, EventArgs e)
{
Login login = new Login();
login.ShowDialog();
}
private async void button5_Click(object sender, EventArgs e)
{
btnDoc.Text = "执行中...";
btnDoc.Enabled = false;
List<TableStruct> res = null;
try
{
res = await CreateTableList();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
btnDoc.Text = "生成表结构说明";
btnDoc.Enabled = true;
return;
}
if (res.Count == 0)
{
MessageBox.Show("请选中要生成表结构的文档!");
btnDoc.Text = "生成表结构说明";
btnDoc.Enabled = true;
return;
}
try
{
DocX doc = null;
await Task.Factory.StartNew((Action)(() =>
{
doc = DocX.Load("表结构说明模板.docx");
for (int i = 0; i < res.Count; i++)
{
Paragraph ptmp = doc.InsertParagraph(doc.Paragraphs[0]);
Novacode.Table tbltmp = doc.InsertTable(doc.Tables[0]);
SetBorder(tbltmp);
ptmp.ReplaceText("#TableName#", res[i].Name);
tbltmp.Rows[0].Cells[1].ReplaceText("#TableName#", res[i].Name);
tbltmp.Rows[1].Cells[1].ReplaceText("#TableDesc#", res[i].Desc ?? "");
tbltmp.Rows[2].Cells[1].ReplaceText("#PrimaryKey#", res[i].PrimaryKey);
for (int ii = 0; ii < res[i].Columns.Count; ii++)
{
Novacode.Row rowtmp = tbltmp.InsertRow(tbltmp.Rows[4]);
rowtmp.Cells[0].ReplaceText("#ColumnName#", res[i].Columns[ii].Name);
rowtmp.Cells[1].ReplaceText("#ColumnType#", res[i].Columns[ii].Type);
rowtmp.Cells[2].ReplaceText("#ColumnDesc#", res[i].Columns[ii].Desc ?? "");
string remark = "";
if (res[i].Columns[ii].IsIdentity)
{
remark += "自增;";
}
if (res[i].Columns[ii].IsNullable)
{
remark += "可空;";
}
if (!string.IsNullOrWhiteSpace(res[i].Columns[ii].Default))
{
remark += "默认(" + res[i].Columns[ii].Default + ");";
}
rowtmp.Cells[3].ReplaceText("#ColumnRemark#", remark);
}
tbltmp.RemoveRow(4);
}
doc.RemoveParagraph(doc.Paragraphs[0]);
doc.Tables[0].Remove();
}));
btnDoc.Text = "生成表结构说明";
btnDoc.Enabled = true;
folderBrowserDialog1.ShowNewFolderButton = true;
folderBrowserDialog1.Description = "请选择文件路径";
folderBrowserDialog1.SelectedPath = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
System.Windows.Forms.DialogResult diares = folderBrowserDialog1.ShowDialog();
if (diares == DialogResult.OK || diares == DialogResult.Yes)
{
string str = folderBrowserDialog1.SelectedPath;
string filepath = Path.Combine(str, DateTime.Now.ToString("yyyy-MM-dd") + "结构说明.docx");
if (File.Exists(filepath))
{
if (MessageBox.Show("发现已存在文件" + filepath + "是否覆盖?", "文件输出选择", MessageBoxButtons.OKCancel) != DialogResult.OK)
{
return;
}
else
{
File.Delete(filepath);
}
}
doc.SaveAs(filepath);
MessageBox.Show("生成成功!");
System.Diagnostics.Process.Start("Explorer.exe", "/select," + filepath);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
finally
{
btnDoc.Text = "生成表结构说明";
btnDoc.Enabled = true;
}
}
private async Task<List<TableStruct>> CreateTableList()
{
SqlServerIDbAccess iDb = IDBFactory.CreateIDB(DataTransfer.iDb.ConnectionString, "SQLSERVER") as SqlServerIDbAccess;
List<string> tblNames = new List<string>();
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
if ((dataGridView1.Rows[i].Cells[0].Value as bool?) == true)
{
tblNames.Add(dataGridView1.Rows[i].Cells[2].Value.ToString());
}
}
return await Task.Factory.StartNew<List<TableStruct>>(() => { return iDb.GetTableStructs(tblNames); });
}
private void SetBorder(Novacode.Table tbl)
{
for (int i = 0; i < tbl.Rows.Count; i++)
{
for (int ii = 0; ii < tbl.Rows[i].Cells.Count; ii++)
{
tbl.Rows[i].Cells[ii].SetBorder(TableCellBorderType.Top, new Border(Novacode.BorderStyle.Tcbs_single, 0, 0, Color.Black));
tbl.Rows[i].Cells[ii].SetBorder(TableCellBorderType.Bottom, new Border(Novacode.BorderStyle.Tcbs_single, 0, 0, Color.Black));
tbl.Rows[i].Cells[ii].SetBorder(TableCellBorderType.Left, new Border(Novacode.BorderStyle.Tcbs_single, 0, 0, Color.Black));
tbl.Rows[i].Cells[ii].SetBorder(TableCellBorderType.Right, new Border(Novacode.BorderStyle.Tcbs_single, 0, 0, Color.Black));
}
}
}
private async void button6_Click(object sender, EventArgs e)
{
btnXML.Text = "执行中...";
btnXML.Enabled = false;
List<TableStruct> res = null;
try
{
res = await CreateTableList();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
btnXML.Text = "导出表结构XML";
btnXML.Enabled = true;
return;
}
if (res.Count == 0)
{
MessageBox.Show("请选中表!");
btnXML.Text = "导出表结构XML";
btnXML.Enabled = true;
return;
}
StringBuilder sb = new StringBuilder();
sb.AppendLine(@"<?xml version=""1.0"" encoding=""utf-8"" ?>");
for (int i = 0; i < res.Count; i++)
{
sb.AppendLine("<" + res[i].Name + " prikey=\"" + res[i].PrimaryKey + "\" desc=\"" + res[i].Desc + "\">");
for (int j = 0; j < res[i].Columns.Count; j++)
{
sb.AppendLine(string.Format(" <{0} type=\"" + res[i].Columns[j].FinalType + "\"><![CDATA[{1}]]></{0}>", res[i].Columns[j].Name, res[i].Columns[j].Desc));
}
sb.AppendLine("</" + res[i].Name + ">");
}
btnXML.Text = "导出表结构XML";
btnXML.Enabled = true;
folderBrowserDialog1.ShowNewFolderButton = true;
folderBrowserDialog1.Description = "请选择文件路径";
folderBrowserDialog1.SelectedPath = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
System.Windows.Forms.DialogResult diares = folderBrowserDialog1.ShowDialog();
if (diares == DialogResult.OK || diares == DialogResult.Yes)
{
string str = folderBrowserDialog1.SelectedPath;
string filepath = Path.Combine(str, DateTime.Now.ToString("yyyy-MM-dd") + "结构说明.xml");
if (File.Exists(filepath))
{
if (MessageBox.Show("发现已存在文件" + filepath + "是否覆盖?", "文件输出选择", MessageBoxButtons.OKCancel) != DialogResult.OK)
{
return;
}
else
{
File.Delete(filepath);
}
}
System.IO.File.WriteAllText(filepath, sb.ToString());
MessageBox.Show("生成成功!");
System.Diagnostics.Process.Start("Explorer.exe", "/select," + filepath);
}
}
private void 编辑连接ToolStripMenuItem_Click(object sender, EventArgs e)
{
ConnEdit edit = new ConnEdit();
edit.ShowDialog();
}
private void 删除表ToolStripMenuItem_Click(object sender, EventArgs e)
{
if (MessageBox.Show("确定删除表?", "确认框", MessageBoxButtons.OKCancel) == System.Windows.Forms.DialogResult.OK)
{
DBUtil.SqlServerIDbAccess iDb = DataTransfer.iDb as SqlServerIDbAccess;
string tblname = dataGridView1.Rows[tblRowEdit].Cells[2].Value.ToString();
iDb.DropTable(tblname);
this.OnLoad(null);
}
}
private void 新建表ToolStripMenuItem_Click(object sender, EventArgs e)
{
AddTable table = new AddTable();
table.ShowDialog();
}
private void 删除列ToolStripMenuItem_Click(object sender, EventArgs e)
{
string colname = dataGridView2.Rows[colRowIndex].Cells[1].Value.ToString();
if (MessageBox.Show("是否删除列?", "确认框", MessageBoxButtons.OKCancel) == System.Windows.Forms.DialogResult.OK)
{
DBUtil.SqlServerIDbAccess iDb = DataTransfer.iDb as DBUtil.SqlServerIDbAccess;
try
{
iDb.DropColumn(textBox1.Text, colname);
MessageBox.Show("删除成功!");
InitTableInfo();
}
catch (Exception ex)
{
MessageBox.Show("删除失败:" + ex.ToString());
}
}
}
private void toolStripMenuItem2_Click_1(object sender, EventArgs e)
{
DBUtil.SqlServerIDbAccess iDb = DataTransfer.iDb as DBUtil.SqlServerIDbAccess;
string colname = dataGridView2.Rows[colRowIndex].Cells[1].Value.ToString();
string colType = dataGridView2.Rows[colRowIndex].Cells[2].Value.ToString();
string tblname = textBox1.Text;
try
{
if (colColIndex == 4)
{
iDb.AlterColumnNullAble(tblname, colname, colType, true);
}
else if (colColIndex == 5)
{
iDb.SaveColumnUnique(tblname, colname, true);
}
dataGridView2.Rows[colRowIndex].Cells[colColIndex].Value = "是";
MessageBox.Show("切换成功!");
}
catch (Exception ex)
{
MessageBox.Show("切换出错:" + ex.ToString());
}
}
private void toolStripMenuItem4_Click(object sender, EventArgs e)
{
DBUtil.SqlServerIDbAccess iDb = DataTransfer.iDb as DBUtil.SqlServerIDbAccess;
string colname = dataGridView2.Rows[colRowIndex].Cells[1].Value.ToString();
string colType = dataGridView2.Rows[colRowIndex].Cells[2].Value.ToString();
string tblname = textBox1.Text;
try
{
if (colColIndex == 4)
{
iDb.AlterColumnNullAble(tblname, colname, colType, false);
}
else if (colColIndex == 5)
{
iDb.SaveColumnUnique(tblname, colname, false);
}
dataGridView2.Rows[colRowIndex].Cells[colColIndex].Value = "否";
MessageBox.Show("切换成功!");
}
catch (Exception ex)
{
MessageBox.Show("切换出错:" + ex.ToString());
}
}
private void 添加列ToolStripMenuItem_Click(object sender, EventArgs e)
{
AddCol col = new AddCol();
DataTransfer.TableName = textBox1.Text;
col.ShowDialog();
}
private void DisableAllBtn()
{
btnTMP.Enabled = false;
btnCode.Enabled = false;
btnDoc.Enabled = false;
btnSQL.Enabled = false;
btnXML.Enabled = false;
}
private void EnableAllBtn()
{
btnTMP.Enabled = true;
btnCode.Enabled = true;
btnDoc.Enabled = true;
btnSQL.Enabled = true;
btnXML.Enabled = true;
}
private void radioButton1_Click(object sender, EventArgs e)
{
CHKTable();
EnableAllBtn();
}
private void radioButton2_Click(object sender, EventArgs e)
{
CHKView();
EnableAllBtn();
btnCode.Enabled = false;
btnTMP.Enabled = false;
btnDoc.Enabled = false;
btnXML.Enabled = false;
}