How to take backup sql server database through ASP.NET and C# .NET ?
In this article I am going to explain simple way to take backup of your database from ASP.NET. This is one line of code to take backup of your database. Use this technique in windows application too to take auto backup every day.
Description
Normally we are handled lots of query to process and save / modify records in database. Some time we did mistake in delete query to loss some records, in that time we need old database backup to recover database.
Here I am explained in simple way to take back up of your database. Use this technique in windows application too in form load event and add that .exe to windows scheduled task to take back up automatically every day. In ASP.NET
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=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 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 and add scheduled task
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!");
}
}
}
}
Build that windows application in bin directory .exe file automatically created.
Now go to control panel choose sheduled 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 help you to learn take database in easy way.
Nice.........