Social Icons

Pages

Saturday 8 September 2018

Backup sql server database in c# button click. code

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 BackupDBSoft
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            DateTime d = DateTime.Now;
            string dd = d.Day + "-" + d.Month;
            string servname = textBox1.Text;
            string dbname = textBox2.Text;// database name

            string aaa = @"Data Source=" + servname + ";Integrated Security=True;Initial Catalog=" + dbname + "";
            SqlConnection con = new SqlConnection(aaa);
            //con.ConnectionString = ConfigurationManager.ConnectionStrings["BackupCatalogDBSoft.Properties.Settings."+dbname+"ConnectionString"].ToString();

            con.Open();
            string str = "USE " + dbname + ";";
            string str1 = "BACKUP DATABASE " + dbname +
                " TO DISK = 'D:\\Database\\" + dbname + "_" + dd +
                ".Bak' WITH FORMAT,MEDIANAME = 'Z_SQLServerBackups',NAME = 'Full Backup of " + dbname + "';";
            SqlCommand cmd1 = new SqlCommand(str, con);
            SqlCommand cmd2 = new SqlCommand(str1, con);
            cmd1.ExecuteNonQuery();
            cmd2.ExecuteNonQuery();
            MessageBox.Show("Successfully Completed Backup. You can find this file (DB Name.Bak) in your Disk D:\\.... never edit this file name.");
            con.Close();
            //this.Close();
        }
    }
}

 

Sample text

Sample Text

Sample Text

 
Blogger Templates