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 '%已待装%'");
}*/
}
效果图
文章来源地址https://www.toymoban.com/news/detail-802185.html
文章来源:https://www.toymoban.com/news/detail-802185.html
到了这里,关于C# winform应用的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!