Description :
When User Clicks on Button, Directory is created on D drive if it is not exist else backup database is saved in that folder.
.cs Code :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Windows.Forms;
using System.IO;
namespace Backup
{
public partial class _Default : System.Web.UI.Page
{
string DbName = "CBS";
SqlConnection conStr = new SqlConnection(ConfigurationManager.ConnectionStrings["CBSConnectionString"].ConnectionString);
DataTable dt = new DataTable();
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnBackup_Click(object sender, EventArgs e)
{
string DestDir = "D:\\Backupdb";
if (!System.IO.Directory.Exists(DestDir))
{
System.IO.Directory.CreateDirectory("D:\\Backupdb");
MessageBox.Show("Directory is Created.");
}
try
{
conStr.Open();
string cmdStr = "BACKUP DATABASE CBS TO DISK='"+DestDir+"\\"+DateTime.Now.ToString("ddMMyyyy_HHmmss")+".Bak'";
SqlCommand cmd = new SqlCommand(cmdStr,conStr);
cmd.ExecuteNonQuery();
MessageBox.Show("Backup is done successfully..");
}
catch(SqlException exc)
{
MessageBox.Show(exc.Message);
}
finally
{
conStr.Close();
}
}
}
}
ASPX Code :
<p>Click to Backup your database :</p>
<asp:Button ID="btnBackup" runat="server" onclick="btnBackup_Click"
Text="Backup" />
Screen Shot:
When User Clicks on Button, Directory is created on D drive if it is not exist else backup database is saved in that folder.
.cs Code :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Windows.Forms;
using System.IO;
namespace Backup
{
public partial class _Default : System.Web.UI.Page
{
string DbName = "CBS";
SqlConnection conStr = new SqlConnection(ConfigurationManager.ConnectionStrings["CBSConnectionString"].ConnectionString);
DataTable dt = new DataTable();
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnBackup_Click(object sender, EventArgs e)
{
string DestDir = "D:\\Backupdb";
if (!System.IO.Directory.Exists(DestDir))
{
System.IO.Directory.CreateDirectory("D:\\Backupdb");
MessageBox.Show("Directory is Created.");
}
try
{
conStr.Open();
string cmdStr = "BACKUP DATABASE CBS TO DISK='"+DestDir+"\\"+DateTime.Now.ToString("ddMMyyyy_HHmmss")+".Bak'";
SqlCommand cmd = new SqlCommand(cmdStr,conStr);
cmd.ExecuteNonQuery();
MessageBox.Show("Backup is done successfully..");
}
catch(SqlException exc)
{
MessageBox.Show(exc.Message);
}
finally
{
conStr.Close();
}
}
}
}
ASPX Code :
<p>Click to Backup your database :</p>
<asp:Button ID="btnBackup" runat="server" onclick="btnBackup_Click"
Text="Backup" />
Screen Shot:
No comments :
Post a Comment