Friday 22 April 2011

How to Taking Full BackUp our Database through Query?"


How to Taking Full BackUp our Database through Sql Query?"

BACKUP DATABASE SchoolMgmt TO DISK='c:\Full.Bak' WITH INIT

use master
RESTORE DATABASE SchoolMgmt FROM DISK='c:\Full.Bak' WITH NORECOVERY, REPLACE



How to Taking Full BackUp our Database through Query?"


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace WindowsFormsApplication8
{
    public partial class Form1 : Form
    {
        SqlConnection cn;
        SqlDataAdapter da;
        DataSet ds;
        SqlCommand cmd;
        SqlDataReader dr;
        //DataTable dt;

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //Button 1

            try
            {
                cn = new SqlConnection("Data Source=COMP-2;Initial Catalog=Master;Persist Security Info=True;User ID=sa;Password=sa123");
                cn.Open();
                da = new SqlDataAdapter("BACKUP DATABASE Master TO DISK='c:\\Full.Bak' WITH INIT", cn);
                ds = new DataSet();
                da.Fill(ds, "Master");

                MessageBox.Show("DataBase Backup Successfull");
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }

        }

        private void Form1_Load(object sender, EventArgs e)
        {
          
        }

                OR

        private void button3_Click(object sender, EventArgs e)
        {
            // Button 2
                   SaveFileDialog sfd = new SaveFileDialog();

                    sfd.Title = "BackUp To...";
                    sfd.Filter = "Backup File|*.bak";
                    sfd.FileName = "Backup_Master " + DateTime.Now.ToLongDateString() + ".Bak";

                    if (sfd.ShowDialog() == DialogResult.OK)
                    {
                        try
                        {
                            cn = new SqlConnection("Data Source=COMP-2;Initial Catalog=Master; Persist Security Info=True;User ID=sa;Password=sa123");
                            cn.Open();
                            da = new SqlDataAdapter("BACKUP DATABASE Master TO DISK='" + sfd.FileName + "'" + " WITH INIT ", cn);
                            ds = new DataSet();
                            da.Fill(ds, "Master");

                            MessageBox.Show("DataBase Backup Succesfull");
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.Message.ToString());
                        }
                    }
        }
    }
}

No comments:

Post a Comment