Introduction
This is a sample C# (Visual Studio 2005) application for Automatic SQL Server Backup Utility using sqlserveragent. I have used SQL-DMO DLL. This article will show you how to create a automatic backup in SQL Server 2000.
This code should work on any PC using VB.NET and installed SQL Server 2000 (any edition or Client Components for SQL Server 2000).
SQLDMO (which is always installed between Microsoft SQL Server 2000 or Microsoft SQL Server Client Tools).
To Do
- First enter your SQL Server username and password in the corresponding Text Box
- Set backup Start date & Backup Time
- After finishing this, please check manually whether it will working or not
Manual Working Procedure
- Run SQL Server Enterprise Manager
- Select management option
- Open SQL Server agent
- Open Jobs window
- Check whether job item exists or not
- Right click on newly created job item, then we will get one
- Popup menu, then select start job
- After finish the job, check in the folder D:\backup whether the backup file has been created or not
Important Functions
Add Reference to SQL-DMO DLL
You can do this by right clicking the project in Solution Explorer, then selecting 'Add Reference', COM components and the latest version of "Microsoft SQLDMO Object Library".
Available Server
public void dIsplayServerList(ComboBox cboListName)
{
try
{
SQLDMO.Application oSQLServerDMOApp = new SQLDMO.Application();
Info.informationLayer info = new Info.informationLayer();
SQLDMO.NameList oNameList;
oNameList = oSQLServerDMOApp.ListAvailableSQLServers();
for (int intIndex = 0; intIndex <= oNameList.Count - 1; intIndex++)
{
if (oNameList.Item(intIndex as object) != null)
{
cboListName.Items.Add(oNameList.Item(intIndex).ToString());
}
}
if (cboListName.Items.Count > 0) cboListName.SelectedIndex = 0;
else cboListName.Text = "(Local)";
}
catch
{
}
}
Available Databases
public void dIsplayDatabases(ComboBox cboDatabase,Info.informationLayer info)
{
try
{
SQLDMO._SQLServer SQLServer = new SQLDMO.SQLServerClass();
cboDatabase.Items.Clear();
SQLServer.Connect(info.strServerName,info.strLoginName,
info.strPwd);
foreach (SQLDMO.Database db in SQLServer.Databases)
{
if (db.Name != null)
cboDatabase.Items.Add(db.Name);
}
cboDatabase.Sorted = true;
if (cboDatabase.Items.Count == 0)cboDatabase.Text = "<NO found databases>";
}
catch (Exception err)
{
info.ErrorMessageDataLayer = err.Message;
}
}
Create Job on Server Agent
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";
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;
}
}
}
Create Job Schedule on Server Agent
public void CreateShedule_Sql(Info.informationLayer info)
{
try
{
SQLDMO.Job SQLJob = new SQLDMO.Job();
SQLDMO._SQLServer SQLServer = new SQLDMO.SQLServerClass();
SQLDMO.JobSchedule SQLSchedule = new SQLDMO.JobSchedule();
SQLServer.Connect(info.strServerName, info.strLoginName, info.strPwd);
SQLJob = SQLServer.JobServer.Jobs.Item(info.strDatabaseName);
SQLSchedule.Name = "Weekly Backup";
SQLSchedule.Schedule.FrequencyType
= SQLDMO.SQLDMO_FREQUENCY_TYPE.SQLDMOFreq_Weekly;
SQLSchedule.Schedule.FrequencyInterval = 2;
SQLSchedule.Schedule.FrequencyRecurrenceFactor = 2;
SQLSchedule.Schedule.ActiveStartDate = info.intStartDate;
SQLSchedule.Schedule.ActiveStartTimeOfDay = info.intStartTime;
SQLSchedule.Schedule.ActiveEndDate = 99991231;
SQLSchedule.Schedule.ActiveEndTimeOfDay = 235959;
SQLJob.BeginAlter();
SQLJob.JobSchedules.Add(SQLSchedule);
SQLJob.DoAlter();
info.ErrorMessageDataLayer = "New Sql Job [Databasename= "
+ info.strDatabaseName + " ]Successfully Created. ";
}
catch (Exception err)
{
info.ErrorMessageDataLayer = err.Message;
}
}
History
- 5th October, 2006: Initial post