This is for backup
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Windows.Forms;
using System.IO;
using System.Collections.Generic;
using System.Data.SqlClient;
using Microsoft.SqlServer.Management.Smo;
using Microsoft.SqlServer.Management.Common;
{
SaveFileDialog saveFileDlg = new SaveFileDialog();
saveFileDlg.Filter = "Mircosoft Access Files(*.mdb)|*.mdb";
saveFileDlg.Title = "Create Invoice Backup";
saveFileDlg.RestoreDirectory = false;
if (saveFileDlg.ShowDialog() == DialogResult.OK)
{
string sourceFile = Application.StartupPath.ToString() + "\\InvoiceGenerator.mdb";
string DatabaseName = saveFileDlg.FileName;
string img = "Images";
string sourcePath = (Application.StartupPath + "/Images/");
string targetPath = DatabaseName.Replace(DatabaseName, img);
try
{
File.Copy(sourceFile, DatabaseName);
MessageBox.Show("Backup Create Successfully");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
if (!System.IO.Directory.Exists(targetPath))
{
System.IO.Directory.CreateDirectory(targetPath);
}
if (System.IO.Directory.Exists(sourcePath))
{
string[] files = System.IO.Directory.GetFiles(sourcePath);
string fileName;
string destFile;
foreach (string s in files)
{
fileName = System.IO.Path.GetFileName(s);
destFile = System.IO.Path.Combine(targetPath, fileName);
System.IO.File.Copy(s, destFile, true);
}
}
else
{
MessageBox.Show("Source path does not exist!");
}
}
}
{
string message = "After Restoring the Database from a previous Backup, Current Database will be lost and the changes made after Backup will become unrecoverable. Are you sure you want to Continue!";
string caption = "Restore Backup";
DialogResult result;
string img;
img = AppDomain.CurrentDomain.BaseDirectory + "database_add_48.png";
result = MessageBox.Show(message, caption, MessageBoxButtons.YesNo, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1);
if (result == DialogResult.Yes)
{
OpenFileDialog openDlg = new OpenFileDialog();
openDlg.Filter = "DataBase Files (*.mdb)|*.mdb";
openDlg.FileName = "";
openDlg.DefaultExt = ".mdb";
openDlg.CheckFileExists = true;
openDlg.CheckPathExists = true;
DialogResult res = openDlg.ShowDialog();
if (res == DialogResult.OK)
{
if (!(openDlg.FileName).EndsWith(".mdb") && !(openDlg.FileName).EndsWith(".MDB"))
MessageBox.Show("Invalid file format...!", "Prajeesh", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
else
{
string sourceFile = Application.StartupPath.ToString() + "\\InvoiceGenerator.mdb";
string DatabaseName = openDlg.FileName;
try
{
File.Copy(DatabaseName, sourceFile, true);
MessageBox.Show("Restore Successfully");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
}
}