lu
2024-10-25 fe09fca3e77be09fed738a975ff9c5a508c2dd0d
DevApp/Gs.DevApp/DevFrm/Sys/EasyCode.cs
@@ -1,11 +1,12 @@
using DevExpress.XtraEditors;
using Newtonsoft.Json;
using Oracle.ManagedDataAccess.Client;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
@@ -14,25 +15,71 @@
{
    public partial class EasyCode : DevExpress.XtraEditors.XtraForm
    {
        //this.txt_sqlconn.Text = "Data Source=2kn4sh70028.vicp.fun,26144;Initial Catalog=gs_Mes;User ID=sa;Password =admin110";
        string sqlCon = "Data Source=192.168.1.146;Initial Catalog=GS_MES;User ID=sa;Password =qixi123";
        string oracleCon = "User Id=system;Password=admin110;Data Source=localhost:1521/MYTEST2;";
        public EasyCode()
        {
            InitializeComponent();
            this.txt_sqlconn.Text = "Data Source=2kn4sh70028.vicp.fun,26144;Initial Catalog=gs_Mes;User ID=sa;Password =admin110";
            getTable();
            this.btnPathSelect.Click += (s, e) =>
            {
                string _GetfilePath = FileSelector.BrowseFilePathForSave();
                txtPath.Text = _GetfilePath;
            };
            this.btnUpSelect.Click += BtnUpSelect_Click;
            this.btnUp.Click += BtnUp_Click;
        }
        #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;
            }
            string _strName = _getClassName();
            List<object> list = txt_table.Properties.Items.GetCheckedValues();
            DataTable dt1 = _getCol(_getTableName(list[0].ToString()));
            List<DataTable> dtList = new List<DataTable>();
            int j = 0;
            foreach (object obj in list)
            {
                if (j > 0)
                    dtList.Add(_getCol(_getTableName(obj.ToString())));
                j++;
            }
            ReadMainFile(dt1, _strName, dtList);
            ReadDesignerFile(dt1, _strName, dtList);
            ReadResxFile(dt1, _strName);
            string path = txtPath.Text;
            lbMsg.Text = "生成成功,路径:" + path;
            return;
        }
        /// <summary>
        /// 读取数据库表
        /// </summary>
        private void getTable()
        {
            string strConn = txt_sqlconn.Text.Trim();
            DataSet dset = new DataSet();
            using (SqlConnection connection = new SqlConnection(strConn))
            using (SqlConnection connection = new SqlConnection(sqlCon))
            {
                connection.Open();
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = connection;
                cmd.CommandText = "SELECT '--请选择--' as TABLE_NAME union all SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = 'BASE TABLE' order by TABLE_NAME desc";
                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 (SqlDataAdapter dt = new SqlDataAdapter(cmd))
                {
                    dt.Fill(dset, "0");
@@ -40,68 +87,36 @@
            }
            if (dset != null && dset.Tables.Count > 0)
            {
                txt_table.DataSource = dset.Tables[0];
                txt_table.DisplayMember = "TABLE_NAME";
                txt_table.ValueMember = "TABLE_NAME";
                txt_category.SelectedIndex = txt_table.SelectedIndex = 0;
                txt_table.Properties.Items.Clear();
                string[] strs = new string[dset.Tables[0].Rows.Count];
                for (int i = 0; i < dset.Tables[0].Rows.Count; i++)
                {
                    strs[i] = dset.Tables[0].Rows[i]["TABLE_NAME"].ToString() + "(" + dset.Tables[0].Rows[i]["TABLE_DESC"].ToString() + ")";
                }
                txt_table.Properties.Items.AddRange(strs);
            }
        }
        /// <summary>
        /// 选择生成路径
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnToPath_Click(object sender, EventArgs e)
        {
            string _GetfilePath = FileSelector.BrowseFilePathForSave();
            txtCreateDBPath.Text = _GetfilePath;
        }
        private void btnOk_Click(object sender, EventArgs e)
        {
            if (txt_sqlconn.Text.Trim() == "")
            {
                MessageBox.Show("请输入数据库连接串");
                return;
            }
            if (txt_table.SelectedIndex <= 0)
            {
                MessageBox.Show("请选择表");
                return;
            }
            if (txt_category.SelectedIndex <= 0)
            {
                MessageBox.Show("请选择类型");
                return;
            }
            if (txtCreateDBPath.Text.Trim() == "")
            {
                MessageBox.Show("请选择生成路径");
                return;
            }
            DataTable dt = _getCol();
            string _strName = _getClassName();
            ReadMainFile(dt, _strName);
            ReadDesignerFile(dt, _strName);
            ReadResxFile(dt, _strName);
            string path = txtCreateDBPath.Text;
            lbMsg.Text = "生成成功,路径:" + path;
        }
        /// <summary>
        /// 读取列名
        /// </summary>
        /// <returns></returns>
        private DataTable _getCol()
        private DataTable _getCol(string tableName)
        {
            string strConn = txt_sqlconn.Text.Trim();
            System.Text.StringBuilder 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");
            DataSet dset = new DataSet();
            using (SqlConnection connection = new SqlConnection(strConn))
            using (SqlConnection connection = new SqlConnection(sqlCon))
            {
                connection.Open();
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = connection;
                cmd.CommandText = "SELECT COLUMN_NAME,DATA_TYPE FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = N'" + txt_table.SelectedValue.ToString() + "'";
                cmd.CommandText = sb.ToString();
                using (SqlDataAdapter dt = new SqlDataAdapter(cmd))
                {
                    dt.Fill(dset, "0");
@@ -114,40 +129,69 @@
            return null;
        }
        /// <summary>
        /// 模版路径
        /// 读模版路径
        /// </summary>
        /// <param name="fileType">cs,Designer.cs</param>
        /// <returns></returns>
        private string _getTemple(string fileType)
        {
            return (@"\templeEasyCode\category1\category1." + 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()
        {
            return "Frm" + txt_table.SelectedValue.ToString();
            string str = ToolBox.UtilityHelper.ToCamelCase(_getMainTable());
            string result = str.Substring(0, 1).ToUpper() + str.Substring(1);
            return "Frm_" + result;
        }
        /// <summary>
        /// 读取主表
        /// </summary>
        /// <returns></returns>
        private string _getMainTable()
        {
            List<object> ckList = txt_table.Properties.Items.GetCheckedValues();
            string _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)
        {
            string _tableName = strName.Split('(')[0];
            return _tableName;
        }
        /// <summary>
        /// 第一个文件
        /// </summary>
        /// <param name="dt"></param>
        /// <param name="_FileName"></param>
        void ReadMainFile(DataTable dt, string _FileName)
        void ReadMainFile(DataTable dt, string _FileName, List<DataTable> dtList = null)
        {
            string _Temppath = Application.StartupPath + _getTemple("cs");
            string contents = System.IO.File.ReadAllText(_Temppath);
            string _tempPath = Application.StartupPath + _getTemple("cs");
            string contents = System.IO.File.ReadAllText(_tempPath);
            string line = Environment.NewLine;
            StringBuilder strbuilder = new StringBuilder();
            strbuilder.Append(contents);
            strbuilder.Replace("{clsName}", _getClassName());
            string _fullPath = txtCreateDBPath.Text + "\\" + _FileName + ".cs";
            StringBuilder sb = new StringBuilder();
            sb.Append(contents);
            sb.Replace("{clsName}", _getClassName());
            string _fullPath = txtPath.Text + "\\" + _FileName + ".cs";
            if (System.IO.File.Exists(_fullPath))
                System.IO.File.Delete(_fullPath);
            System.IO.StreamWriter sw = System.IO.File.CreateText(_fullPath);
            sw.Write(strbuilder.ToString());
            sw.Write(sb.ToString());
            sw.Close();
            sw.Dispose();
        }
@@ -156,10 +200,10 @@
        /// </summary>
        /// <param name="dt"></param>
        /// <param name="_FileName"></param>
        void ReadDesignerFile(DataTable dt, string _FileName)
        void ReadDesignerFile(DataTable dt, string _FileName, List<DataTable> dtList = null)
        {
            string _Temppath = Application.StartupPath + _getTemple("Designer.cs");
            string contents = System.IO.File.ReadAllText(_Temppath);
            string _tempPath = Application.StartupPath + _getTemple("Designer.cs");
            string contents = System.IO.File.ReadAllText(_tempPath);
            StringBuilder strbuilder = new StringBuilder();
            string line = Environment.NewLine;
            strbuilder.Append(contents);
@@ -168,12 +212,10 @@
            if (dt.Rows.Count > 0)
            {
                string _FieldName = "";//字段名
                string _FieldText = "";//
                string _prefix = "txt_";
                string _Lbfix = "lb_";
                string _gvFix = "gv_";
                string _FieldText = "";//字段文本
                string _FieldNameSql = "";//原始字段名,主要用于查询
                int _x = 0, _y = 0, _Tabindex = 210;
                string _ControlType = "";
                string _ControlType = "TextEdit";//控件类型,目前不作判断都为文本框
                string _lbType = "LabelControl";
                System.Text.StringBuilder _gvItemCol = new StringBuilder();
                for (int c = 0; c < _RowCount; c++)
@@ -184,85 +226,189 @@
                        _y = 20 + 32 * c / 4;//计算控件在Y轴的位置
                    }
                    _Tabindex = c % 4 + c / 4 + _Tabindex;
                    //多控件类型 这边可以根据具体的需求进行控件的扩展,目前都是文本
                    switch (dt.Rows[c]["DATA_TYPE"].ToString())
                    {
                        case "1111":
                            break;
                        default:
                            _FieldName = ToolBox.UtilityHelper.ToCamelCase(dt.Rows[c]["COLUMN_NAME"].ToString());
                            _FieldText = ToolBox.UtilityHelper.ToCamelCase(dt.Rows[c]["COLUMN_NAME"].ToString());
                            _ControlType = "TextEdit";
                            _FieldText = dt.Rows[c]["REMARK"].ToString();
                            _FieldNameSql = "query_a." + dt.Rows[c]["COLUMN_NAME"].ToString();
                            break;
                    }
                    string _labName = _Lbfix + _FieldName;//label名
                    string _ControlName = _prefix + _FieldName;//文本框名
                    string _gvName = _gvFix + _FieldName;//gv中的列名
                    string _labName = "lb_" + _FieldName;//label名
                    string _txtName = "txt_" + _FieldName;//文本框名
                    string _gvName = "gv_" + _FieldName;//gv中的列名
                    if (_gvItemCol.Length > 0)
                        _gvItemCol.Append(",");
                    _gvItemCol.Append("this." + _gvName);
                    //BQSetControlObject  实例化控件
                    string BQSetControlObject = ("BQSetControlObject" + line + "this." + _labName + " =  new DevExpress.XtraEditors." + _lbType + "();" + line);
                    BQSetControlObject = (BQSetControlObject + "this." + _ControlName + " = new DevExpress.XtraEditors." + _ControlType + "(); " + line);
                    BQSetControlObject = (BQSetControlObject + "this." + _txtName + " = new DevExpress.XtraEditors." + _ControlType + "(); " + line);
                    BQSetControlObject = (BQSetControlObject + "this." + _gvName + " = new DevExpress.XtraGrid.Columns.GridColumn(); " + line);
                    strbuilder.Replace("BQSetControlObject", BQSetControlObject);
                    //创建对象结束后     this.SuspendLayout();
                    //BQSetControlInfo  设置控件信息  位置 大小 名称
                    string _ControlName02 = "\"" + _labName + "\"";//给控件Text 赋值的是带有双引号的字符串 因此这个里要特殊处理下
                    string _FieldName02 = "\"" + _FieldName + "\"";
                    string _FieldText02 = "\"" + (_FieldText + ":") + "\"";
                    //BQSetControlInfo  设置控件信息位置大小名称
                    string BQSetControlInfo = "BQSetControlInfo" + line
                             //label
                             + "//" + line
                             + "//" + _labName + line
                             + "//" + line
                             + "this." + _labName + ".Location = new System.Drawing.Point(" + _x + ", " + _y + ");" + line//位置
                             + "this." + _labName + ".Name = " + _ControlName02 + ";" + line//给Name赋值
                             + "this." + _labName + ".Size = new System.Drawing.Size(150,21);" + 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 + ".Tag = " + _FieldName02 + ";" + line//Tag值
                             + "this." + _labName + ".Appearance.Font = new System.Drawing.Font(\"Tahoma\", 10F);" + line
                            + "this." + _labName + ".Text = " + _FieldText02 + ";" + line
                            //文本
                            + "this." + _labName + ".Text = \"" + _FieldText + " \";" + line
                            //文本
                            + "//" + line
                            + "//" + _ControlName + line
                            + "//" + _txtName + line
                            + "//" + line
                            + "this." + _ControlName + ".Location = new System.Drawing.Point(" + (_x + 60) + ", " + _y + ");" + line//位置
                            + "this." + _ControlName + ".Name = " + _ControlName02 + ";" + line
                            + "this." + _ControlName + ".Size = new System.Drawing.Size(150,21);" + line
                            + "this." + _ControlName + ".TabIndex = " + _Tabindex + ";" + line
                            + "this." + _ControlName + ".Tag = " + _FieldName02 + ";" + line
                            + "this." + _ControlName + ".Properties.Appearance.Font = new System.Drawing.Font(\"Tahoma\", 10F);" + line
                           //gv
                            + "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 = " + _FieldName02 + ";" + line
                           + "this." + _gvName + ".Caption = " + _FieldName02 + ";" + line
                           + "this." + _gvName + ".MinWidth = 25;" + 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//将控件添加到Form 容器中  这个容器可以是 panel 可以是groupbox 等等
                    //BQAddControltoMContainer//将控件添加到panel1容器中
                    string BQAddControltoMContainer = "BQAddControltoMContainer" + line + "  this.panel1.Controls.Add(this." + _labName + ");" + line;
                    BQAddControltoMContainer += " this.panel1.Controls.Add(this." + _ControlName + ");" + line;
                    BQAddControltoMContainer += " this.panel1.Controls.Add(this." + _txtName + ");" + line;
                    strbuilder.Replace("BQAddControltoMContainer", BQAddControltoMContainer);
                    //BQCreateConrolObejct //创建控件 
                    string BQCreateConrolObejct = "BQCreateConrolObejct" + line + "private DevExpress.XtraEditors." + _lbType + " " + _labName + ";" + line;
                    BQCreateConrolObejct += "private DevExpress.XtraEditors." + _ControlType + " " + _ControlName + ";" + 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());
                System.IO.File.Delete(txtCreateDBPath.Text + "\\" + _FileName + ".Designer.cs");
                string path = txtCreateDBPath.Text;
                //构建明细gridview1
                if (dtList != null && dtList.Count > 0)
                {
                    int _idx = 1;
                    System.Text.StringBuilder _tabItem = new System.Text.StringBuilder();
                    foreach (DataTable dt2 in dtList)
                    {
                        string _tabMxPage = "tabMxPage" + _idx.ToString();
                        string _gcMx = "gcMx" + _idx.ToString();
                        string _gvMx = "gvMx" + _idx.ToString();
                        if (_tabItem.Length > 0)
                            _tabItem.Append(",");
                        _tabItem.Append("this." + _tabMxPage);
                        //BQSetControlObject 实例化控件
                        string 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 设置控件信息位置大小名称
                        string 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.ToString() + "\";" + 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 //创建控件
                        string _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);
                        System.Text.StringBuilder _layout = new StringBuilder();
                        _layout.Append("this.tabMxPage1.SuspendLayout();");
                        strbuilder.Replace("{layoutList}", _layout.ToString());
                        System.Text.StringBuilder _resume = new StringBuilder();
                        _resume.Append("this.tabMxPage1.ResumeLayout(false);");
                        strbuilder.Replace("{resumeLayout}", _resume.ToString());
                        System.Text.StringBuilder _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 (int c = 0; c < _RowCount; c++)
                        {
                            switch (dt2.Rows[c]["DATA_TYPE"].ToString())
                            {
                                default:
                                    _FieldName = ToolBox.UtilityHelper.ToCamelCase(dt2.Rows[c]["COLUMN_NAME"].ToString());
                                    _FieldText = dt2.Rows[c]["REMARK"].ToString();
                                    _FieldNameSql = "query_a." + dt2.Rows[c]["COLUMN_NAME"].ToString();
                                    break;
                            }
                            string _gvColName = _gvMx + _FieldName;
                            if (_gvItemCol.Length > 0)
                                _gvItemCol.Append(",");
                            _gvItemCol.Append("this." + _gvColName);
                            //BQSetControlObject  实例化控件
                            string BQSetControlObject = "BQSetControlObject" + line;
                            BQSetControlObject += ("this." + _gvColName + " = new DevExpress.XtraGrid.Columns.GridColumn(); ");
                            strbuilder.Replace("BQSetControlObject", BQSetControlObject);
                            //BQSetControlInfo  设置控件信息位置大小名称
                            string 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 //创建控件
                            string 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());
                }
                System.IO.File.Delete(txtPath.Text + "\\" + _FileName + ".Designer.cs");
                string path = txtPath.Text;
                System.IO.StreamWriter sw = System.IO.File.CreateText(path + "\\" + _FileName + ".Designer.cs");
                sw.Write(strbuilder.ToString());
                sw.Close();
                sw.Dispose();
            }
        }
        /// <summary>
        /// 第三个文件
        /// </summary>
@@ -275,19 +421,245 @@
            StringBuilder strbuilder = new StringBuilder();
            string line = Environment.NewLine;
            strbuilder.Append(contents);
            System.IO.File.Delete(txtCreateDBPath.Text + "\\" + _FileName + ".resx");
            string path = txtCreateDBPath.Text;
            System.IO.File.Delete(txtPath.Text + "\\" + _FileName + ".resx");
            string path = txtPath.Text;
            System.IO.StreamWriter sw = System.IO.File.CreateText(path + "\\" + _FileName + ".resx");
            sw.Write(strbuilder.ToString());
            sw.Close();
            sw.Dispose();
        }
        private void btnToPath_Click_1(object sender, EventArgs e)
        #endregion
        #region 文件上传
        /// <summary>
        /// 选择上传文件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BtnUpSelect_Click(object sender, EventArgs e)
        {
            string _GetfilePath = FileSelector.BrowseFilePathForSave();
            txtCreateDBPath.Text = _GetfilePath;
            OpenFileDialog openFileDialog = new OpenFileDialog();
            // 设置初始目录
            openFileDialog.InitialDirectory = "c:\\";
            // 设置文件过滤选项,如:"文本文件 (*.txt)|*.txt|所有文件 (*.*)|*.*"
            openFileDialog.Filter = "文本文件 (*.txt)|*.txt|所有文件 (*.*)|*.*";
            // 设置标题
            openFileDialog.Title = "选择文件";
            // 显示对话框
            DialogResult result = openFileDialog.ShowDialog();
            // 确认用户没有取消操作
            if (result == DialogResult.OK)
            {
                // 获取选中的文件路径
                string 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)
        {
            string WebApiUrl = System.Configuration.ConfigurationSettings.AppSettings.Get("WebApiUrl").ToString();
            string address = WebApiUrl + "Upload/UploadFile";
            string filePath = txtUpUrl.Text.Trim();
            string 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", ToolBox.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);
                    string 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)
        {
            List<dynamic> _list = new List<dynamic>();
            for (int i = 0; i < 20; i++)
            {
                _list.Add(new
                {
                    t1 = "test1_" + i.ToString(),//单位编号
                    t2 = "hi2_" + i.ToString(),//单位名称
                });
            }
            var _json = new
            {
                category = "单位信息",
                list = _list
            };
            try
            {
                string _apiUrl = "http://localhost:5263/";
                string _strReturnJson = HttpPost(_apiUrl, "ErpMes/PushMesByCategory", JsonConvert.SerializeObject(_json));
                txtMesResult.Text = _strReturnJson;
            }
            catch (Exception ex)
            {
                ToolBox.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,不能删除
            string token = (Guid.NewGuid().ToString() + "~" + Guid.NewGuid().ToString());
            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.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)
                {
                    StreamReader 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
        #region 批量读取
        private void btnTable_Click(object sender, EventArgs e)
        {
            using (OracleConnection connection = new OracleConnection(oracleCon))
            {
                try
                {
                    connection.Open();
                    using (OracleCommand command = connection.CreateCommand())
                    {
                        command.CommandText = "SELECT * FROM " + txtTable1.Text.Trim();
                        using (OracleDataReader oracleDataReader = command.ExecuteReader())
                        {
                            using (SqlBulkCopy bulkCopy = new SqlBulkCopy(sqlCon))
                            {
                                bulkCopy.DestinationTableName = txtTable1.Text.Trim();
                                bulkCopy.WriteToServer(oracleDataReader);
                            }
                        }
                    }
                    ToolBox.MsgHelper.ShowInformation("提示,操作成功!");
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    ToolBox.MsgHelper.ShowError("提示:" + ex.Message);
                }
                finally
                {
                    if (connection.State != System.Data.ConnectionState.Closed)
                    {
                        connection.Close();
                    }
                }
            }
        }
        #endregion
        #region 批量准备
        private void btnRemark_Click(object sender, EventArgs e)
        {
            if (txtTable2.Text.Trim().Length <= 0)
            {
                MessageBox.Show("请输入表名");
                return;
            }
            using (SqlConnection conn = new SqlConnection(sqlCon))
            {
                using (SqlCommand cmd = new SqlCommand("[tmp_property]", conn))
                {
                    try
                    {
                        conn.Open();
                        cmd.CommandType = CommandType.StoredProcedure;
                        SqlParameter[] parameters = new SqlParameter[] {
                                new SqlParameter("@tabname",txtTable2.Text.Trim()),
                                new SqlParameter("@com",txtOracleDec.Text.Trim()),
                            };
                        foreach (SqlParameter parameter in parameters)
                        {
                            cmd.Parameters.Add(parameter);
                        }
                        cmd.ExecuteNonQuery();
                        ToolBox.MsgHelper.ShowInformation("提示,操作成功!");
                    }
                    catch (Exception ex)
                    {
                        ToolBox.MsgHelper.ShowError("提示:" + ex.Message);
                    }
                    finally
                    {
                        conn.Close();
                    }
                }
            }
        }
        #endregion
    }
}
}