Dear Sir,
i want to set sql backup time in every weekend.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Data;
using System.Data.OleDb;
using System.Data.SqlClient;
using System.Configuration;
namespace backupk
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//SqlConnection sqlconn = new SqlConnection(@"Data Source=(localdb)\Projects;Initial Catalog=Cascading_ddl;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False");
SqlConnection sqlconn = new SqlConnection(@"Data Source=10.1.246.4;Initial Catalog=DB_NTPC_MEJA_INTRANET; User ID=dba_ntpc_meja_intranet; Password=alpha$890; Connection Timeout=15;Connection Lifetime=0;Min Pool Size=0;Max Pool Size=100;Pooling=true");
SqlCommand sqlcmd = new SqlCommand();
SqlDataAdapter da = new SqlDataAdapter();
DataTable dt = new DataTable();
// Backup destibation
//string backupDestination = @"C:\\SQLBackUpFolder";
//string backupDestination = @"C: \\10.1.246.4\INTERNET_DRIVE";
string backupDestination = @"E:\Program Files\Microsoft SQL Server\MSSQL14.SQLMTPP\MSSQL\Backup";
//string backupDestination = @"\\10.1.246.4\\E:\\Program Files\\Microsoft SQL Server\\MSSQL14.SQLMTPP\\MSSQL\\Backup";
// check if backup folder exist, otherwise create it.
if (!System.IO.Directory.Exists(backupDestination))
{
System.IO.Directory.CreateDirectory(@"C:\\SQLBackUpFolder");
}
try
{
sqlconn.Open();
sqlcmd = new SqlCommand("backup database DB_NTPC_MEJA_INTRANET to disk='" + backupDestination + "\\" + DateTime.Now.ToString("ddMMyyyy_HHmmss") + ".Bak'", sqlconn);
sqlcmd.ExecuteNonQuery();
//Close connection
sqlconn.Close();
Response.Write("Backup database successfully");
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
}
}
}