none
Backup Ms Access database which is project datasource RRS feed

  • Question

  • Hi,

      How can I back up my access database

     FolderLocations.validateFolderExistence();
    
                    string queryString = "backup database SparesDatabase to disk = '" + FolderLocations.modulesFolderPath + @"\SparesDatabase.accdb";
                    using (OleDbConnection connection = new OleDbConnection(connString.GetConnectionString))
                    {
                        connection.Open();
                        using (OleDbCommand command = new OleDbCommand(queryString, connection))
                        {
                            command.ExecuteNonQuery();
                        }
                    }

    which throws an exception saying SQL only recognizes UPDATE, SELECT, DELETE and such like statements but not the update statement. I need to later use the backup just in case anything goes wrong with my application.

    I also looked for the database location after installation using Path.Combine(Environment.CurrentDirectory, "SparesDatabase.accdb") but the database isn't placed there (where .exe is) after installation

    Please help me back up my database. Thanks in advance.


    Dennis

    Sunday, October 26, 2014 3:06 PM

Answers

  • Hello Dennis,

    >>How can I back up my access database

    From your description, it seems that you want to back up the ms access database in C#, for this, you could try to use File.Copy() method as:

    string CurrentDatabasePath = @"D:\BMX\Project\2014\10\ADO.NET\P20141027\Sample20141027.accdb";
    
    string PathtobackUp = @"D:\BMX\Project\2014\10\ADO.NET\P20141027\Backup.accdb";
    
    File.Copy(CurrentDatabasePath, PathtobackUp, true);

    As far as I know, the “backup” is used for a SQL Server database.

    Another way it to back up the database by hand as described in this link:

    https://support.office.com/en-us/article/Back-up-a-database-483d3d0a-4786-4bff-9f70-b11baea520a7

    Regards.

    Fred.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Monday, October 27, 2014 3:12 AM
    Moderator
  • I would check the value of |DataDirectory| to see what it is. I generally set it to something other than the default, which is in the application folder and typically not updateable. The below routines may help:

        Sub DisplayDataDirectory()
    
            Dim ApplicationPath As String = ""
    
            If IsNothing(System.AppDomain.CurrentDomain.GetData("DataDirectory")) Then
                ApplicationPath = System.Windows.Forms.Application.StartupPath
            Else
                ApplicationPath = System.AppDomain.CurrentDomain.GetData("DataDirectory").ToString
            End If
    
        End Sub
    
        Sub SetDataDirectory()
    
            AppDomain.CurrentDomain.SetData("DataDirectory", String.Concat(System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "\Database"))
    
        End Sub


    Paul ~~~~ Microsoft MVP (Visual Basic)

    Wednesday, October 29, 2014 12:47 PM

All replies

  • Hello Dennis,

    >>How can I back up my access database

    From your description, it seems that you want to back up the ms access database in C#, for this, you could try to use File.Copy() method as:

    string CurrentDatabasePath = @"D:\BMX\Project\2014\10\ADO.NET\P20141027\Sample20141027.accdb";
    
    string PathtobackUp = @"D:\BMX\Project\2014\10\ADO.NET\P20141027\Backup.accdb";
    
    File.Copy(CurrentDatabasePath, PathtobackUp, true);

    As far as I know, the “backup” is used for a SQL Server database.

    Another way it to back up the database by hand as described in this link:

    https://support.office.com/en-us/article/Back-up-a-database-483d3d0a-4786-4bff-9f70-b11baea520a7

    Regards.

    Fred.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Monday, October 27, 2014 3:12 AM
    Moderator
  • Hi,

     how can I get the path to the database now after installation? Here is my connection string to the database

    Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\SparesDatabase.accdb;

    If I replace |DataDirectory|\SparesDatabase.accdb; as the path to the data source, I get an exception path has invalid characters.

     So how can I get the path, I am using ClickOnce installation, Thanks for your feedback


    Dennis

    Monday, October 27, 2014 12:29 PM
  • Hello Dennis,

    >> how can I get the path to the database now after installation?

    The solution in my last reply is based that I assume that you already know the path of the access file. From your description, I am confused why you do not know it since the application is installed by you.

    >> If I replace |DataDirectory|\SparesDatabase.accdb; as the path to the data source, I get an exception path has invalid characters

    When this error is thrown, at runtime or when compling? Usually, when typing a file path, I would add the “@” symbol as:

    @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\BMX\Project\2014\10\ADO.NET\P20141027\Sample20141027.accdb;Persist Security Info=True;"

    Please have a try if this works for you.

    >> So how can I get the path, I am using ClickOnce installation,

    I am not a ClickOnce expert, so I am not clear where the access file is stored, you could confirm it with the ClickOnce expert at the specific forum:

    https://social.msdn.microsoft.com/Forums/windows/en-US/home?forum=winformssetup

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, October 28, 2014 8:14 AM
    Moderator
  • I would check the value of |DataDirectory| to see what it is. I generally set it to something other than the default, which is in the application folder and typically not updateable. The below routines may help:

        Sub DisplayDataDirectory()
    
            Dim ApplicationPath As String = ""
    
            If IsNothing(System.AppDomain.CurrentDomain.GetData("DataDirectory")) Then
                ApplicationPath = System.Windows.Forms.Application.StartupPath
            Else
                ApplicationPath = System.AppDomain.CurrentDomain.GetData("DataDirectory").ToString
            End If
    
        End Sub
    
        Sub SetDataDirectory()
    
            AppDomain.CurrentDomain.SetData("DataDirectory", String.Concat(System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "\Database"))
    
        End Sub


    Paul ~~~~ Microsoft MVP (Visual Basic)

    Wednesday, October 29, 2014 12:47 PM