Function which is used to take backup from Sql Server via Code
protected void BackUpNow()
{
try
{
using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ToString()))
{
string query = "BACKUP DATABASE Northwind " +
"TO DISK = 'C:\\Setup\\Northwind.bak' " +
"WITH NAME = 'ApressFinancial-Full Database Backup', " +
"SKIP, " +
"NOUNLOAD, " +
"STATS = 10";
using (SqlCommand cmd = new SqlCommand(query, conn))
{
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
Response.Write("Your Database Backup Successfully Done");
}
}
}
catch(Exception ex)
{
lblException.Text = ex.Message;
}
}
Call this Function on Page Load
protected void Page_Load(object sender, EventArgs e)
{
BackUpNow();
}
No comments:
Post a Comment