lu
2024-12-06 770c671fb4ad9bf3691b1250bc10f9c4087edf97
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
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using Gs.DevApp.ToolBox;
using Newtonsoft.Json;
using Oracle.ManagedDataAccess.Client;
 
namespace Gs.DevApp.DevFrm.Sys
{
    public partial class EasyCode : XtraForm
    {
        private readonly string oracleCon =
            "User Id=system;Password=admin110;Data Source=localhost:1521/MYTEST2;";
 
        //this.txt_sqlconn.Text = "Data Source=2kn4sh70028.vicp.fun,26144;Initial Catalog=gs_Mes;User ID=sa;Password =admin110";
        private readonly string sqlCon =
            "Data Source=192.168.1.146;Initial Catalog=GS_MES;User ID=sa;Password =qixi123";
 
        public EasyCode()
        {
            InitializeComponent();
            getTable();
            btnPathSelect.Click += (s, e) =>
            {
                var _GetfilePath = FileSelector.BrowseFilePathForSave();
                txtPath.Text = _GetfilePath;
            };
            btnUpSelect.Click += BtnUpSelect_Click;
            btnUp.Click += BtnUp_Click;
        }
 
        #region 批量读取
 
        private void btnTable_Click(object sender, EventArgs e)
        {
            using (var connection = new OracleConnection(oracleCon))
            {
                try
                {
                    connection.Open();
                    using (var command = connection.CreateCommand())
                    {
                        command.CommandText =
                            "SELECT * FROM " + txtTable1.Text.Trim();
                        using (var oracleDataReader = command.ExecuteReader())
                        {
                            using (var bulkCopy = new SqlBulkCopy(sqlCon))
                            {
                                bulkCopy.DestinationTableName =
                                    txtTable1.Text.Trim();
                                bulkCopy.WriteToServer(oracleDataReader);
                            }
                        }
                    }
 
                    MsgHelper.ShowInformation("提示,操作成功!");
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    MsgHelper.ShowError("提示:" + ex.Message);
                }
                finally
                {
                    if (connection.State != ConnectionState.Closed)
                        connection.Close();
                }
            }
        }
 
        #endregion
 
        #region 批量准备
 
        private void btnRemark_Click(object sender, EventArgs e)
        {
            if (txtTable2.Text.Trim().Length <= 0)
            {
                MessageBox.Show("请输入表名");
                return;
            }
 
            using (var conn = new SqlConnection(sqlCon))
            {
                using (var cmd = new SqlCommand("[tmp_property]", conn))
                {
                    try
                    {
                        conn.Open();
                        cmd.CommandType = CommandType.StoredProcedure;
                        SqlParameter[] parameters =
                        {
                            new SqlParameter("@tabname", txtTable2.Text.Trim()),
                            new SqlParameter("@com", txtOracleDec.Text.Trim())
                        };
                        foreach (var parameter in parameters)
                            cmd.Parameters.Add(parameter);
                        cmd.ExecuteNonQuery();
                        MsgHelper.ShowInformation("提示,操作成功!");
                    }
                    catch (Exception ex)
                    {
                        MsgHelper.ShowError("提示:" + ex.Message);
                    }
                    finally
                    {
                        conn.Close();
                    }
                }
            }
        }
 
        #endregion
 
        #region 代码生成
 
        /// <summary>
        ///     确定生成
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnOk_Click(object sender, EventArgs e)
        {
            if (txt_table.EditValue == null ||
                string.IsNullOrEmpty(txt_table.EditValue.ToString()))
            {
                MessageBox.Show("请选择表");
                return;
            }
 
            if (txtPath.Text.Trim() == "")
            {
                MessageBox.Show("请选择生成路径");
                return;
            }
 
            var _strName = _getClassName();
            var list = txt_table.Properties.Items.GetCheckedValues();
            var dt1 = _getCol(_getTableName(list[0].ToString()));
            var dtList = new List<DataTable>();
            var j = 0;
            foreach (var obj in list)
            {
                if (j > 0)
                    dtList.Add(_getCol(_getTableName(obj.ToString())));
                j++;
            }
 
            ReadMainFile(dt1, _strName, dtList);
            ReadDesignerFile(dt1, _strName, dtList);
            ReadResxFile(dt1, _strName);
            var path = txtPath.Text;
            lbMsg.Text = "生成成功,路径:" + path;
        }
 
        /// <summary>
        ///     读取数据库表
        /// </summary>
        private void getTable()
        {
            var dset = new DataSet();
            using (var connection = new SqlConnection(sqlCon))
            {
                connection.Open();
                var cmd = new SqlCommand();
                cmd.Connection = connection;
                cmd.CommandText =
                    "SELECT TABLE_NAME,(SELECT top 1 value AS ExtendedPropertyValue FROM sys.extended_properties WHERE minor_id=0 and major_id = OBJECT_ID(''+TABLE_NAME+''))  as TABLE_DESC  FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = 'BASE TABLE' order by TABLE_NAME asc";
                using (var dt = new SqlDataAdapter(cmd))
                {
                    dt.Fill(dset, "0");
                }
            }
 
            if (dset != null && dset.Tables.Count > 0)
            {
                txt_table.Properties.Items.Clear();
                var strs = new string[dset.Tables[0].Rows.Count];
                for (var i = 0; i < dset.Tables[0].Rows.Count; i++)
                    strs[i] = dset.Tables[0].Rows[i]["TABLE_NAME"] + "(" +
                              dset.Tables[0].Rows[i]["TABLE_DESC"] + ")";
                txt_table.Properties.Items.AddRange(strs);
            }
        }
 
        /// <summary>
        ///     读取列名
        /// </summary>
        /// <returns></returns>
        private DataTable _getCol(string tableName)
        {
            var sb = new StringBuilder();
            sb.Append(
                "SELECT COLUMN_NAME = a.name, DATA_TYPE = b.name, REMARK = isnull(g.[value],a.name) ");
            sb.Append(
                " FROM syscolumns a  left join systypes b on a.xusertype = b.xusertype inner join sysobjects d on a.id = d.id and d.xtype = 'U'  and d.name<>'dtproperties' ");
            sb.Append(" left join syscomments e on a.cdefault = e.id ");
            sb.Append(
                " left join sys.extended_properties g on a.id = G.major_id and a.colid = g.minor_id ");
            sb.Append(
                " left join sys.extended_properties f on d.id = f.major_id and f.minor_id = 0 ");
            sb.Append(" where d.name = '" + tableName + "'");
            sb.Append(" order by  a.id, a.colorder");
            var dset = new DataSet();
            using (var connection = new SqlConnection(sqlCon))
            {
                connection.Open();
                var cmd = new SqlCommand();
                cmd.Connection = connection;
                cmd.CommandText = sb.ToString();
                using (var dt = new SqlDataAdapter(cmd))
                {
                    dt.Fill(dset, "0");
                }
            }
 
            if (dset != null && dset.Tables.Count > 0) return dset.Tables[0];
            return null;
        }
 
        /// <summary>
        ///     读模版路径
        /// </summary>
        /// <param name="fileType">cs,Designer.cs</param>
        /// <returns></returns>
        private string _getTemple(string fileType)
        {
            if (txt_table.Properties.Items.GetCheckedValues().Count < 2)
                return @"\templeEasyCode\category1\category." + fileType;
            return @"\templeEasyCode\category2\category." + fileType;
        }
 
        /// <summary>
        ///     读取文件类名
        /// </summary>
        /// <returns></returns>
        private string _getClassName()
        {
            var str = UtilityHelper.ToCamelCase(_getMainTable());
            var result = str.Substring(0, 1).ToUpper() + str.Substring(1);
            return "Frm_" + result;
        }
 
        /// <summary>
        ///     读取主表
        /// </summary>
        /// <returns></returns>
        private string _getMainTable()
        {
            var ckList = txt_table.Properties.Items.GetCheckedValues();
            var _strName = ckList[0].ToString();
            foreach (string str in ckList)
                if (str.Length < _strName.Length)
                    _strName = str;
            return _getTableName(_strName);
        }
 
        /// <summary>
        ///     截取表名
        /// </summary>
        /// <param name="strName"></param>
        /// <returns></returns>
        private string _getTableName(string strName)
        {
            var _tableName = strName.Split('(')[0];
            return _tableName;
        }
 
        /// <summary>
        ///     第一个文件
        /// </summary>
        /// <param name="dt"></param>
        /// <param name="_FileName"></param>
        private void ReadMainFile(DataTable dt, string _FileName,
            List<DataTable> dtList = null)
        {
            var _tempPath = Application.StartupPath + _getTemple("cs");
            var contents = File.ReadAllText(_tempPath);
            var line = Environment.NewLine;
            var sb = new StringBuilder();
            sb.Append(contents);
            sb.Replace("{clsName}", _getClassName());
            var _fullPath = txtPath.Text + "\\" + _FileName + ".cs";
            if (File.Exists(_fullPath))
                File.Delete(_fullPath);
            var sw = File.CreateText(_fullPath);
            sw.Write(sb.ToString());
            sw.Close();
            sw.Dispose();
        }
 
        /// <summary>
        ///     第二个文件
        /// </summary>
        /// <param name="dt"></param>
        /// <param name="_FileName"></param>
        private void ReadDesignerFile(DataTable dt, string _FileName,
            List<DataTable> dtList = null)
        {
            var _tempPath = Application.StartupPath + _getTemple("Designer.cs");
            var contents = File.ReadAllText(_tempPath);
            var strbuilder = new StringBuilder();
            var line = Environment.NewLine;
            strbuilder.Append(contents);
            strbuilder.Replace("{clsName}", _getClassName());
            var _RowCount = dt.Rows.Count;
            if (dt.Rows.Count > 0)
            {
                var _FieldName = ""; //字段名
                var _FieldText = ""; //字段文本
                var _FieldNameSql = ""; //原始字段名,主要用于查询
                int _x = 0, _y = 0, _Tabindex = 210;
                var _ControlType = "TextEdit"; //控件类型,目前不作判断都为文本框
                var _lbType = "LabelControl";
                var _gvItemCol = new StringBuilder();
                for (var c = 0; c < _RowCount; c++)
                {
                    _x = 20 + c % 4 * 250; //计算控件在X轴位置
                    if (c % 4 == 0) _y = 20 + 32 * c / 4; //计算控件在Y轴的位置
                    _Tabindex = c % 4 + c / 4 + _Tabindex;
                    switch (dt.Rows[c]["DATA_TYPE"].ToString())
                    {
                        default:
                            _FieldName = UtilityHelper.ToCamelCase(
                                dt.Rows[c]["COLUMN_NAME"].ToString().Trim());
                            _FieldText = dt.Rows[c]["REMARK"].ToString().Trim();
                            _FieldNameSql = "query_a." +
                                            dt.Rows[c]["COLUMN_NAME"].ToString()
                                                .Trim();
                            break;
                    }
 
                    var _labName = "lb_" + _FieldName; //label名
                    var _txtName = "txt_" + _FieldName; //文本框名
                    var _gvName = "gv_" + _FieldName; //gv中的列名
                    if (_gvItemCol.Length > 0)
                        _gvItemCol.Append(",");
                    _gvItemCol.Append("this." + _gvName);
                    //BQSetControlObject  实例化控件
                    var BQSetControlObject = "BQSetControlObject" + line +
                                             "this." + _labName +
                                             " =  new DevExpress.XtraEditors." +
                                             _lbType + "();" + line;
                    BQSetControlObject = BQSetControlObject + "this." +
                                         _txtName +
                                         " = new DevExpress.XtraEditors." +
                                         _ControlType + "(); " + line;
                    BQSetControlObject = BQSetControlObject + "this." +
                                         _gvName +
                                         " = new DevExpress.XtraGrid.Columns.GridColumn(); " +
                                         line;
                    strbuilder.Replace("BQSetControlObject",
                        BQSetControlObject);
                    //BQSetControlInfo  设置控件信息位置大小名称
                    var BQSetControlInfo = "BQSetControlInfo" + line
                        //label
                        + "//" + line
                        + "//" + _labName + line
                        + "//" + line
                        + "this." + _labName +
                        ".Location = new System.Drawing.Point(" + _x + ", " +
                        _y + ");" + line
                        + "this." + _labName + ".Name = \"" + _labName + "\";" +
                        line
                        + "this." + _labName +
                        ".Size = new System.Drawing.Size(150,21);" + line
                        + "this." + _labName + ".TabIndex = " + _Tabindex +
                        ";" + line
                        + "this." + _labName +
                        ".Appearance.Font = new System.Drawing.Font(\"Tahoma\", 10F);" +
                        line
                        + "this." + _labName + ".Text = \"" + _FieldText +
                        " \";" + line
                        //文本
                        + "//" + line
                        + "//" + _txtName + line
                        + "//" + line
                        + "this." + _txtName +
                        ".Location = new System.Drawing.Point(" + (_x + 60) +
                        ", " + _y + ");" + line
                        + "this." + _txtName + ".Name = \"" + _txtName + "\";" +
                        line
                        + "this." + _txtName +
                        ".Size = new System.Drawing.Size(150,21);" + line
                        + "this." + _txtName + ".TabIndex = " + _Tabindex +
                        ";" + line
                        + "this." + _txtName +
                        ".Properties.Appearance.Font = new System.Drawing.Font(\"Tahoma\", 10F);" +
                        line
                        //主gridView
                        + "//" + line
                        + "//" + _gvName + line
                        + "//" + line
                        + "this." + _gvName +
                        ".AppearanceCell.Font = new System.Drawing.Font(\"Tahoma\", 10F);" +
                        line
                        + "this." + _gvName +
                        ".AppearanceCell.Options.UseFont = true;" + line
                        + "this." + _gvName + ".FieldName = \"" + _FieldName +
                        "\";" + line
                        + "this." + _gvName + ".Caption =  \"" + _FieldText +
                        " \";" + line
                        + "this." + _gvName + ".Tag = \"" + _FieldNameSql +
                        "\";" + line
                        + "this." + _gvName + ".MinWidth =50;" + line
                        + "this." + _gvName + ".Visible = true;" + line
                        + "this." + _gvName + ".Width =94;" + line
                        + "this." + _gvName +
                        ".OptionsColumn.AllowEdit = false;" + line
                        + "this." + _gvName + ".VisibleIndex = 0;" + line;
                    strbuilder.Replace("BQSetControlInfo", BQSetControlInfo);
                    //BQAddControltoMContainer//将控件添加到panel1容器中
                    var BQAddControltoMContainer = "BQAddControltoMContainer" +
                                                   line +
                                                   "  this.panel1.Controls.Add(this." +
                                                   _labName + ");" + line;
                    BQAddControltoMContainer +=
                        " this.panel1.Controls.Add(this." + _txtName + ");" +
                        line;
                    strbuilder.Replace("BQAddControltoMContainer",
                        BQAddControltoMContainer);
                    //BQCreateConrolObejct //创建控件 
                    var BQCreateConrolObejct = "BQCreateConrolObejct" + line +
                                               "private DevExpress.XtraEditors." +
                                               _lbType + " " + _labName + ";" +
                                               line;
                    BQCreateConrolObejct += "private DevExpress.XtraEditors." +
                                            _ControlType + " " + _txtName +
                                            ";" + line;
                    BQCreateConrolObejct +=
                        " private DevExpress.XtraGrid.Columns.GridColumn " +
                        _gvName + ";" + line;
                    strbuilder.Replace("BQCreateConrolObejct",
                        BQCreateConrolObejct);
                }
 
                //把列加入到gridview中
                strbuilder.Replace("{colList}", _gvItemCol.ToString());
                //构建明细gridview1
                if (dtList != null && dtList.Count > 0)
                {
                    var _idx = 1;
                    var _tabItem = new StringBuilder();
                    foreach (var dt2 in dtList)
                    {
                        var _tabMxPage = "tabMxPage" + _idx;
                        var _gcMx = "gcMx" + _idx;
                        var _gvMx = "gvMx" + _idx;
                        if (_tabItem.Length > 0)
                            _tabItem.Append(",");
                        _tabItem.Append("this." + _tabMxPage);
                        //BQSetControlObject 实例化控件
                        var ss = "BQSetControlObject" + line;
                        ss += " this." + _tabMxPage +
                              " = new DevExpress.XtraTab.XtraTabPage();" + line;
                        ss += " this." + _gcMx +
                              " = new DevExpress.XtraGrid.GridControl();" +
                              line;
                        ss += " this." + _gvMx +
                              " = new DevExpress.XtraGrid.Views.Grid.GridView();";
                        strbuilder.Replace("BQSetControlObject", ss);
                        //BQSetControlInfo 设置控件信息位置大小名称
                        var inf = "BQSetControlInfo" + line
                            + "this." + _tabMxPage + ".Controls.Add(this." +
                            _gcMx + ");"
                            + "this." + _tabMxPage + ".Name = \"" + _tabMxPage +
                            "\";" + line
                            + "this." + _tabMxPage +
                            ".Size = new System.Drawing.Size(1186, 112);" + line
                            + "this." + _tabMxPage + ".Text = \"明细" + _idx +
                            "\";" + line
                            + "//" + line
                            + "//" + _gcMx + line
                            + "//" + line
                            + " this." + _gcMx +
                            ".Dock = System.Windows.Forms.DockStyle.Fill;" +
                            line
                            + " this." + _gcMx +
                            ".Location = new System.Drawing.Point(0, 0);" + line
                            + " this." + _gcMx + ".MainView = this." + _gvMx +
                            ";" + line
                            + " this." + _gcMx + ".Name = \"" + _gcMx + "\";" +
                            line
                            + " this." + _gcMx +
                            ".Size = new System.Drawing.Size(1124, 215);" + line
                            + " this." + _gcMx + ".TabIndex = 0;" + line
                            + " this." + _gcMx +
                            ".ViewCollection.AddRange(new DevExpress.XtraGrid.Views.Base.BaseView[] { this." +
                            _gvMx + "});" + line
                            + "//" + line
                            + "//" + _gvMx + line
                            + "//" + line
                            + " this." + _gvMx + ".GridControl = this." +
                            _gcMx + ";" + line
                            + " this." + _gvMx + ".Name = \"" + _gvMx + "\";" +
                            line
                            + " this." + _gvMx +
                            ".OptionsFind.ShowSearchNavButtons = false;" + line
                            + " this." + _gvMx +
                            ".OptionsView.NewItemRowPosition = DevExpress.XtraGrid.Views.Grid.NewItemRowPosition.Bottom;" +
                            line
                            + " this." + _gvMx +
                            ".OptionsBehavior.AllowAddRows = DevExpress.Utils.DefaultBoolean.True;" +
                            line
                            + " this." + _gvMx +
                            ".OptionsView.ShowGroupPanel = false;" + line
                            + " this." + _gvMx +
                            ".Columns.AddRange(new DevExpress.XtraGrid.Columns.GridColumn[] { {colMx1List}});";
                        strbuilder.Replace("BQSetControlInfo", inf);
                        //BQCreateConrolObejct //创建控件 
                        var _cc = "BQCreateConrolObejct" + line;
                        _cc += " private DevExpress.XtraTab.XtraTabPage " +
                               _tabMxPage + ";" + line;
                        _cc += " private DevExpress.XtraGrid.GridControl " +
                               _gcMx + ";" + line;
                        _cc +=
                            " private DevExpress.XtraGrid.Views.Grid.GridView " +
                            _gvMx + ";";
                        strbuilder.Replace("BQCreateConrolObejct", _cc);
                        var _layout = new StringBuilder();
                        _layout.Append("this.tabMxPage1.SuspendLayout();");
                        strbuilder.Replace("{layoutList}", _layout.ToString());
                        var _resume = new StringBuilder();
                        _resume.Append("this.tabMxPage1.ResumeLayout(false);");
                        strbuilder.Replace("{resumeLayout}",
                            _resume.ToString());
                        var _beg = new StringBuilder();
                        _beg.Append(
                            "((System.ComponentModel.ISupportInitialize)(this." +
                            _gcMx + ")).BeginInit();" + line);
                        _beg.Append(
                            " ((System.ComponentModel.ISupportInitialize)(this." +
                            _gvMx + ")).BeginInit();" + line);
                        strbuilder.Replace("{begInit}", _beg.ToString());
                        ///////////////////
                        _RowCount = dt2.Rows.Count;
                        _gvItemCol = new StringBuilder();
                        for (var c = 0; c < _RowCount; c++)
                        {
                            switch (dt2.Rows[c]["DATA_TYPE"].ToString())
                            {
                                default:
                                    _FieldName =
                                        UtilityHelper.ToCamelCase(
                                            dt2.Rows[c]["COLUMN_NAME"]
                                                .ToString());
                                    _FieldText = dt2.Rows[c]["REMARK"]
                                        .ToString();
                                    _FieldNameSql = "query_a." +
                                                    dt2.Rows[c]["COLUMN_NAME"];
                                    break;
                            }
 
                            var _gvColName = _gvMx + _FieldName;
                            if (_gvItemCol.Length > 0)
                                _gvItemCol.Append(",");
                            _gvItemCol.Append("this." + _gvColName);
                            //BQSetControlObject  实例化控件
                            var BQSetControlObject =
                                "BQSetControlObject" + line;
                            BQSetControlObject += "this." + _gvColName +
                                                  " = new DevExpress.XtraGrid.Columns.GridColumn(); ";
                            strbuilder.Replace("BQSetControlObject",
                                BQSetControlObject);
                            //BQSetControlInfo  设置控件信息位置大小名称
                            var BQSetControlInfo = "BQSetControlInfo" + line
                                + "this." + _gvColName +
                                ".AppearanceCell.Font = new System.Drawing.Font(\"Tahoma\", 10F);" +
                                line
                                + "this." + _gvColName +
                                ".AppearanceCell.Options.UseFont = true;" + line
                                + "this." + _gvColName + ".FieldName = \"" +
                                _FieldName + "\";" + line
                                + "this." + _gvColName + ".Caption =  \"" +
                                _FieldText + " \";" + line
                                + "this." + _gvColName + ".Tag = \"" +
                                _FieldNameSql + "\";" + line
                                + "this." + _gvColName + ".MinWidth = 50;" +
                                line
                                + "this." + _gvColName + ".Visible = true;" +
                                line
                                + "this." + _gvColName + ".Width =94;" + line
                                + "this." + _gvColName +
                                ".OptionsColumn.AllowEdit = true;" + line
                                + "this." + _gvColName + ".VisibleIndex = 0;" +
                                line;
                            strbuilder.Replace("BQSetControlInfo",
                                BQSetControlInfo);
                            //BQCreateConrolObejct //创建控件 
                            var BQCreateConrolObejct =
                                "BQCreateConrolObejct" + line;
                            BQCreateConrolObejct +=
                                " private DevExpress.XtraGrid.Columns.GridColumn " +
                                _gvColName + ";";
                            strbuilder.Replace("BQCreateConrolObejct",
                                BQCreateConrolObejct);
                        }
 
                        strbuilder.Replace("{colMx1List}",
                            _gvItemCol.ToString());
                        _idx++;
                    }
 
                    strbuilder.Replace("{tabMx1List}", _tabItem.ToString());
                }
 
                File.Delete(txtPath.Text + "\\" + _FileName + ".Designer.cs");
                var path = txtPath.Text;
                var sw =
                    File.CreateText(path + "\\" + _FileName + ".Designer.cs");
                sw.Write(strbuilder.ToString());
                sw.Close();
                sw.Dispose();
            }
        }
 
 
        /// <summary>
        ///     第三个文件
        /// </summary>
        /// <param name="dt"></param>
        /// <param name="_FileName"></param>
        private void ReadResxFile(DataTable dt, string _FileName)
        {
            var _Temppath = Application.StartupPath + _getTemple("resx");
            var contents = File.ReadAllText(_Temppath);
            var strbuilder = new StringBuilder();
            var line = Environment.NewLine;
            strbuilder.Append(contents);
            File.Delete(txtPath.Text + "\\" + _FileName + ".resx");
            var path = txtPath.Text;
            var sw = File.CreateText(path + "\\" + _FileName + ".resx");
            sw.Write(strbuilder.ToString());
            sw.Close();
            sw.Dispose();
        }
 
        #endregion
 
 
        #region 文件上传
 
        /// <summary>
        ///     选择上传文件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BtnUpSelect_Click(object sender, EventArgs e)
        {
            var openFileDialog = new OpenFileDialog();
            // 设置初始目录
            openFileDialog.InitialDirectory = "c:\\";
            // 设置文件过滤选项,如:"文本文件 (*.txt)|*.txt|所有文件 (*.*)|*.*"
            openFileDialog.Filter = "文本文件 (*.txt)|*.txt|所有文件 (*.*)|*.*";
            // 设置标题
            openFileDialog.Title = "选择文件";
            // 显示对话框
            var result = openFileDialog.ShowDialog();
            // 确认用户没有取消操作
            if (result == DialogResult.OK)
            {
                // 获取选中的文件路径
                var filePath = openFileDialog.FileName;
                Console.WriteLine($"选中的文件: {filePath}");
                txtUpUrl.Text = filePath;
            }
        }
 
        /// <summary>
        ///     确定上传
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private async  void BtnUp_Click(object sender, EventArgs e)
        {
            var WebApiUrl = ConfigurationSettings.AppSettings.Get("WebApiUrl");
            var address = WebApiUrl + "Upload/UploadFile";
            var filePath = txtUpUrl.Text.Trim();
            var apiUrl = address; // 替换为你的API地址
            await UploadFileAsync(apiUrl, filePath);
        }
 
        private async  Task UploadFileAsync(string url, string filePath)
        {
            using (var httpClient = new HttpClient())
            using (var form = new MultipartFormDataContent())
            {
                httpClient.DefaultRequestHeaders.Add("token",
                    UtilityHelper.GetBasicAuthTicket());
                using (var fs = File.OpenRead(filePath))
                using (var streamContent = new StreamContent(fs))
                {
                    form.Add(streamContent, "file", Path.GetFileName(filePath));
                    var response = await httpClient.PostAsync(url, form);
                    var responseString =
                        await response.Content.ReadAsStringAsync();
                    MessageBox.Show(responseString);
                }
            }
        }
 
        #endregion
 
 
        #region 推送测试
 
        /// <summary>
        ///     确定推送
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnMes_Click(object sender, EventArgs e)
        {
            var _list = new List<dynamic>();
            for (var i = 0; i < 20; i++)
                _list.Add(new
                {
                    t1 = "test1_" + i, //单位编号
                    t2 = "hi2_" + i //单位名称
                });
            var _json = new
            {
                category = "单位信息",
                list = _list
            };
            try
            {
                var _apiUrl = "http://localhost:5263/";
                var _strReturnJson = HttpPost(_apiUrl,
                    "ErpMes/PushMesByCategory",
                    JsonConvert.SerializeObject(_json));
                txtMesResult.Text = _strReturnJson;
            }
            catch (Exception ex)
            {
                MsgHelper.Warning("提示:" + ex.Message);
            }
        }
 
        /// <summary>
        /// </summary>
        /// <param name="url">apiUrl根地址</param>
        /// <param name="meth">apiUrl方法名</param>
        /// <param name="param">json参数</param>
        /// <returns></returns>
        public static string HttpPost(string url, string meth, string param)
        {
            //这是一个虚拟的token,不能删除
            var token = Guid.NewGuid() + "~" + Guid.NewGuid();
            var request = (HttpWebRequest)WebRequest.Create(url + meth);
            request.Method = "POST";
            request.ContentType = "application/json";
            request.Headers.Add("token", token);
            request.Accept = "*/*";
            request.Timeout = 15000;
            request.AllowAutoRedirect = false;
            StreamWriter requestStream = null;
            WebResponse response = null;
            string responseStr = null;
            try
            {
                requestStream = new StreamWriter(request.GetRequestStream());
                requestStream.Write(param);
                requestStream.Close();
                response = request.GetResponse();
                if (response != null)
                {
                    var reader = new StreamReader(response.GetResponseStream(),
                        Encoding.UTF8);
                    responseStr = reader.ReadToEnd();
                    reader.Close();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                request = null;
                requestStream = null;
                response = null;
            }
 
            return responseStr;
        }
 
        #endregion
    }
}