lg
2024-09-17 ae27635bd92120d0dd975d8480c344162610219a
DevApp/Gs.DevApp/DevFrm/Sys/EasyCode.cs
@@ -5,7 +5,10 @@
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
@@ -17,47 +20,30 @@
        public EasyCode()
        {
            InitializeComponent();
            this.txt_sqlconn.Text = "Data Source=2kn4sh70028.vicp.fun,26144;Initial Catalog=gs_Mes;User ID=sa;Password =admin110";
            //this.txt_sqlconn.Text = "Data Source=2kn4sh70028.vicp.fun,26144;Initial Catalog=gs_Mes;User ID=sa;Password =admin110";
            this.txt_sqlconn.Text = "Data Source=.;Initial Catalog=gs_Mes;User ID=sa;Password =admin110";
            getTable();
        }
        /// <summary>
        /// 读取数据库表
        /// </summary>
        private void getTable()
        {
            string strConn = txt_sqlconn.Text.Trim();
            DataSet dset = new DataSet();
            using (SqlConnection connection = new SqlConnection(strConn))
            {
                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";
                using (SqlDataAdapter dt = new SqlDataAdapter(cmd))
                {
                    dt.Fill(dset, "0");
                }
            }
            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;
            }
            this.btnToPath.Click += BtnToPath_Click;
            this.btnUpSelect.Click += BtnUpSelect_Click;
            this.btnUp.Click += BtnUp_Click;
        }
        /// <summary>
        /// 选择生成路径
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnToPath_Click(object sender, EventArgs e)
        private void BtnToPath_Click(object sender, EventArgs e)
        {
            string _GetfilePath = FileSelector.BrowseFilePathForSave();
            txtCreateDBPath.Text = _GetfilePath;
        }
        /// <summary>
        /// 确定生成
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnOk_Click(object sender, EventArgs e)
        {
            if (txt_sqlconn.Text.Trim() == "")
@@ -87,6 +73,32 @@
            ReadResxFile(dt, _strName);
            string path = txtCreateDBPath.Text;
            lbMsg.Text = "生成成功,路径:" + path;
        }
        /// <summary>
        /// 读取数据库表
        /// </summary>
        private void getTable()
        {
            string strConn = txt_sqlconn.Text.Trim();
            DataSet dset = new DataSet();
            using (SqlConnection connection = new SqlConnection(strConn))
            {
                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";
                using (SqlDataAdapter dt = new SqlDataAdapter(cmd))
                {
                    dt.Fill(dset, "0");
                }
            }
            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;
            }
        }
        /// <summary>
        /// 读取列名
@@ -293,17 +305,72 @@
            sw.Close();
            sw.Dispose();
        }
        private void btnToPath_Click_1(object sender, EventArgs e)
        {
            string _GetfilePath = FileSelector.BrowseFilePathForSave();
            txtCreateDBPath.Text = _GetfilePath;
        }
        /// <summary>
        /// 报表设计
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            EasyRpt frm = new EasyRpt("001");
            frm.Show();
        }
        /// <summary>
        /// 选择上传文件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BtnUpSelect_Click(object sender, EventArgs e)
        {
            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;
            }
        }
        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);
                }
            }
        }
    }
}
}