本文介绍了上传后删除excel文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已经上传了一个excel文件,但是当我再次上传prevoius文件时却没有被删除.这是因为文件已存在"
我的代码如下:
------------------------------------------------
I have uploaded an excel file but when once again when i upload the prevoius file does not get deleted.It occurs as "File already exists"
My code is as follows:
------------------------------------------------
If File.Exists("c:\craupload\NLAO_Upload.xls") Then
If conn.State <> ConnectionState.Open Then
conn.Open()
End If
strDelFile = "exec master..xp_cmdshell ''del c:\craupload\NLAO_Upload.xls''"
cmd = New SqlCommand(strDelFile, conn)
cmd.CommandTimeout = 0
intFileDelete = cmd.ExecuteNonQuery
If conn.State <> ConnectionState.Closed Then
conn.Close()
End If
End If
--------------------------------------------
但这不会在将数据插入临时表后立即删除文件.请建议!!!!!!!!
在此先感谢
--------------------------------------------
But this does not Deletes the file as soon as the data is inserted in temp table.Please Suggest!!!!!!!!
Thanks In Advance
推荐答案
private void Form1_Load(object sender, EventArgs e)
{
DataTable dtExcel = GetDataFromExcel();
/*Implement logic to insert rows in database*/
System.IO.File.Delete("C:\\my\\Book1.xls");
}
public DataTable GetDataFromExcel()
{
DataTable dt = new DataTable();
con.ConnectionString = connectionString;
cmd.Connection = con;
OleDbDataAdapter dap = new OleDbDataAdapter();
dap.SelectCommand = cmd;
dap.Fill(dt);
return dt;
}
这篇关于上传后删除excel文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!