Option Explicit On
Option Strict On
Imports System.ServiceProcess
Imports System.Xml
Imports System.Text
Imports System.IO
Imports System.Diagnostics
Imports System.Data
Imports System.Web.Mail
Imports Microsoft.VisualBasic.ControlChars
Imports System.Timers
Imports System.Data.SqlClient
Public Class Service1
Inherits System.ServiceProcess.ServiceBase
#Region " Component Designer generated code "
Public Sub New()
MyBase.New()
' This call is required by the Component Designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call
End Sub
'UserService overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
' The main entry point for the process
<MTAThread()> _
Shared Sub Main()
Dim ServicesToRun() As System.ServiceProcess.ServiceBase
' More than one NT Service may run within the same process. To add
' another service to this process, change the following line to
' create a second service object. For example,
'
' ServicesToRun = New System.ServiceProcess.ServiceBase () {New Service1, New MySecondUserService}
'
ServicesToRun = New System.ServiceProcess.ServiceBase() {New Service1}
System.ServiceProcess.ServiceBase.Run(ServicesToRun)
End Sub
'Required by the Component Designer
Private components As System.ComponentModel.IContainer
' NOTE: The following procedure is required by the Component Designer
' It can be modified using the Component Designer.
' Do not modify it using the code editor.
Friend WithEvents Timer1 As System.Timers.Timer
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.Timer1 = New System.Timers.Timer
CType(Me.Timer1, System.ComponentModel.ISupportInitialize).BeginInit()
'
'Timer1
'
Me.Timer1.Enabled = True
Me.Timer1.Interval = 10000
'
'Service1
'
Me.ServiceName = "Service1"
CType(Me.Timer1, System.ComponentModel.ISupportInitialize).EndInit()
End Sub
#End Region
Protected Overrides Sub OnStart(ByVal args() As String)
' Add code here to start your service. This method should set things
' in motion so your service can do its work.
Timer1.Enabled = True
End Sub
Protected Overrides Sub OnStop()
' Add code here to perform any tear-down necessary to stop your service.
Timer1.Enabled = False
End Sub
Private Sub Timer1_Elapsed(ByVal sender As System.Object, ByVal e As System.Timers.ElapsedEventArgs) Handles Timer1.Elapsed
Working()
End Sub
Private Sub Working()
' Local variables
Dim sFrom As String = String.Empty
Dim sTo As String = String.Empty
Dim sCc As String = String.Empty
Dim sBcc As String = String.Empty
Dim sSubject As String = String.Empty
Dim sBody As String = String.Empty
Dim nSleep As Integer = 15
Dim nPause As Integer = 1000
'Create a var. named rightNow and set it to the current date/time
Dim rightNow As DateTime = DateTime.Now
Dim weekValue As String 'create a string
weekValue = rightNow.ToString("dd/MM/yy")
'Database
Dim testvariable As String = String.Empty
Dim Conn As SqlConnection
'for basic querying like SELECT
Dim Cmd As SqlDataAdapter
'for other types of query like INSERT, UPDATE and DELETE...
Dim Cmd2 As SqlCommand
Dim myquery As String = "SELECT * FROM Roles, Employees CROSS JOIN Rota WHERE (Rota.EmergencyManager = Employees.CAI) AND Week = '" + weekValue + "' AND (Roles.Role = Employees.Role) OR (Rota.SiteContact = Employees.CAI) AND Week = '" + weekValue + "' AND (Roles.Role = Employees.Role) OR (Rota.Logger = Employees.CAI) AND Week = '" + weekValue + "' AND (Roles.Role = Employees.Role) OR (Rota.Logistics = Employees.CAI) AND Week = '" + weekValue + "' AND (Roles.Role = Employees.Role) OR (Rota.Agency = Employees.CAI) AND Week = '" + weekValue + "' AND (Roles.Role = Employees.Role) OR (Rota.POB = Employees.CAI) AND Week = '" + weekValue + "' AND (Roles.Role = Employees.Role) OR (Rota.HR = Employees.CAI) AND Week = '" + weekValue + "' AND (Roles.Role = Employees.Role) OR (Rota.PGA = Employees.CAI) AND Week = '" + weekValue + "' AND (Roles.Role = Employees.Role)"
Conn = New SqlConnection("server=abentgapw7; trusted_connection=true; database=ERR")
Cmd = New SqlDataAdapter(myquery, Conn)
' Create a new DataSet
Dim ds As DataSet = New DataSet("theEmployee")
' Call the Fill method to load the DataSet
Cmd.Fill(ds, "theEmployee")
' Add DataBinding to variable
testvariable = ds.Tables("theEmployee").Rows(0)("FirstName").ToString() + " " + ds.Tables("theEmployee").Rows(0)("LastName").ToString()
' Enter the infinite loop
Try
' First, sleep for a while
'mWorker.Sleep(nSleep * 60 * 1000)
'mWorker.Sleep(2000)
' Woke up
'LogInfo("Service woke up")
Dim i As Integer = 0
' Prepare e-mail fields
Dim oMailMsg As MailMessage = New MailMessage
oMailMsg.From = sFrom
oMailMsg.To = sTo
oMailMsg.Cc = sCc
' Call a stored procedure to process the current item
' The success message
oMailMsg.Subject = sSubject + "Emergency Response Rota"
oMailMsg.BodyFormat = MailFormat.Html
oMailMsg.Body = sBody + "<HTML><BODY>Dear All,<BR><BR>The Rota for the week beginning the " + weekValue + " is as follows: <br>" + testvariable + "</BODY></HTML>"
SmtpMail.SmtpServer = "mailhost.wfc.domain.net"
' Send the message
If Not (oMailMsg.To = String.Empty) Then
SmtpMail.Send(oMailMsg)
End If
' Pause to avoid hogging the CPU
'mWorker.Sleep(nPause)
Catch obug As Exception
'LogEvent(obug.Message)
Finally
End Try
End Sub
End Class