【问题标题】:Ace oledb connection close file still in use errorAce oledb 连接关闭文件仍在使用错误
【发布时间】:2020-02-11 13:20:28
【问题描述】:

最近我在 Excel 2016 上遇到了一个问题。在我关闭 excel 2016 环境中的连接字符串后,系统仍然锁定 excel 无法编辑(比如正在使用的文件)。但代码在 excel 2007 和 2010 环境中运行良好(连接关闭后,该文件已准备好供我的下一个功能打开和编辑)。

在 excel 2016 环境中,文件似乎需要锁定 75 秒后才自动解锁,但在 excel 2007 和 2010 环境中不会发生这种情况

我应该在哪里更改它?

Public Function gf_ExcelConnection(ByRef strLocation As String, ByRef sWorksheetName As String, _
                                   ByRef lExcelDataset As DataSet, ByRef iLastRow As Integer, _
                                   ByRef sColumn As String) As Boolean

    Dim strExcelConn As String = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source=" & strLocation & _
                                 "; Extended Properties=""Excel 12.0 Xml;HDR=YES;IMEX=1;"""

    Dim ExcelConn As New System.Data.OleDb.OleDbConnection(strExcelConn)
    Dim ExcelCommand As OleDbCommand = New OleDbCommand
    Dim ExcelDataAdapter As New OleDbDataAdapter

    Try
        gf_ExcelConnection = True

        ExcelCommand.CommandText = "SELECT * FROM [" + sWorksheetName + "A1:" & sColumn & iLastRow & "] "
        ExcelDataAdapter.SelectCommand = ExcelCommand
        ExcelDataAdapter.SelectCommand.Connection = ExcelConn

        MsgBox(103)

        ExcelConn.Open()
        ExcelDataAdapter.Fill(lExcelDataset, "ImportTable")
        ExcelConn.Close()
        ExcelConn.Dispose()

        MsgBox(104)

    Catch ex As Exception
        gf_ExcelConnection = False
        gclsError.gf_ErrorHandler("InvalidIncorrectFile", Windows.Forms.MessageBoxButtons.OK, Windows.Forms.MessageBoxIcon.Error, False)
        ExcelConn.Close()
    Finally
        ExcelConn.Close()
        ExcelConn.Dispose()
        ExcelConn = Nothing

        MsgBox(105)
    End Try
End Function

【问题讨论】:

  • 请移除所有CloseDispose 调用,并在所有相关对象(ExcelConnExcelCommandExcelDataAdapter)周围放置Using
  • 在这个例子中我如何使用?

标签: excel vb.net excel-2016 oledbconnection aceoledb


【解决方案1】:

发现问题并解决

这个问题是由于微软最近对 MS Office 产品进行了更新。

在我卸载并重新安装 AccessDatabaseEngine(Microsoft Access Database Engine 2016 Redistributable)之后。万岁..错误没有出现

【讨论】:

    猜你喜欢
    • 2013-08-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-31
    • 1970-01-01
    相关资源
    最近更新 更多