Dtabase Restore And Backup in C#

Posted by imomins on May 12, 2011 at 8:45 AM

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Text;

using System.Windows.Forms;

using Microsoft.SqlServer.Management.Smo;

using Microsoft.SqlServer.Management.Common;

using Microsoft.SqlServer.Management;

using System.IO;

using net.ERP.AIMS.Constants;

using net.ERP.AIMS.Utility;

using net.ERP.AIMS.DatabaseSchema.DBExistanceChecking;

using System.Linq;

using System.Data.Sql;

using System.Data.SqlClient;

 

namespace net.ERP.AIMS.Modules.Settings.GUI.Config

{

    public partial class frmSettingsConfigDatabaseBackup : Form

    {       

        public frmSettingsConfigDatabaseBackup()

        {

            InitializeComponent();

        }

        private static Server srvSql;

 

        //private string mpristrFolderNameOne = "net.ERP.AIMS(Backup).bak";

        //private string mpristrFolderNameTwo = "\net.ERP.AIMS(Backup).bak";

 

        private void cmdLocation_Click(object sender, EventArgs e)

        {            

            FolderBrowserDialog folderDlg = new FolderBrowserDialog();

            folderDlg.ShowNewFolderButton = true;          

            DialogResult result = folderDlg.ShowDialog();

            if (result == DialogResult.OK)

            {

                txtBrowse.Text = folderDlg.SelectedPath;               

                //folderBrowserDialog.SelectedPath = txtBrowse.Text + @"\net.ERP.AIMS(Backup).bak";                               

            }

        }

 

        private void cmdBackup_Click(object sender, EventArgs e)

        {

            string lstrToday = DateTime.Today.ToString("(MM-dd-yyyy)");

            string lstrFileName = null;

            if (txtBrowse.TextLength == 3)

            {

                lstrFileName = "DATABASE_BACKUP";

            }

            else

            {

                lstrFileName = @"\DATABASE_BACKUP";

            }                       

            string lstrExtension = ".bak";

            string lstrFilePath = null;

            string lstrTodayFile = lstrFileName + lstrToday + lstrExtension;

            if (srvSql != null)

            {

                // Create a new backup operation

                Backup bkpDatabase = new Backup();               

                // Set the backup type to a database backup

                bkpDatabase.Action = BackupActionType.Database;

                // Set the database that we want to perform a backup on               

                bkpDatabase.Database = clsCommonConstants.gmpubcstrDatabaseName;

                // Set the backup device to a file          

                BackupDeviceItem bkpDevice = new BackupDeviceItem(txtBrowse.Text + lstrTodayFile, DeviceType.File);

                bkpDatabase.Devices.Add(bkpDevice);               

                //Add the backup device to the backup                       

                lstrFilePath = txtBrowse.Text + lstrTodayFile;

                if (File.Exists(lstrFilePath))

                {

                    File.Delete(lstrFilePath);

                }             

                // Perform the backup

                bkpDatabase.SqlBackup(srvSql);

                MessageBox.Show("Backup Successfull");              

            }

            else

            {

                // There was no connection established; probably the Connect button was not clicked

                MessageBox.Show("A connection to a SQL server was not established.", "Not Connected to Server", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);

            }

        }

 

        private void DatabaseBackup_Load(object sender, EventArgs e)

        {

            ServerConnection srvConn = new ServerConnection(clsCommonConstants.gmpubstastrServerName.ToString());

            srvSql = new Server(srvConn);          

        }

 

        private void cmdRestore_Click(object sender, EventArgs e)

        {

            string lstrToday = DateTime.Today.ToString("(MM-dd-yyyy)");

            string lstrFileName = null;

            if (txtBrowse.TextLength == 3)

            {

                lstrFileName = "DATABASE_BACKUP";

            }

            else

            {

                lstrFileName = @"\DATABASE_BACKUP";

            }

            string lstrExtension = ".bak";           

            string lstrTodayFile = lstrFileName + lstrToday + lstrExtension;        

            if (srvSql != null)

            {                           

                Restore rstDatabase = new Restore();

                BackupDeviceItem bkpDevice = new BackupDeviceItem(txtBrowse.Text + lstrTodayFile, DeviceType.File);

                // Add the backup device to the restore type

                rstDatabase.Devices.Add(bkpDevice);                

                // Set the database that we want to perform the restore on               

                rstDatabase.Database = clsCommonConstants.gmpubcstrDatabaseName;

                // Set the restore type to a database restore

                rstDatabase.Action = RestoreActionType.Database;

                // If the database already exists, replace it                                                               

                rstDatabase.ReplaceDatabase = true;             

                // Perform the restore

                rstDatabase.SqlRestore(srvSql);

                MessageBox.Show("Restore Successfull");         

            }

            else

            {

                // There was no connection established; probably the Connect button was not clicked

                MessageBox.Show("A connection to a SQL server was not established.", "Not Connected to Server", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);

            }         

        }

 

        private void txtBrowse_TextChanged(object sender, EventArgs e)

        {

 

        }     

    }

}

 


Categories: C#, Personal, SQL

Post a Comment

Oops!

Oops, you forgot something.

Oops!

The words you entered did not match the given text. Please try again.

You must be a member to comment on this page. Sign In or Register

0 Comments