Congratulations to all the winners of April 2013, they have won INR 3400 cash and INR 20147 worth prizes !
Go to DotNetFunda.com
Twitter TwitterLinkedIn
YouTubeGoogle
 Online : 20970 |  Welcome, Guest!   Register  Login
Home > Articles > ADO.NET > How to take backup sql server database through ASP.NET & C#.NET

How to take backup sql server database through ASP.NET & C#.NET

2 vote(s)
Rating: 5 out of 5
Article posted by Itsmemohanr on 8/21/2012 | Views: 4368 | Category: ADO.NET | Level: Intermediate | Points: 250 red flag


In this article I am going to explain how to take backup of your database from ASP.NET or from C#.NET.

Introduction

In this article I am going to explain how to take backup of your database either from ASP.NET or C#.NET.


Objective

Backup of your database either from ASP.NET or C#.NET



Using the code



Default.aspx Code Behind

using System.Data;
using System.Data.SqlClient;
using System.IO;

public partial class _Default : System.Web.UI.Page
{
//Metioned here your database name
string dbname = "test1";
SqlConnection sqlcon=new SqlConnection();
SqlCommand sqlcmd = new SqlCommand();
SqlDataAdapter da = new SqlDataAdapter();
DataTable dt = new DataTable();

protected void Page_Load(object sender, EventArgs e)
{

}
protected void Button1_Click(object sender, EventArgs e)
{
//Mentioned Connection string make sure that user id and password sufficient previlages
sqlcon.ConnectionString = @"Server=MOHANDB\SQLEXPRESS;database=" + dbname + ";uid=ravindran;pwd=srirangam;";

//Enter destination directory where backup file stored
string destdir = "D:\\backupdb";

//Check that directory already there otherwise create
if (!System.IO.Directory.Exists(destdir))
{
System.IO.Directory.CreateDirectory("D:\\backupdb");
}
try
{
//Open connection
sqlcon.Open();
//query to take backup database
sqlcmd = new SqlCommand("backup database test to disk='" + destdir + "\\" + DateTime.Now.ToString("ddMMyyyy_HHmmss") + ".Bak'", sqlcon);
sqlcmd.ExecuteNonQuery();
//Close connection
sqlcon.Close();
Response.Write("Backup database successfully");
}
catch (Exception ex)
{
Response.Write("Error During backup database!");
}
}
}

In C#.NET Windows application 

using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Windows.Forms;

namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
//Metioned here your database name
string dbname = "test1";
SqlConnection sqlcon=new SqlConnection();
SqlCommand sqlcmd = new SqlCommand();
SqlDataAdapter da = new SqlDataAdapter();
DataTable dt = new DataTable();

public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{

//Mentioned Connection string make sure that user id and password sufficient previlages
sqlcon.ConnectionString = @"Server=RAVI-PC\SQLEXPRESS;database=" + dbname + ";uid=ravindran;pwd=srirangam;";

//Enter destination directory where backup file stored
string destdir = "D:\\backupdb";

//Check that directory already there otherwise create
if (!System.IO.Directory.Exists(destdir))
{
System.IO.Directory.CreateDirectory("D:\\backupdb");
}
try
{
//Open connection
sqlcon.Open();
//query to take backup database
sqlcmd = new SqlCommand("backup database " + dbname + " to disk='" + destdir + "\\" + DateTime.Now.ToString("ddMMyyyy_HHmmss") + ".Bak'", sqlcon);
sqlcmd.ExecuteNonQuery();
//Close connection
sqlcon.Close();
MessageBox.Show("Backup database successfully");
}
catch (Exception ex)
{
MessageBox.Show("Error During backup database!");
}
}
}
}

1. Now Build that windows application in bin directory .exe file automatically created.
2. Go to control panel choose scheduled task -> Add Schedule task choose that exe file and select which data that exe run after that your database backup automatically created every day and stored in specified location.

Output 

Check the D:\backupdb directory the database backup created automatically. 

Conclusion

I hope this article helps you to learn take database in easy way. 



If you like this article, subscribe to our RSS Feed. You can also subscribe via email to our Interview Questions, Codes and Forums section.

Page copy protected against web site content infringement by Copyscape
Found interesting? Add this to:



Please Sign In to vote for this post.

Experience:3 year(s)
Home page:http://www.dotnetfunda.com
Member since:Tuesday, August 21, 2012
Level:Starter
Status: [Member]
Biography:
 Responses
Posted by: Ksuresh | Posted on: 03 Sep 2012 07:38:11 AM | Points: 25

Hi Mohan Rajendran,

Thanks, Very good Article, it worked for me

Please provide me for MySql Database.

Thanks and Regards
Suresh

>> Write Response - Respond to this post and get points
Related Posts

To my surprise, there are still software development houses that still initiate Development using languages like VB6 and obviously using ADO. In most of the time when someone has a Problem about ADO, I fail to referrer them to an article about Introduction to ADO.NET. So I thought I should write one that will tell them how great it is to build Applications in .NET and ADO.NET is the best thing that has ever happened to Database Programming. In this Article, we are going to introduce those who are still experiencing ADO Problems to .Net.

In this article, we shall learn how to save an Image into the SQL Server database using Stored Procedure in ASP.NET.

In this post I introduce you to Linq and some of its very important features.

We will see overview of ADO.NET. This is the first part of the ADO.NET Series article.

The following example illustrates how to insert a new user in a users table that has a username and password field.

More ...
About Us | Contact Us | The Team | Advertise | Software Development | Write for us | Testimonials | Privacy Policy | Terms of Use | Link Exchange | Members | Go Top
General Notice: If you find plagiarised (copied) contents on this page, please let us know the original source along with your correct email id (to communicate) for further action.
Copyright © DotNetFunda.Com. All Rights Reserved. Copying or mimicking the site design and layout is prohibited. Logos, company names used here if any are only for reference purposes and they may be respective owner's right or trademarks. | 5/19/2013 11:09:47 AM