I use MS Access database and I want to create a simple backup system, but I have not idea how to create it. I tried with copy paste on the database but I got error because my database is being used by my application... I also tried to disconnect the connection with the base, but i got the same error again...

My problem is in the replace the backup...

// New Open File Dialot
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Title = "Враќање бекап";
            ofd.Filter = "MS Access Database (*.mdb)|*.mdb";

            // Locate the Path of the my application
            System.Reflection.Assembly asb = System.Reflection.Assembly.GetEntryAssembly();
            string directoryPath = System.IO.Path.GetDirectoryName(asb.Location);

            string myFileName = "";
            string myPath = "";

            if (ofd.ShowDialog() == DialogResult.OK)
            {
                // Get the filename of the selected .mdb file...
                myFileName = System.IO.Path.GetFileName(ofd.FileName);
                // Get the path of the selectet .mdb file... 
                myPath = System.IO.Path.GetDirectoryName(ofd.FileName);

                // if .mdb alredy exists
                if (File.Exists(@"" + directoryPath + "/myNotebook.mdb"))
                {
                    DialogResult dr = MessageBox.Show("bla bla", "Warning",
                        MessageBoxButtons.YesNo, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button2);
                    if (dr == DialogResult.Yes)
                    {
                        try
                        {
                            Form1 frmMainForm = new Form1();
                            // Closing the connection with the database
                            frmMainForm.database.Close();
                            // Try delete alredy exists database
                            File.Delete(directoryPath + "//myNotebook.mdb");
                            // Replace the new database...
                            File.Copy(@"" + myPath + "/" + myFileName, @"" + directoryPath + "/myNotebook.mdb");
                        }
                       .....

Anyone help me ?
Thanks...

Recommended Answers

All 3 Replies

You can do a "hot copy" of the database but this is not recommended. You need to close and dispose of all your OleDb connectors to the database in your application before you try to access the file. In order to access a file that is in use you need to open it a little differently. Here is an example of a method that can open a file in use:

public static bool IsValidImage(string FileName)
    {
      if (string.IsNullOrEmpty(FileName))
        throw new ArgumentException("File name cannot be empty", "FileName");
      if (!File.Exists(FileName))
        throw new FileNotFoundException("File could not be found", FileName);

      try
      {
        //This lets us open up a file that is in use by another application
        using (FileStream fs = new FileStream(FileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
        {
          using (StreamReader sr = new StreamReader(fs))
          {
            return IsValidImage(fs);
          }
        }
      }
      catch (Exception ex)
      {
        if (ex is IOException)
          throw;
        return false;
      }
    }

Next -- this line makes no sense: if (File.Exists(@"" + directoryPath + "/myNotebook.mdb")) Lastly -- I don't understand what you're asking exactly. What is the problem with replacing the existing backup?

You can do a "hot copy" of the database but this is not recommended. You need to close and dispose of all your OleDb connectors to the database in your application before you try to access the file. In order to access a file that is in use you need to open it a little differently. Here is an example of a method that can open a file in use:

public static bool IsValidImage(string FileName)
    {
      if (string.IsNullOrEmpty(FileName))
        throw new ArgumentException("File name cannot be empty", "FileName");
      if (!File.Exists(FileName))
        throw new FileNotFoundException("File could not be found", FileName);

      try
      {
        //This lets us open up a file that is in use by another application
        using (FileStream fs = new FileStream(FileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
        {
          using (StreamReader sr = new StreamReader(fs))
          {
            return IsValidImage(fs);
          }
        }
      }
      catch (Exception ex)
      {
        if (ex is IOException)
          throw;
        return false;
      }
    }

Thank you for your example. :)

Next -- this line makes no sense: if (File.Exists(@"" + directoryPath + "/myNotebook.mdb")) Lastly -- I don't understand what you're asking exactly. What is the problem with replacing the existing backup?

So.. if you have a backup of the database and if the your current database destroyed then you can replace with the your backup... maybe this is horrible solution but I have no idea for better solution :/

Well file paths use backslashes instead of forward slashes normally... and you are using "" + .. . Why have the empty string to begin with?

If the file is in use then you haven't shut down all of your connections to the database. You can try the hot copy and see if that works or else ensure that you have shut down all connections THEN copy the database.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.