0

我有一个 VB.NET (Visual Studio 2010) 应用程序,它以编程方式创建 Access 07 数据库,然后将 CSV 文件作为新的 Access 表导入。数据库被创建,CSV 被导入没有问题。该代码使用 ADOX.Catalog 创建数据库,并使用 OleDb.OleDbConnection 与 ACE 导入 CSV。一切都很好,除了我第一次打开 Access DB。当我从我的桌面 (Office 07) 启动 Access 07 时,我在屏幕右下角看到绿色的“修复”进度条约 5 秒钟。它只发生在我第一次打开数据库时。数据库和表工作正常,但 Access 肯定会修复一些问题。我每次都可以重新创建这种行为。第一次打开数据库时如何避免修复?任何想法都会有所帮助。

Public Function CreateTaxDatabase(ByVal DatabaseFullPath As String) As Boolean

  Dim bAns As Boolean
  Dim cat As ADOX.Catalog

  Try
     '' check if file exists
     If System.IO.File.Exists(DatabaseFullPath) Then
        '' delete the old file
        System.IO.File.Delete(DatabaseFullPath)
     End If

     Dim sCreateString As String
     cat = New ADOX.Catalog()
     sCreateString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & DatabaseFullPath
     cat.Create(sCreateString)

     '' close the connection
     Dim connection As ADODB.Connection = DirectCast(cat.ActiveConnection, ADODB.Connection)
     connection.Close()
     bAns = True
  Catch Excep As System.Runtime.InteropServices.COMException
     bAns = False
  Finally
     cat = Nothing
  End Try
  Return bAns
End Function

Sub ImportCSV(dbPath As String, CSVPath As String, CSVFile as String)

  Dim conn As OleDb.OleDbConnection = Nothing
  Dim SQL As String = ""

  Try
     conn = New OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + dbPath)
     If conn.State = ConnectionState.Closed Then
        conn.Open()
     End If
  Catch ex As Exception
     MsgBox(ex.Message, MsgBoxStyle.Exclamation + MsgBoxStyle.OkOnly, "Error Connecting to Database")
     If conn.State = ConnectionState.Open Then
        conn.Close()
     End If
 Return
  End Try

  SQL = "SELECT * INTO Table1 FROM [Text;FMT=Delimited;HDR=Yes;CharacterSet=ANSI;DATABASE=" + CSVPath + "].[" + CSVFile + "]"

  Try
     Dim SQLCmd As OleDb.OleDbCommand = conn.CreateCommand
     SQLCmd.CommandText = SQL
     SQLCmd.ExecuteNonQuery()
     Application.DoEvents()
  Catch ex As Exception
     SQL = "There was an error executing the following SQL Statement:" + vbCrLf + _
        SQL + vbCrLf + "Error - " + Trim(Str(Err.Number)) + " " + Err.Description
     MsgBox(SQL, MsgBoxStyle.Exclamation + MsgBoxStyle.OkOnly, "SQL Error")
  Finally
     If conn.State = ConnectionState.Open Then
        conn.Close()
     End If
     If Not IsNothing(conn) Then
        conn = Nothing
     End If
  End Try
End Sub
4

1 回答 1

0

这似乎可以解决问题

Public Sub CompactRepairDB(DBPath As String)
   '' compact and repair DB
   Dim AccessApp As Microsoft.Office.Interop.Access.Application = New Microsoft.Office.Interop.Access.Application()
   Dim TempDB = GetFilePath(DBPath) + "Compact1.ACCDB"

   DeleteFile(TempDB)
   AccessApp.OpenCurrentDatabase(DBPath)
   AccessApp.CloseCurrentDatabase()
   AccessApp.CompactRepair(DBPath, TempDB)
   AccessApp.Quit()
   DeleteFile(DBPath)
   '' rename Compact1 to Original DB Name
   My.Computer.FileSystem.RenameFile(TempDB, GetFileName(DBPath))
End Sub
于 2017-01-05T17:54:26.243 回答