How to do DB backup with VistaDB?

195 Views Asked by At

I'm trying to do a XML export of the database with VistaDB. Unfortuanently, I'm lost.

Here's my code so far:

    Dim instance As IVistaDBDatabase
    Dim value As System.Int64

    SaveFileDialog2.InitialDirectory = "C:\"
    Select Case SaveFileDialog2.ShowDialog()
        Case Windows.Forms.DialogResult.OK
            Try
                instance.ExportXml(SaveFileDialog2.FileName, VistaDBXmlWriteMode.All)
            Catch ex As Exception
                MsgBox(ex.ToString)
            End Try
    End Select

All I get is object not set to instance of an object (or something like that).

Can anyone provide so guidance?

2

There are 2 best solutions below

0
On BEST ANSWER

I was able to figure it out with help from an old code package. Here is the proper code:

    Dim vdbDatabase As IVistaDBDatabase = Nothing
    Dim filenameDB As String = SaveFileDialog2.FileName
    Dim filenameXML As String = SaveFileDialog2.FileName

    Select Case SaveFileDialog2.ShowDialog()
        Case System.Windows.Forms.DialogResult.OK
            Try

                ' Call the method that will open the connection to the database and return an open IVistaDBDatabase object for the database to us
                vdbDatabase = DDA.VistaDBEngine.Connections.OpenDDA.OpenDatabase("C:\Ledger.vdb5", VistaDBDatabaseOpenMode.NonexclusiveReadOnly, Nothing)


                ' Clear the XML Transfer List - this is the list used by the Import/Export methods
                vdbDatabase.ClearXmlTransferList()

                ' Loop through the tables in the database and add them to the XmlTransferList
                For Each tableName As String In vdbDatabase.GetTableNames()
                    vdbDatabase.AddToXmlTransferList(tableName)
                Next


                ' Call the ExportXML method with the name of the XML      file and the WriteMode value indicating what to Export
                vdbDatabase.ExportXml(filenameXML, VistaDBXmlWriteMode.All)

                ' If the database is open - close it
                If Not vdbDatabase.IsClosed Then
                    vdbDatabase.Close()
                End If
                MsgBox("Database Export complete.", MsgBoxStyle.Information)
            Catch ex As Exception
                WriteErrorEvent("Error exporting Database in addin", ex)
                MsgBox("An error occured attempting to export the Database. Error Message:" & vbCrLf & vbCrLf & ex.ToString, MsgBoxStyle.Exclamation)
            Finally
                ' If we have a vdbDatabase object               
                If Not (vdbDatabase Is Nothing) Then
                    ' If it is open, close it
                    If Not vdbDatabase.IsClosed Then
                        vdbDatabase.Close()
                    End If

                    ' Dispose of the object
                    vdbDatabase.Dispose()
                End If
            End Try
    End Select
0
On

A Little late but i've made a small change to your code, so you can see the exact time of your backup.

    Dim vdbDatabase As IVistaDBDatabase = Nothing
    Dim filenamedb As String = vdbDatabase
    Dim filenameXML As String = "MyDataBaseName_" + Now.ToString("dd-MM-yyyy_hh-mm-ss") + ".Xml"
    Dim path As String
    path = System.IO.Path.GetFullPath(filenameXML)

    Try
   vdbDatabase = DDA.VistaDBEngine.Connections.OpenDDA.OpenDatabase("DataDirectory|MyDataBase.vdb5", VistaDBDatabaseOpenMode.NonexclusiveReadOnly, Nothing)
vdbDatabase.ClearXmlTransferList()

        For Each tableName As String In vdbDatabase.GetTableNames()
            vdbDatabase.AddToXmlTransferList(tableName)
        Next

        vdbDatabase.ExportXml(filenameXML, VistaDBXmlWriteMode.All)

        If Not vdbDatabase.IsClosed Then
            vdbDatabase.Close()
        End If

        MsgBox("Backup is in " + path, MsgBoxStyle.Information)

    Catch ex As Exception
        MsgBox("An error occured attempting to export the Database. Error Message:" & vbCrLf & vbCrLf & ex.ToString, MsgBoxStyle.Exclamation)
    Finally

        If Not (vdbDatabase Is Nothing) Then
            If Not vdbDatabase.IsClosed Then
                vdbDatabase.Close()
            End If               
            vdbDatabase.Dispose()
        End If
    End Try