Click here to Skip to main content
11,408,708 members (59,988 online)
Click here to Skip to main content

SQL Agent: A Job Scheduler Framework

, 8 May 2008 GPL3
Rate this:
Please Sign up or sign in to vote.
An easy-to-use .NET solution for Job Scheduling with pluggable actions that runs on a Windows Service. Back up your SQL Server Express and much more!


For my last project, I've been using the latest free version of SQL Server, called SQL Server Express Edition, available here. I immediately noticed that the database scheduler was removed since the previous version. I found it really interesting creating my own SQL Agent. In this article, I'll show you how to create a generic scheduler that runs on a Windows Service. The scheduler will be able to accept pluggable actions that we can develop, later implementing a custom interface.


This article is about Jobs Scheduling. You're required to have basic knowledge of how to run and install a Windows Service that uses Microsoft .NET Framework v2.0. A good knowledge of OOP, Interfaces and Reflection is also required in order to understand pluggable actions.

Install It!

The solution provided with this article is composed of four projects:

  • SQLAgent, a Windows Service that wraps the scheduler
  • SQLAgentUI, a Windows Application to control the service from the System Tray
  • EventScheduler, a Class Library containing all the classes composing the Scheduler and the forms to configure it
  • EventSchedulerAction, a Class Library of pluggable actions for the Scheduler jobs

You can install the service using the attached setup or, alternatively, using the command line as shown below:

SQLAgent.exe -i

Run It!

Start the execution of SQLAgentUI and press the button Run beside the label SQL Server Agent. If you do have problems running the system tray application, please go back to the service installation process. Press the button Configure SQL Server Agent to access the configuration page. The following page is shown:

You're now ready to configure your jobs!

Using the Code

The core library of the project is EventScheduler. In fact, the event scheduler allows us to schedule our jobs, configure them with a seamless interface and store the settings through an XML file. All these features are one step away from you. We only need to implement the IActionable interface defined by the library. The following is the implementation of the SQL Backup Job.

Imports Microsoft.SqlServer.Management
Imports EventScheduler
Public Class MSSQLBackup
Implements IActionable

#Region "Implements IActionable"
    Protected Shared DefaultParameters() As ActionParameter = _
        {New ActionParameter("Server", _
        ".\SQLExpress"), _
        New ActionParameter("Database", ""), _
        New ActionParameter("Backup Folder", _

    Public ReadOnly Property ActionParameters() As _
        EventScheduler.ActionParameter() _
        Implements IActionable.ActionParameters
            Return MSSQLBackup.DefaultParameters.Clone
        End Get
    End Property

    Public Sub ActionRun(ByVal s As EventScheduler.Schedule) _
        Implements IActionable.ActionRun
        Dim oParameters() As ActionParameter = s.GetActionParameters
        BackupDatabase(oParameters(0).Value, _
            oParameters(1).Value, _
    End Sub

    Public ReadOnly Property ActionText() As String _
        Implements IActionable.ActionText
            Return "SQL Server Backup"
        End Get
    End Property
#End Region

#Region "Private Methods"
    Protected Sub BackupDatabase(ByVal ServerName As String, _
        ByVal DatabaseName As String, _
        ByVal BackupFolder As String)
            Dim srv As New Smo.Server(ServerName)
            Dim bkp As New Smo.Backup
            bkp.Action = Smo.BackupActionType.Database
            bkp.Database = DatabaseName

            Dim filePath As String = BackupFolder
            filePath = System.IO.Path.Combine(BackupFolder, _
                DatabaseName & "-" & Now.Year() & _
                "-" & Now.Month.ToString("00") & _
                "-" & Now.Day.ToString("00") & _
                "-" & Now.Hour.ToString("00") & _
                "-" & Now.Minute.ToString("00") & _
                "-" & Now.Second.ToString("00") & _

            bkp.Devices.AddDevice(filePath, Smo.DeviceType.File)

            Catch ex As Exception
        End Try
    End Sub
#End Region

End Class

The following is a screenshot of the Jobs Scheduler settings page:

Please note that the Event Scheduler retrieves the data necessary to load the action list using the IActionable interface, as well as the parameters list. The action settings interface will look as follows:

The XML file will have the following aspect:

<?xml version="1.0"?>
    <ArrayOfSchedule xmlns:xsi=""
        <Schedule xsi:type="DailySchedule" Name="Daily Backup" 
            Active="false" Interval="0">
                    <Value xsi:type="xsd:string">.\SQLExpress
                    <Value xsi:type="xsd:string">MyDatabase</Value>
                    <Name>Backup Folder</Name>
                    <Value xsi:type="xsd:string">C:\Backup\</Value>

Configure It!

SQLAgent and SQLAgentUI are configured to run with the default SQL Server Express instance. Anyway, it's possible to configure it for named instances of SQL Server. The configuration mainly stands on two files:

  • SQLAgent.exe.config
  • SQLAgentUI.exe.config

Extend It!

The pluggable actions for the Event Scheduler actually define a framework for our Windows Services. This makes the application really extensible! I've included in this release:

  • SQL Server Database Backup using SMO
  • SQL Server Database Shrink using SMO
  • Execution of an SQL command
  • Send Email (when used as a PostAction, a [RESULTSET]tag can be included into the message text)
  • Send SMS using Esendex Web Service (when used as a PostAction, a [RESULTSET]tag can be included into the message text)

Please contribute to this project by sending your actions!!!

Final Notes

I hope that you find this article useful. If you find this article stupid, annoying or incorrect, express this fact by rating the article as you see fit. In the end, you're very welcome at any moment to freely contribute to this project by suggesting improvements, or by submitting code or other materials.


  • Thanks to Gonzalo Brusella for the reflection implementation
  • Thanks to Lukasz Swiatkowski for the cool glass button
  • Thanks to Nick Wälti for his beautiful gradient panel
  • Thanks to Sriram Chitturi for his article on events scheduling



  • 30th August, 2006 – First submission
  • 6th April 2007, – Project updated
  • 10th August, 2007 – Project updated
  • 4th September, 2007 - Project updated
  • 18th September, 2007 - Project updated
  • 6th May, 2008 - Project updated


This article, along with any associated source code and files, is licensed under The GNU General Public License (GPLv3)


About the Author

Danilo Corallo
Software Developer (Senior)
Italy Italy
I am 35 years old and I've been working with C++, Visual Basic .NET, C# and ASP.NET. I have a large experience in Industrial Automation solutions, but I've worked also as Web developer and DBA. I like to share knowledge and projects with other people.

Comments and Discussions

GeneralTool for scheduling sql-scripts, backups and more PinmemberDaoCode1-Apr-15 13:39 
QuestionSQL Server 2014 PinmemberMember 1148140525-Feb-15 23:27 
AnswerRe: SQL Server 2014 PinmemberDanilo Corallo22-Mar-15 12:11 
QuestionRe: SQL Server 2014 PinmemberDizzyX24-Mar-15 23:38 
AnswerRe: SQL Server 2014 PinmemberDaoCode1-Apr-15 13:41 
GeneralRe: SQL Server 2014 PinmemberDizzyX2-Apr-15 2:39 
Questionssrs PinmemberMember 1096497028-Jul-14 20:35 
GeneralMy vote of 5 PinmemberneofitDA14-Mar-14 7:45 
QuestionVS 2010 PinmemberAlberto Chavez12-Jun-13 7:41 
GeneralMy vote of 5 Pinmemberpradiprenushe27-Aug-12 0:31 
SuggestionRecommend Vale Software's Express Agent ($79) Pinmemberjroughgarden23-Apr-12 13:05 
QuestionEmail settings PinmemberLupdaloo4-Apr-12 22:16 
GeneralMy vote of 5 PinmemberOnAClearDiskYouCanSeekForever6-Dec-11 10:52 
Questionwhat is this " Dna2.Forms.ColorWithAlpha" PinmemberNadi3226-Nov-11 3:04 
AnswerRe: what is this " Dna2.Forms.ColorWithAlpha" PinmemberAman Thakur22-Mar-15 11:52 
GeneralRe: what is this " Dna2.Forms.ColorWithAlpha" PinmemberDanilo Corallo22-Mar-15 12:08 
GeneralRe: what is this " Dna2.Forms.ColorWithAlpha" PinmemberAman Thakur22-Mar-15 12:32 
GeneralRe: what is this " Dna2.Forms.ColorWithAlpha" PinmemberDanilo Corallo22-Mar-15 12:48 
GeneralRe: what is this " Dna2.Forms.ColorWithAlpha" PinmemberAman Thakur22-Mar-15 12:53 
QuestionAgent not working in another user. PinmemberSamRST15-Nov-11 3:59 
QuestionI can´t start the service ... PinmemberemmanuelJuarez8-Apr-11 14:42 
GeneralError: Backup Failed - Failed to connect to server Pinmemberkiki1322-Feb-11 7:40 
GeneralRe: Error: Backup Failed - Failed to connect to server PinmemberSteve Pritchard5-Apr-11 15:38 
GeneralThe .Net SqlClient Data Provider has received a severity 14, state 1 error number 18456; Login failed for user 'NT AUTHORITY\ANONYMOUS LOGON'. PinmemberMember 769222922-Feb-11 4:07 
Generalsource with no dna PinmemberMember 24400167-Feb-11 8:09 
Generalcannot open mssql$sqlexpress service on computer Pinmemberthishanc31-Aug-10 19:39 
GeneralRe: cannot open mssql$sqlexpress service on computer Pinmemberkevin hadfield17-Sep-12 11:17 
GeneralMy vote of 5 Pinmembersharadyaaaaa25-Jul-10 2:58 
GeneralSMTP SSL [modified] Pinmemberipi_35921-Jul-10 5:42 
GeneralCannot open MSSQL$SQLEXPRESS service on Computer PinmemberPhilip Horan11-Mar-10 3:05 
GeneralRe: Cannot open MSSQL$SQLEXPRESS service on Computer PinmemberPhilip Horan11-Mar-10 3:10 
GeneralRe: Cannot open MSSQL$SQLEXPRESS service on Computer Pinmemberkevin hadfield17-Sep-12 11:16 
GeneralSchedule daily backup for sql server 2005 datbase using sql agent Pinmemberkirancherupally8-Mar-10 19:20 
GeneralGot this working but only on a computer running Windows Authentication Pinmemberccastanedo23-Feb-10 16:06 
GeneralUnable to get the scheduled job run Pinmembervihsuna19-Feb-10 7:58 
GeneralMultiple Actions for a Schedule PinmemberLT30011-Jan-10 5:00 
GeneralDoesn't work under Vista 64x PinmemberKehagiem28-Dec-09 6:25 
GeneralDoesn't appear to work with SQL Server Express 2008 PinmemberKazMaxLtd25-Dec-09 13:23 
GeneralRe: Doesn't appear to work with SQL Server Express 2008 PinmemberDanilo Corallo25-Dec-09 13:47 
GeneralRe: Doesn't appear to work with SQL Server Express 2008 PinmemberKazMaxLtd25-Dec-09 15:35 
QuestionRe: Doesn't appear to work with SQL Server Express 2008 Pinmemberfierikit15-Jan-10 1:40 
AnswerRe: Doesn't appear to work with SQL Server Express 2008 PinmemberAsafMeir10-Feb-10 23:27 
GeneralFailing when connecting to db PinmemberLorenzo Consegni10-Dec-09 23:57 
GeneralRe: Failing when connecting to db PinmemberKikoz6811-Dec-09 17:07 
GeneralRe: Failing when connecting to db PinmemberLorenzo Consegni16-Dec-09 6:28 
QuestionHow to get results PinmemberNitin Sawant7-Dec-09 23:14 
AnswerRe: How to get results PinmemberDanilo Corallo11-Dec-09 23:04 
GeneralMy vote of 1 Pinmemberplume27-Aug-09 2:49 
RantRe: My vote of 1 PinmemberMember 27934304-Sep-09 14:01 
AnswerRe: My vote of 1 PinmemberSteve Pritchard29-Nov-09 7:11 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.150414.5 | Last Updated 8 May 2008
Article Copyright 2006 by Danilo Corallo
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid