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)
{
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;
}
}
return 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;
}
else
{
MessageBox.Show("导入失败!");
fileImported = false;
}
}
}
return fileImported;
}
public void noQuery(string sql)
{
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();
}
private void button60_Click(object sender, EventArgs e)
{
DLimport0("cs", "RB", "派发清空导入" + button60.Text);
}
效果图