public void CreateJob_Sql(Info.informationLayer info)
{
try
{
SQLDMO._SQLServer SQLServer = new SQLDMO.SQLServerClass();
SQLDMO.Job SQLJob = new SQLDMO.Job();
SQLDMO.JobSchedule SQLSchedule = new SQLDMO.JobSchedule();
SQLServer.Connect(info.strServerName, info.strLoginName, info.strPwd);
switch (SQLServer.JobServer.Status)
{
case SQLDMO_SVCSTATUS_TYPE.SQLDMOSvc_Stopped:
SQLServer.JobServer.Start();
SQLServer.JobServer.AutoStart = true;
break;
}
SQLJob.Name = info.strDatabaseName;
SQLJob.Description = "Check and Backup" + info.strDatabaseName;
SQLServer.JobServer.Jobs.Add(SQLJob);
SQLJob.Category = "Database Maintenance";
SQLDMO.JobStep aJobStep = new SQLDMO.JobStep();
aJobStep.Name = "Step 2: Backup the Database";
aJobStep.StepID = 1;
aJobStep.DatabaseName = info.strDatabaseName;
aJobStep.SubSystem = "TSQL";
//------>>> If BackUp Folder is Not Found then create BackUp Folder.
string DirectoryName = "D:\\BackUp";
if (Directory.Exists(DirectoryName)==false)
{
System.IO.Directory.CreateDirectory(DirectoryName);
}
//------>>>
string sExt;
sExt="EXEC master.dbo.xp_sqlmaint '-S " + info.strServerName + " -U " + info.strLoginName + " -P " + info.strPwd + " -D " + info.strDatabaseName + " -CkDB -CkAl -CkCat -BkUpMedia DISK -BkUpDB D:\\Backup -BkExt BAK -DelBkUps 2weeks -BkUpOnlyIfClean -Rpt D:\\Backup\\BackDB_Checks.txt'";
aJobStep.Command = sExt;
aJobStep.OnSuccessAction = SQLDMO_JOBSTEPACTION_TYPE.SQLDMOJobStepAction_QuitWithSuccess;
aJobStep.OnFailAction = SQLDMO_JOBSTEPACTION_TYPE.SQLDMOJobStepAction_QuitWithFailure;
SQLJob.JobSteps.Add(aJobStep);
SQLJob.ApplyToTargetServer(info.strServerName);
aJobStep.DoAlter();
SQLJob.Refresh();
aJobStep.Refresh();
}
catch (Exception Err)
{
info.ErrorMessageDataLayer = Err.Message;
}
}