Skip to main content
Email Password   helpLost your password?

Introduction

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.

Background

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:

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", _
        "C:\Backup\")}

    Public ReadOnly Property ActionParameters() As _
        EventScheduler.ActionParameter() _
        Implements IActionable.ActionParameters
        Get
            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, _
            oParameters(2).Value)
    End Sub

    Public ReadOnly Property ActionText() As String _
        Implements IActionable.ActionText
        Get
            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)
        Try
            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") & _
                ".bak")

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

            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="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:xsd="http://www.w3.org/2001/XMLSchema">
        <Schedule xsi:type="DailySchedule" Name="Daily Backup" 
            Active="false" Interval="0">
            <StartTime>2006-08-30T12:45:30.15625+02:00</StartTime>
            <NextInvokeTime>2006-08-31T12:45:30.15625+02:00
            </NextInvokeTime>
            <LastInvokeTime>2006-08-30T12:45:40.84375+02:00
            </LastInvokeTime>
            <Action>0</Action>
            <ActionParameters>
                <ActionParameter>
                    <Name>Server</Name>
                    <Value xsi:type="xsd:string">.\SQLExpress
                    </Value>
                    <Type>NormalActionParameter</Type>
                </ActionParameter>
                <ActionParameter>
                    <Name>Database</Name>
                    <Value xsi:type="xsd:string">MyDatabase</Value>
                    <Type>NormalActionParameter</Type>
                </ActionParameter>
                <ActionParameter>
                    <Name>Backup Folder</Name>
                    <Value xsi:type="xsd:string">C:\Backup\</Value>
                    <Type>NormalActionParameter</Type>
                </ActionParameter>
            </ActionParameters>
            <WeekDays>
                <boolean>false</boolean>
                <boolean>true</boolean>
                <boolean>true</boolean>
                <boolean>true</boolean>
                <boolean>true</boolean>
                <boolean>true</boolean>
                <boolean>false</boolean>
            </WeekDays>
            <BeginValidTime>2006-08-30T00:00:00+02:00</BeginValidTime>
            <EndValidTime>2006-08-30T00:00:00+02:00</EndValidTime>
        </Schedule>
    </ArrayOfSchedule>

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:

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:

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.

Credits

References

History

You must Sign In to use this message board.
 
 
Per page   
 FirstPrevNext
GeneralMy vote of 1 Pin
plume
2:49 27 Aug '09  
RantRe: My vote of 1 Pin
Member 2793430
14:01 4 Sep '09  
GeneralErrors about Dna2 somthing Pin
Ali S Syed
4:38 19 Jun '09  
GeneralDoes anyone have this working on Windows Vista? Pin
rrieman
11:40 2 Jun '09  
GeneralJob Scheduler in EZManage SQL Pin
itayl
4:14 27 May '09  
RantWhy VB if you have C# experiance? Pin
Member 2686413
10:14 30 Apr '09  
GeneralOnRunPostActionComplete not assigned? Pin
paulwoll
16:06 24 Apr '09  
GeneralGot it working Pin
George Carvill
9:34 26 Feb '09  
QuestionNothing happening Pin
George Carvill
8:04 26 Feb '09  
GeneralSchedule never fires? Pin
Dave Dunbar
12:18 1 Jan '09  
QuestionSqlserver express 2008 Pin
Member 2978029
6:42 10 Dec '08  
GeneralLast Run and Next Run does not get updated Pin
kemal_eg
4:49 31 Oct '08  
GeneralDocumentation Note Pin
bubk
10:58 2 Oct '08  
GeneralService pauses when logoff Pin
Rui Frazao
7:08 23 Sep '08  
GeneralRe: Service pauses when logoff Pin
kemal_eg
6:36 29 Oct '08  
GeneralRe: Service pauses when logoff Pin
Rui Frazao
3:32 31 Oct '08  
GeneralRe: Service pauses when logoff Pin
trueMoRoZ
21:38 20 Oct '09  
GeneralHi! Need help in catching errors by C# code on Job failure.. Pin
Member 3115760
21:39 22 Sep '08  
QuestionWin2k8 Issue Pin
shadow43701
6:52 18 Sep '08  
AnswerRe: Win2k8 Issue Pin
Danilo Corallo
22:17 22 Sep '08  
Generaldna2.forms?? Pin
paulwoll
10:49 10 Sep '08  
GeneralRe: dna2.forms?? Pin
Danilo Corallo
21:36 10 Sep '08  
GeneralRestore capability Pin
jj8558
9:42 3 Sep '08  
GeneralProject Build Side Note Pin
Sluggish
6:59 16 Aug '08  
GeneralHow to Backup to a network share Pin
Member 2373983
22:18 26 May '08  


Last Updated 8 May 2008 | Advertise | Privacy | Terms of Use | Copyright © CodeProject, 1999-2009