Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > create ms access database backup and restore from c# windows application
 

create ms access database backup and restore from c# windows application

hi
i tried to create backup and restore ms access database from c#.net.I wrote the following coding.Backup is created and i opened that backup file using open with ms access.There is no table in that.

To create backup
public static void createBackup()
{

//create backup file
try
{
Catalog cat = new Catalog();
Directory.CreateDirectory("D:\\Data");
cat.Create("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\\Data\\" + dbname + ".bak;Jet OLEDB:Engine Type=5");
cat = null;
}
catch (Exception ex)
{
}
OleDbConnection con = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\\Transport\\Data\\" + dbname + ".mdb");
string oQuery = "Backup database c:\\Transport\\Data\\" + dbname + ".mdb To disk=D:\\Data\\" + dbname + ".bak";
OleDbCommand com = new OleDbCommand(oQuery, con);
con.Open();
try
{
com.ExecuteNonQuery();
}
catch (Exception ex)
{
}
con.Close();
com = null;
oQuery = "";
}

//Restore

public static void createRestore()
{
//create restore file
OleDbConnection con = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\Transport\\Data\\" + dbname + ".mdb");
string oQuery = "Restore database c:\\Transport\\Data\\" + dbname + ".mdb from disk=D:\\Data\\" + dbname + ".bak";
OleDbCommand com = new OleDbCommand(oQuery, con);
con.Open();
try
{
com.ExecuteNonQuery();
}

catch (Exception ex)
{

}
con.Close();
com = null;
oQuery = "";
}

After creating backup i deleted a row in original database after that i clicked restore,it doesnt show the deleted record.
Please help.Its urgent

Jhanani.r
JananiSharma  Thursday, November 29, 2007 5:14 AM

Hi, JananiSharma,

Based on my understanding, your backupAccess DataBase doesn't work, does it?

As you are just copying mdb from here to there, why don't you just use System.IO.File.Copy to backup and restore your mdb file?

http://msdn2.microsoft.com/en-us/library/system.io.file.copy.aspx

And after you restore the mdb file, you should retrieve the data from your mdb file again to get the latest Data.

Regards

Yu Guo â€?MSFT  Tuesday, December 04, 2007 7:34 AM
Hi thanks for this code ya..
but i can't understand wt is the meaning for catalog and dbname
please reply me...
baju_crazy  Wednesday, May 07, 2008 9:43 AM
What if the DB is password protected?
DManA  Tuesday, August 11, 2009 7:41 PM

You can use google to search for other answers

Custom Search

More Threads

• Simple Populate combobox from SQL server
• Navigating to a specific item using a binding navigator
• How to position a small form inside a bigger one when clicking on acertain button without cutting the smaller form
• modifying the data in Strongly typed dataset before updating to the database
• Data Binding behavior for Access
• Bug? ComboBox raises PropertyChanged event when property hasn't changed
• Array.ForEach for other types
• Copy-Paste images in DataGridWiew
• Databound DataGridView
• how to add one row in front in grid view