使用VB.net创建Access 07 DB在第一次打开时始终运行Repair

时间:2016-12-31 17:17:17

标签: vb.net oledb access ms-jet-ace

我有一个VB.NET(Visual Studio 2010)应用程序,它以编程方式创建Access 07数据库,然后将CSV文件作为新的Access表导入。创建数据库,导入CSV时没有任何问题。该代码使用ADOX.Catalog创建数据库,使用ACE的OleDb.OleDbConnection导入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

1 个答案:

答案 0 :(得分: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