Creating SQL server database backup file in Asp.Net with C#

28 Jan 2018 Kamal Pratap
0 Comments 3226 Views

Database administrator need to takes database backup regularly. If you backup database files regularly, you can save your information.

SQL Server database backup can be done in many ways. You can either use SQL Commands (Will explain in upcoming post Create and schedule database backup job using SQL Server Management Studio) or create your own code in .Net to backup data file. Today we explain how to create SQL Server database backup file in ASP.Net using C#. 

Use this code in Aspx Page

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1" %>

<!DOCTYPE html>
<html xmlns="">
<head runat="server">
    <title>SQL Server Database backup in Asp.Net using C#</title>
    <form id="form1" runat="server">
            <table cellpadding="10" cellspacing="10" style="border: solid 1px black;" width="50%" align="center">
                    <td style="height: 35px; background-color: blue; font-weight: bold; font-size: 16pt; font-family: Times New Roman; color: White"
                        align="center">Backup SQL Server DataBase  
                    <td align="center">
                        <asp:Label ID="lblError" runat="server" ForeColor="Red"></asp:Label>
                    <td align="center">
                        <asp:Button ID="btnBackup" runat="server" Text="Backup DataBase" OnClick="btnBackup_Click" />


First we create the connection from the database using connection string then we give the path where we generate the file. After debug the code we will see the backup file in the folder.

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication1
    public partial class WebForm1 : System.Web.UI.Page
        SqlConnection con = new SqlConnection();
        SqlCommand sqlcmd = new SqlCommand();
        SqlDataAdapter da = new SqlDataAdapter();
        DataTable dt = new DataTable();
        protected void Page_Load(object sender, EventArgs e)

        protected void btnBackup_Click(object sender, EventArgs e)
            //Define the database connetion string here
            con.ConnectionString = @"uid=sa; pwd=sql; database=Test; data source=DESKTOP-AQRK51N;";

            string backupDIR = "E:\\DatabaseBackup";
            if (!System.IO.Directory.Exists(backupDIR))
                sqlcmd = new SqlCommand("backup database test to disk='" + backupDIR + "\\" + DateTime.Now.ToString("ddMMyyyy_HHmmss") + ".Bak'", con);
                lblError.Text = "Backup database successfully";
            catch (Exception ex)
                lblError.Text = "Error Occured During DB backup process !<br>" + ex.ToString();


Kamal Pratap

I have 6+ years experience in .Net technologies like Asp.Net, C#, WCF, Web Services, SQL Server, Ajax, LinQ. Currently I am working in Netcarrots Loyalty Services as a Software Developer.


No coments found to display!

Leave a Comment