This article explains how to take a backup of our database using C# and ASP.NET.
The following is my screen:
Image 1
The following is my aspx:
- <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head runat="server">
- <title>Backup and Restore DataBase</title>
- </head>
- <body>
- <form id="form1" runat="server">
- <div>
- <table cellpadding="10" cellspacing="10" style="border: solid 10px red; background-color: Skyblue;"
- width="90%" align="center">
- <tr>
- <td style="height: 35px; background-color: Yellow; font-weight: bold; font-size: 16pt;
- font-family: Times New Roman; color: Red" align="center">
- Backup SQL Server DataBase
- </td>
- </tr>
- <tr>
- <td align="center">
- <asp:Label ID="lblError" runat="server" ForeColor="Red"></asp:Label>
- </td>
- </tr>
- <tr>
- <td align="right">
- <asp:Button ID="btnBackup" runat="server" Text="Backup DataBase" OnClick="btnBackup_Click" />
- </td>
- </tr>
- </table>
- </div>
- </form>
- </body>
- </html>
- using System;
- using System.Configuration;
- using System.Data;
- using System.Linq;
- using System.Web;
- using System.Web.Security;
- using System.Web.UI;
- using System.Web.UI.HtmlControls;
- using System.Web.UI.WebControls;
- using System.Web.UI.WebControls.WebParts;
- using System.Xml.Linq;
- using System.Data.SqlClient;
- public partial class _Default : 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)
- {
- //IF SQL Server Authentication then Connection String
- //con.ConnectionString = @"Server=MyPC\SqlServer2k8;database=" + YourDBName + ";uid=sa;pwd=password;";
- //IF Window Authentication then Connection String
- con.ConnectionString = @"Server=MyPC\SqlServer2k8;database=Test;Integrated Security=true;";
- string backupDIR = "H:\\BackupDB";
- if (!System.IO.Directory.Exists(backupDIR))
- {
- System.IO.Directory.CreateDirectory(backupDIR);
- }
- try
- {
- con.Open();
- sqlcmd = new SqlCommand("backup database test to disk='" + backupDIR + "\\" + DateTime.Now.ToString("ddMMyyyy_HHmmss") + ".Bak'", con);
- sqlcmd.ExecuteNonQuery();
- con.Close();
- lblError.Text = "Backup database successfully";
- }
- catch (Exception ex)
- {
- lblError.Text = "Error Occured During DB backup process !<br>" + ex.ToString();
- }
- }
- }

Image 2

Vrahmdev TiwariPosted Jun 7, 2018, 7:22 AM
Dear Sir, I want to create backup my online database. So Please help me how to do that ? It's not working online.
Hem BhattaPosted Jun 25, 2016, 7:07 AM
What about restoring database plz provide restore database code
Morteza KermaniPosted Mar 28, 2016, 6:21 AM
Thanks for your code. I have extended it to automatically get Connection string and database name: string ConnectionString = ConfigurationManager.ConnectionStrings["SiteSqlServer"].ConnectionString; sqlcmd = new SqlCommand("DECLARE @DataBaseName as nVarchar(60);SET @DataBaseName = (SELECT DB_NAME() AS DataBaseName); backup database @DataBaseName to disk='" + backupDIR + "\\" + DateTime.Now.ToString("ddMMyyyy_HHmmss") + ".Bak'", con);
ప్రభాకర్ జాపతిPosted Feb 9, 2016, 9:26 AM
thnak u sir..
Mohamed Gani MnPosted Oct 30, 2015, 1:46 PM
Is it all manual type of backup
Rahul Kumar SaxenaPosted Jul 14, 2015, 3:22 PM
Thanks Amrit Sharma
Amrit SharmaPosted Jul 14, 2015, 1:38 PM
nice. thank you so much for this article.
Sunil DeshalahrePosted Jun 15, 2015, 10:30 AM
I have code for dynamic connectivity to SQL server databse. It works fine for all the Network and 3rd party dedicated server. But the above code dose not work for the database which is hosted in server.
Sunil DeshalahrePosted Jun 15, 2015, 10:28 AM
It wont work in IIS.
Parth PatoliyaPosted Feb 8, 2015, 12:51 AM
server error--"Could not find a part of the path 'E:\backup'." plz help me for this
Cem GunduzPosted Oct 30, 2014, 4:35 AM
it creates backup file in server computer. how can i save backup file in my local computer?
Chandradev PrasadPosted Oct 30, 2014, 2:19 AM
Nice article.