使用OleDB从中导入数据后无法删除excel文件,表示正在被另一个进程使用

时间:2011-12-21 19:53:10

标签: asp.net oledb

我正在使用OleDB从ASP.NET中的Excel文件导入数据。完成导入后,我想使用命令System.IO.File.Delete()删除该文件,但它会引发以下异常:

The process cannot access the file '...29.xls' because it is being used by another process.

我使用以下代码打开和关闭文件:

Dim fajl As String
fajl = MapPath("fajlovi/" + Request.QueryString("ID"))
        Dim sConnectionStringExcel As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & fajl & ";Extended Properties=Excel 8.0;"
        Dim objConnExcel As New OleDbConnection(sConnectionStringExcel)
        objConnExcel.Open()
        Dim objCmdSelectExcel As New OleDbCommand("SELECT ZavedenKodPov, Ime, Mjesto, Adresa, JMBG, LicniBroj, ZaposlenKod, Nepoznat, Umro, Penzioner, Reon, VoziloProizvodjac, VoziloModel, VoziloRegistracija, Nekretnina, Datum, KontoBroj, NazivKonta, OpisPromjene, Dug, Pot FROM [Sheet1$]", objConnExcel)
        Dim objAdapterExcel As New OleDbDataAdapter()
        objAdapterExcel.SelectCommand = objCmdSelectExcel
        Dim objDatasetExcel As New DataSet()
        objAdapterExcel.Fill(objDatasetExcel, "XLData")
        Dim tExcel As DataTable
        tExcel = objDatasetExcel.Tables(0)
       '.
       '.
       '.
objConnExcel.Close()
System.IO.File.Delete(fajl)

任何想法我做错了什么?

1 个答案:

答案 0 :(得分:2)

处理命令和连接,最好包含在using语句中。那么你应该能够删除该文件。