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

backup database
Image 2