Click here to Skip to main content
13,053,891 members (58,977 online)
Click here to Skip to main content
Add your own
alternative version


38 bookmarked
Posted 5 Oct 2006

Automatic SQL Server Backup Utility using sqlserveragent

, 5 Oct 2006
Rate this:
Please Sign up or sign in to vote.
Automatic SQL Server Backup Utility using sqlserveragent
Sample Image - Sql_backup_utility.jpg


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

  1. First enter your SQL Server username and password in the corresponding Text Box
  2. Set backup Start date & Backup Time
  3. After finishing this, please check manually whether it will working or not

Manual Working Procedure

  1. Run SQL Server Enterprise Manager
  2. Select management option
  3. Open SQL Server agent
  4. Open Jobs window
  5. Check whether job item exists or not
  6. Right click on newly created job item, then we will get one
  7. Popup menu, then select start job
  8. 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)
        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)
        if (cboListName.Items.Count > 0) cboListName.SelectedIndex = 0;
        else cboListName.Text = "(Local)";

Available Databases

public void dIsplayDatabases(ComboBox cboDatabase,Info.informationLayer info)
            SQLDMO._SQLServer SQLServer = new SQLDMO.SQLServerClass();
            foreach (SQLDMO.Database db in SQLServer.Databases)
                if (db.Name != null)
            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)
            SQLDMO._SQLServer SQLServer = new SQLDMO.SQLServerClass();
            SQLDMO.Job SQLJob = new SQLDMO.Job();
            SQLDMO.JobSchedule SQLSchedule = new SQLDMO.JobSchedule();

            SQLServer.Connect(info.strServerName, info.strLoginName, 

            switch (SQLServer.JobServer.Status)
            case SQLDMO_SVCSTATUS_TYPE.SQLDMOSvc_Stopped:
                SQLServer.JobServer.AutoStart = true;

            SQLJob.Name = info.strDatabaseName;
            SQLJob.Description = "Check and Backup" + info.strDatabaseName;

            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)
            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;
              = SQLDMO_JOBSTEPACTION_TYPE.SQLDMOJobStepAction_QuitWithSuccess;
              = SQLDMO_JOBSTEPACTION_TYPE.SQLDMOJobStepAction_QuitWithFailure;

        catch (Exception Err)
            info.ErrorMessageDataLayer = Err.Message;

Create Job Schedule on Server Agent

public void CreateShedule_Sql(Info.informationLayer info)
        //it will take backup every week 2 day
        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);

        // create a new JobSchedule object
        SQLSchedule.Name = "Weekly Backup";
        SQLSchedule.Schedule.FrequencyInterval = 2;
        SQLSchedule.Schedule.FrequencyRecurrenceFactor = 2; 

        // // start on Feb18, 2000 - at 12.55
        SQLSchedule.Schedule.ActiveStartDate = info.intStartDate;
        SQLSchedule.Schedule.ActiveStartTimeOfDay = info.intStartTime;
        ////  this schedule has no end time or end date

        SQLSchedule.Schedule.ActiveEndDate = 99991231;
        SQLSchedule.Schedule.ActiveEndTimeOfDay = 235959;

        ////  add the schedule to the Job
        info.ErrorMessageDataLayer = "New Sql Job [Databasename= " 
            + info.strDatabaseName + " ]Successfully Created.  ";
    catch (Exception err)
        info.ErrorMessageDataLayer = err.Message;


  • 5th October, 2006: Initial post


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Web Developer
India India
I'm Joshy George.I finished my Post Graduation in Computer Application,i'm from the great 'Gods Own Country', Kerala, India. Now I'm working in Infopark Kochin, as a specialised areas are C# ,Asp.Net ,Vb6,smart card application,Tapii,Comm ctrls.

You may also be interested in...


Comments and Discussions

Bugerror Pin
Member 111519145-May-15 23:49
memberMember 111519145-May-15 23:49 
QuestionError Pin
Ahmad Halabi30-Jul-13 0:13
memberAhmad Halabi30-Jul-13 0:13 
GeneralMy vote of 4 Pin
Chien Thanh4-Apr-12 17:04
memberChien Thanh4-Apr-12 17:04 
Generalit is not working in sql server 2008 too Pin
Watcharakorn Wanich22-May-11 6:03
memberWatcharakorn Wanich22-May-11 6:03 
Questionhow to pass windows authentication parameter Pin
kirankkk200929-Jan-10 18:42
memberkirankkk200929-Jan-10 18:42 
Generalgood one, thanks Pin
itayl27-May-09 12:14
memberitayl27-May-09 12:14 
GeneralError : Cannot start this job because it doesn't not have any step Pin
DungVinh24-Sep-08 18:23
memberDungVinh24-Sep-08 18:23 
Thanks your solution !
"Right click on newly created job item then, we will get one popup menu, then select start job"
I gave a error like that
Error : "Cannot start this job because it doesn't not have any step"
You maybe fix this error !
Questionsqldmo errors Pin
anahita_m19-Aug-07 21:44
memberanahita_m19-Aug-07 21:44 
Generalit is not working in sql server 2005 Pin
manish ojha16-May-07 8:28
membermanish ojha16-May-07 8:28 
QuestionWill this work in Sql2005? Pin
AutolibSe4-Dec-06 10:01
memberAutolibSe4-Dec-06 10:01 
GeneralSql Server Pin
sathishtl00714-Nov-06 17:43
membersathishtl00714-Nov-06 17:43 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170713.1 | Last Updated 5 Oct 2006
Article Copyright 2006 by Joshy_geo
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid