Wednesday, 20 April 2011

All Database Backup Program Using C#.Net


All Database Backup Program Using C#.Net

Add Refrences From Solution Explore

Microsoft.SqlServer.ConnectionInfo
Microsoft.SqlServer.Smo
Microsoft.SqlServer.SmoEnum
Interop.SQLDMO


Namespaces:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.SqlServer.Server;
using Microsoft.SqlServer.Management.Smo;
using Microsoft.SqlServer.Management;
using System.IO;

namespace WindowsFormsApplication3
{
   
    public partial class Form1 : Form
    {
        SqlConnection cn;
        //SqlDataAdapter da;
        DataTable dt;
        SqlDataReader dr;
        SqlCommand cmd;
       
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            GetAllServer();
            this.Text= "DataBase Backup Program";
        }
      
        //To Get All Server

        public void GetAllServer()
        {
            dt = SmoApplication.EnumAvailableSqlServers(false);
            if (dt.Rows.Count > 0)
            {
                foreach (DataRow dr in dt.Rows)
                {
                    comboBoxServer.Items.Add((dr["Name"]));
                }
            }
        }
      
        //To Get All DataBase of a Selected Server
       
        public void GetAllDataBase()
        {
            try
            {
                cn = new SqlConnection("server=" + comboBoxServer.SelectedItem.ToString() + ";uid=" + textBoxUid.Text + ";pwd=" + textBoxPassword.Text + ";");
                cn.Open();
                cmd = new SqlCommand();
                cmd.Connection = cn;
                cmd.CommandType = CommandType.StoredProcedure;

                //SYSTEM STORE PROCEDURE
                cmd.CommandText = "sp_databases";


                dr = cmd.ExecuteReader();

                while (dr.Read())
                {
                    comboBoxDatabaseList.Items.Add(dr.GetString(0));
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());

               
            }
        }

       
        //To Take Backup
       
        public void TakeBackUp(string BackupDBName, string FileNamePath)
        {
            try
            {
                if (textBoxUid.Text != "" && textBoxPassword.Text != "")
                {
                    Server sqlServerInstance = new Server(new Microsoft.SqlServer.Management.Common.ServerConnection(new System.Data.SqlClient.SqlConnection("Data Source=" + comboBoxServer.SelectedItem.ToString() + ";Initial Catalog=" + comboBoxDatabaseList.SelectedItem.ToString() + "; uid=" + textBoxUid.Text + "; pwd=" + textBoxPassword.Text + ";")));
                    Backup objBackup = new Backup();
                   
                    objBackup.Devices.AddDevice(FileNamePath, DeviceType.File);
                    objBackup.Database = BackupDBName;
                    objBackup.Action = BackupActionType.Database;
                    objBackup.SqlBackup(sqlServerInstance);
           
                    MessageBox.Show("The backup of database " + "'" + BackupDBName + "'" + " completed sccessfully", "Microsoft SQL Server Management Studio", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    MessageBox.Show("Please enter userId and password");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }


        private void button1_Click(object sender, EventArgs e)
        {

            //connect Button

             if (comboBoxServer.SelectedIndex > 0)
            {
                if (textBoxUid.Text != "" && textBoxPassword.Text != "")
                {
                    GetAllDataBase();
                    MessageBox.Show("Successfully DataBase Connected.");
                }
                else
                {
                    MessageBox.Show("Please enter userId and password");
                }
            }
            else
            {
                MessageBox.Show("Please select a Server to connect");
            }
        }
       

        private void button2_Click(object sender, EventArgs e)
        {
            SaveFileDialog sfd = new SaveFileDialog();
           
            sfd.Title = "BackUp To...";
            sfd.Filter = "Backup File|*.bak";
            sfd.FileName = "Backup_"+ comboBoxDatabaseList.SelectedItem.ToString()+ "_" + DateTime.Now.ToLongDateString()  + ".bak";
          
            if (sfd.ShowDialog() == DialogResult.OK)
            {               
                if (comboBoxDatabaseList.SelectedIndex > 0)
                {                
                   TakeBackUp(comboBoxDatabaseList.SelectedItem.ToString(),sfd.FileName);
                                    
                }
                else
                {
                    MessageBox.Show("Please select a database to backup");
                }
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            //Exit Application

            this.Close();
        }

        private void comboBoxServer_SelectedIndexChanged(object sender, EventArgs e)
        {
            comboBoxDatabaseList.Items.Clear();
            textBoxUid.Text = "";
            textBoxPassword.Text = "";
           
            MessageBox.Show("Enter UserId And Password to connect this DataSource.");
        }

        private void button4_Click(object sender, EventArgs e)
        {

              //Clear Button 
            comboBoxServer.Items.Clear();
            comboBoxServer.Text = "";
            textBoxUid.Text = "";
            textBoxPassword.Text = "";
            comboBoxDatabaseList.Items.Clear();
            comboBoxDatabaseList.Text = ""; 
            cn.Close();
           
           
        }

              
    }
}

Screen Shots:






No comments:

Post a Comment