Take SQL Server Database Backup in ASP.Net

This article explains how to take a backup of our database using C# and ASP.NET.

The following is my screen:

backup SQL Server DataBase
                                                                     Image 1

The following is my aspx:

  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>  
  2.   
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  4. <html xmlns="http://www.w3.org/1999/xhtml">  
  5. <head runat="server">  
  6.     <title>Backup and Restore DataBase</title>  
  7. </head>  
  8. <body>  
  9.     <form id="form1" runat="server">  
  10.     <div>  
  11.         <table cellpadding="10" cellspacing="10" style="border: solid 10px red; background-color: Skyblue;"  
  12.             width="90%" align="center">  
  13.             <tr>  
  14.                 <td style="height: 35px; background-color: Yellow; font-weight: bold; font-size: 16pt;  
  15.                     font-family: Times New Roman; color: Red" align="center">  
  16.                     Backup SQL Server DataBase  
  17.                 </td>  
  18.             </tr>  
  19.             <tr>  
  20.                 <td align="center">  
  21.                     <asp:Label ID="lblError" runat="server" ForeColor="Red"></asp:Label>  
  22.                 </td>  
  23.             </tr>  
  24.             <tr>  
  25.                 <td align="right">  
  26.                     <asp:Button ID="btnBackup" runat="server" Text="Backup DataBase" OnClick="btnBackup_Click" />  
  27.                 </td>  
  28.    
  29.             </tr>   
  30.         </table>  
  31.     </div>  
  32.     </form>  
  33. </body>  
  34. </html>  
My aspx.cs code is:
  1. using System;  
  2. using System.Configuration;  
  3. using System.Data;  
  4. using System.Linq;  
  5. using System.Web;  
  6. using System.Web.Security;  
  7. using System.Web.UI;  
  8. using System.Web.UI.HtmlControls;  
  9. using System.Web.UI.WebControls;  
  10. using System.Web.UI.WebControls.WebParts;  
  11. using System.Xml.Linq;  
  12. using System.Data.SqlClient;  
  13.   
  14. public partial class _Default : System.Web.UI.Page  
  15. {  
  16.     SqlConnection con = new SqlConnection();  
  17.     SqlCommand sqlcmd = new SqlCommand();  
  18.     SqlDataAdapter da = new SqlDataAdapter();  
  19.     DataTable dt = new DataTable();  
  20.   
  21.     protected void Page_Load(object sender, EventArgs e)  
  22.     {  
  23.   
  24.     }  
  25.     protected void btnBackup_Click(object sender, EventArgs e)  
  26.     {  
  27.         //IF SQL Server Authentication then Connection String  
  28.         //con.ConnectionString = @"Server=MyPC\SqlServer2k8;database=" + YourDBName + ";uid=sa;pwd=password;";  
  29.   
  30.         //IF Window Authentication then Connection String  
  31.         con.ConnectionString = @"Server=MyPC\SqlServer2k8;database=Test;Integrated Security=true;";  
  32.   
  33.         string backupDIR = "H:\\BackupDB";  
  34.         if (!System.IO.Directory.Exists(backupDIR))  
  35.         {  
  36.             System.IO.Directory.CreateDirectory(backupDIR);  
  37.         }  
  38.         try  
  39.         {  
  40.             con.Open();  
  41.             sqlcmd = new SqlCommand("backup database test to disk='" + backupDIR + "\\" + DateTime.Now.ToString("ddMMyyyy_HHmmss") + ".Bak'", con);  
  42.             sqlcmd.ExecuteNonQuery();  
  43.             con.Close();  
  44.             lblError.Text = "Backup database successfully";  
  45.         }  
  46.         catch (Exception ex)  
  47.         {  
  48.             lblError.Text = "Error Occured During DB backup process !<br>" + ex.ToString();  
  49.         }  
  50.     }  
  51. }  
Check your backup directory:

backup database
                                                                     Image 2

 


Similar Articles