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

发布于 2024-12-22 06:38:16 字数 1278 浏览 1 评论 0原文

我正在使用 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)

任何想法我在做什么错误的?

I'm importing data from an Excel file in ASP.NET using OleDB. After finishing the import, I want to delete the file using the command System.IO.File.Delete(), but it throws the following exception:

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

I used the following code to open and close the file:

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)

Any ideas what I'm doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

山人契 2024-12-29 06:38:16

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

dispose of the command and connection, preferably wrapped in a using statement. then you should be able to delete the file.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文