C# winform应用

C# winform应用

需求:导入Excel文件时需要执行其他操作,实现如果取消导入就不执行其他操作

C#代码实现
private bool DLimport0(string tablename, string datebasename, string buttonname)
        {
            string xxx = "";
            string Tag = "";
            string connString = "server=192.168.1.110;uid=sa;pwd=xyz@0123456...;database=" + datebasename;
            System.Windows.Forms.OpenFileDialog fd = new OpenFileDialog();
            fd.Multiselect = true;
            if (fd.ShowDialog() == DialogResult.OK)
            {
                foreach (string file in fd.FileNames)
                {
                    xxx += file + "\n";
                }
                DialogResult dr = MessageBox.Show("是否将以下文件导入到【" + buttonname + "】\n" + xxx, "导入文件确认", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
                if (dr == DialogResult.OK)
                {
                    //label1.Visible = true;
                    richTextBox1.Text = null;
                    richTextBox2.Text = null;
                    DL(connString, tablename);

                    foreach (string file in fd.FileNames)
                    {
                        richTextBox2.AppendText(DateTime.Now.ToString("HH:mm:ss  ") + System.IO.Path.GetFileName(file) + "数据读取中...\n");
                        Tag = TransferData(file, tablename, connString);
                        richTextBox2.AppendText("--------------------\n");
                        richTextBox1.ScrollToCaret();
                        richTextBox2.ScrollToCaret();
                    }

                    if (Tag == "TAG") { MessageBox.Show("导入完成!"); }
                    else MessageBox.Show("导入失败!");

                    return true; // 返回true表示文件导入成功
                }
            }

            return false; // 返回false表示用户取消了文件选择
        }

        private bool NODLimport0(string tablename, string datebasename, string buttonname)
        {
            bool fileImported = false; // 声明一个用于判断文件是否导入成功的变量
            string xxx = "";
            string Tag = "";
            string connString = "server=192.168.1.110;uid=sa;pwd=xyz@0123456...;database=" + datebasename;
            System.Windows.Forms.OpenFileDialog fd = new OpenFileDialog();
            fd.Multiselect = true;

            DialogResult fdResult = fd.ShowDialog();

            if (fdResult == DialogResult.OK)
            {
                foreach (string file in fd.FileNames)
                {
                    xxx += file + "\n";
                }

                DialogResult dr = MessageBox.Show("是否将以下文件导入到【" + buttonname + "】\n" + xxx, "导入文件确认", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);

                if (dr == DialogResult.OK)
                {
                    richTextBox1.Text = null;
                    richTextBox2.Text = null;

                    foreach (string file in fd.FileNames)
                    {
                        richTextBox2.AppendText(DateTime.Now.ToString("HH:mm:ss  ") + System.IO.Path.GetFileName(file) + "数据读取中...\n");
                        Tag = TransferData(file, tablename, connString);
                        richTextBox2.AppendText("--------------------\n");
                        richTextBox1.ScrollToCaret();
                        richTextBox2.ScrollToCaret();
                    }

                    if (Tag == "TAG")
                    {
                        MessageBox.Show("导入完成!");
                        fileImported = true; // 设置文件导入成功的标志变量为true
                    }
                    else
                    {
                        MessageBox.Show("导入失败!");
                        fileImported = false; // 设置文件导入成功的标志变量为false
                    }
                }
            }

            return fileImported; // 返回文件导入成功的标志变量
        }
		public void noQuery(string sql)
        {
            //server=127.0.0.1;
            string connString = "server=192.168.1.110;uid=sa;pwd=xyz@0123456...;database=RB";
            SqlConnection conn = new SqlConnection(connString);
            SqlCommand cmd = new SqlCommand(sql, conn);
            cmd.CommandTimeout = 1000;
            conn.Open();
            cmd.ExecuteNonQuery();
            conn.Close();
        }

        //测试按钮清空数据 DLimport0 NODLimport0
        private void button60_Click(object sender, EventArgs e)
        {
            DLimport0("cs", "RB", "派发清空导入" + button60.Text);
            //NODLimport0("cs", "RB", "派发增量导入" + button60.Text);
            /*if (NODLimport0("cs", "RB", "派发增量导入" + button60.Text))
            {
                noQuery("update cs set 未完成大类='其他' WHERE 未完成大类 LIKE '%其他原因%'");
                noQuery("update cs set 定单状态='缓装' WHERE 定单状态 LIKE '%已缓装%'");
                noQuery("update cs set 定单状态='待装' WHERE 定单状态 LIKE '%已待装%'");
            }*/
        }
效果图

C# winform应用_第1张图片

你可能感兴趣的:(C#,C#,winform,c#,开发语言)