How to backup and restore access database using C#.Net

 

Introduction

Here i will explain How to backup and restore access database using C#.Net.

History

In the Previous articles i explained how to add namespace in vb.netredirect to page according to role while login in asp.nethow to open form on click of the button using c#.nethow to close form on click of the button using C#.net, how to insert record into database from listview control using C#.net and many more about C#.Net.

Step 1.

Create Backup Function using following code.

        protected void Backup(string path)
        {
            // ----------------------------------- 
            // -- CREATE FILE BACKUP 
            // ----------------------------------- 
            string src = Application.StartupPath + @"\hotel.mdb";
            string dst = path;
            System.IO.File.Copy(src, dst, true);
        }

Here we will give path of source database and give the destination path to save backup file. In last we will use Copy Function for Backup.

Step 2.

Write the below code on click event of the Button. Using Below code we will browse for database of which you want to take backup. After browsing the database we will pass selected database path to the Backup function. And call Backup Function.

      try
            {
                SaveFileDialog SaveFD1 = new SaveFileDialog();
                string FileName = "";
                SaveFD1.InitialDirectory = "D:";
                SaveFD1.FileName = "";
                SaveFD1.Title = "Backup ";
                SaveFD1.DefaultExt = "mdb";
                SaveFD1.Filter = "Ms-Access Files (*.mdb)|*.mdb|All Files|*.*";
                //SaveFD1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*"; 
                SaveFD1.FilterIndex = 1;
                SaveFD1.RestoreDirectory = true;
                if (SaveFD1.ShowDialog() == DialogResult.OK)
                {

                    FileName = SaveFD1.FileName;
                    Backup(FileName);
                    MessageBox.Show("Backup Process is Completed Successfully !", "Backup Status", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error while backup " + ex, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

 

Next : How to Restore database using C#

Give your Valuable Comments.
comments powered by Disqus
 
Follow me on twitter
About Me
Harpreet Singh
I began programming with C++ when i was 17. Then at the middle of my study .NET came. Then I began to read C# and VB.NET. By the time i learnt SQL and ASP.NET, and developed some websites such as news portals that are active now. Currently i am running my own Software company.
More Articles