A day with .Net

My day to day experince in .net

Back up/Restore SQL SERVER 2000 database using SQLDMO.DLL

Posted by vivekcek on July 24, 2009

So in the previous post i discussed about how attach .mdf file to sql server 2000 by using sqldmo.dll.Now i am discussing about how back up and restore sql server databse using sqldmo.

The UI and code is given below.UI include one open file dialog and save file dialog.

BACKRESTORE

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using SQLDMO;
using System.Xml;
using System.Data.SqlClient;
namespace eStock
{
    public partial class frmBackUp : Form
    {
        private string server, uid, pwd, database;
        public frmBackUp()
        {
            InitializeComponent();
        }

        private void frmBackUp_Load(object sender, EventArgs e)
        {
           Getinfo();
           txtServer.Text=  server; 
           txtUid.Text= uid; 
           txtPwd.Text= pwd;
           txtDatabase.Text= database;

        }
       private void Getinfo()
        {
            try
            {
                XmlTextReader xmlreadr = new XmlTextReader("config.xml");
                xmlreadr.WhitespaceHandling = WhitespaceHandling.None;
                xmlreadr.MoveToContent();
                XmlReader readr = xmlreadr;
                server = readr.ReadElementString();
                uid = readr.ReadElementString();
                pwd = readr.ReadElementString();
                database = readr.ReadElementString();
                readr.Close();

            
            
            }
           catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
           }
        
        }

        private void cmdBackupRestore_Click(object sender, EventArgs e)
        {
            if (this.rdbBackUp.Checked)
            {
                Backup();
            }
            else
                Restore();
            
        }

        private void Backup()
        {
            try
            {
                this.Cursor = Cursors.WaitCursor;
                //create an instance of a server class
                SQLDMO._SQLServer srv = new SQLDMO.SQLServerClass();
                //connect to the server
                srv.Connect(this.txtServer.Text, this.txtUid.Text, this.txtPwd.Text);
                //create a backup class instance
                SQLDMO.Backup bak = new SQLDMO.BackupClass();
                //set the backup device = files property ( easy way )
                bak.Devices = bak.Files;
                //set the files property to the File Name text box
                bak.Files = this.txtFile.Text;
                //set the database to the chosen database
                bak.Database = this.txtDatabase.Text;
                //perform the backup
                bak.SQLBackup(srv);
                MessageBox.Show("Database successfully backed up.", "Backup Successfull");
                this.Cursor = Cursors.Default;
            }
            catch (Exception err)
            {
                this.Cursor = Cursors.Default;
                MessageBox.Show(err.Message, "Error");
            }
        }

        private void Restore()
        {
           
            try
            {
                SqlConnection.ClearAllPools();
                this.Cursor = Cursors.WaitCursor;
                
                //create an instance of a server class
                SQLDMO._SQLServer srv = new SQLDMO.SQLServerClass();
                //connect to the server
                srv.Connect(this.txtServer.Text, this.txtUid.Text, this.txtPwd.Text);
                //create a restore class instance
                SQLDMO.Restore res = new SQLDMO.RestoreClass();
                //set the backup device = files property ( easy way )
                res.Devices = res.Files;
                //set the files property to the File Name text box
                res.Files = this.txtFile.Text;
                //set the database to the chosen database
                res.Database = this.txtDatabase.Text;
                // Restore the database
                res.ReplaceDatabase = true;
                res.SQLRestore(srv);
                MessageBox.Show("Database restored successfully,Restart eStock.", "Restore Successfull");
                this.Cursor = Cursors.Default;
            }
            catch (Exception err)
            {
                this.Cursor = Cursors.Default;
                MessageBox.Show(err.Message, "Error");
            }
        }

        private void cmdFile_Click(object sender, EventArgs e)
        {
            if (this.rdbBackUp.Checked)
            {
                //backup
                this.saveFileDialog1.ShowDialog();
                if (this.saveFileDialog1.FileName != "")
                    this.txtFile.Text = this.saveFileDialog1.FileName;
            }
            else
            {
                //restore
                this.openFileDialog1.ShowDialog();
                if (this.openFileDialog1.FileName != "")
                    this.txtFile.Text = this.openFileDialog1.FileName;
            }
        }

        private void rdbBackUp_CheckedChanged(object sender, EventArgs e)
        {
            this.cmdBackupRestore.Text = "Back Up";
        }

        private void rdbRestore_CheckedChanged(object sender, EventArgs e)
        {
          this.cmdBackupRestore.Text = "Restore";
        }
    }
}

Advertisements

6 Responses to “Back up/Restore SQL SERVER 2000 database using SQLDMO.DLL”

  1. Blad said

    hello, I love your application, but what is the XML file content?, I mean the “config.xml”, I hope you can help, thanks!! :-))

  2. Blad said

    HELLO, THANKS FOR THE PROMPT REPLY, BUT NOT TO PUT IN THE FILE config.xml, please help.

    • rakshit said

      config.xml is the application configuration file ..
      which contains the all database/server settings such as serverid passwords .conn strings..this is automatic application generated file…
      but i think this is not a good option ….
      the best solution is to simply specify the server name implictly…

      • vivekcek said

        hi i just give a name config.xml,when this application is installing the server credentials are obtained form user.actually this is not a stand alone app,its a part of an inventory work.

    • vivekcek said

      its not the application config file i mean,i just give a name to the xml file

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s