The Below Code Shows The Code Snippet To Take The Backup, Open And Save The Database:
using System;
using System.Collections.Generic;
usingSystem.ComponentModel;
usingSystem.Data;
usingSystem.Drawing;
usingSystem.Linq;
usingSystem.Text;
usingSystem.Windows.Forms;
namespaceBackupDatabase
{
public partial class Form1 : Form
{
publicForm1()
{
InitializeComponent();
}
To Take Backup Of The Existing Database, The Below Code Is Used
privatevoid btnBackup_Click(objectsender, EventArgs e)
{
try
{
// string sourcepath = "E:\\Chaitanya\\BackupDatabase\\BackupDatabase\\bin\\CrystalPrintWithBtn_DB.accdb";// if the database is at Any other path then the path should be declared like this..
//The Source Path Where The Database is Located.
string sourcepath ="../CrystalPrintWithBtn_DB.accdb";
// string destinationpath = "C:\\Documents and Settings\\RACK\\My Documents\\Downloads\\backupdatabase"+DateTime.Now.ToString("_MMMdd_yyyy_HHmmss")+".accdb";
//The Destination Path Where To Save The Backup Directly By Button Click Event Fires
stringdestinationpath = "C:\\backupdatabase"+ DateTime.Now.ToString("_MMMdd_yyyy_HHmmss") + ".accdb";
// when the source database is at .exe file then we have to write the code in comment lines below..
//System.IO.File.Copy(Application.StartupPath.ToString() + "\\CrystalPrintWithBtn_DB.accdb",destinationpath );
//System.IO.File.Copy(sourcepath,destinationpath);// Simple Code For Backup or Copy The Database
//Copy Database From Source Path To Destination Path
System.IO.File.Copy(System.IO.Path.GetFullPath(sourcepath), destinationpath);
MessageBox.Show("Backup stored Successfully");
}
catch(Exception ex)
{
MessageBox.Show("Error"+ex.Message);
}
}
To Open/Copy The File:
private void btnOpen_Click(object sender, EventArgs e)
{
//To Open/Copy The File From Required Destination Path
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
//if (System.IO.File.Exists(Application.StartupPath.ToString() + "\\CrystalPrintWithBtn_DB.accdb"))
//{
// System.IO.File.Delete(Application.StartupPath.ToString() + "\\CrystalPrintWithBtn_DB.accdb");
//}
//System.IO.File.Copy(openFileDialog1.FileName, Application.StartupPath.ToString() + "\\CrystalPrintWithBtn_DB.accdb");
MessageBox.Show("Backup Opened Successfully", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
MessageBox.Show("Operation Cancelled", "", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
To Save/Paste The File:
privatevoid btnSave_Click(objectsender, EventArgs e)
{
try
{
//To Save The Opened/Copied File To Required Destination Path
if(saveFileDialog1.ShowDialog() == DialogResult.OK)
{
//if(System.IO.File.Exists(saveFileDialog1.FileName))
//{
// System.IO.File.Delete(saveFileDialog1.FileName);
//}
//System.IO.File.Copy(Application.StartupPath.ToString() + "\\CrystalPrintWithBtn_DB.accdb", saveFileDialog1.FileName);
MessageBox.Show("Backup Saved Successfully", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
MessageBox.Show("Operation Cancelled", "", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
catch(Exception ex)
{
MessageBox.Show("Error Occurred"+ex.Message);
}
}
}
}
No comments:
Post a Comment
Note: only a member of this blog may post a comment.