-
Start Visual Studio 2005.
-
From the File menu, select New Project. The New Project dialog box appears.
-
In the Project Types pane, select Visual Basic. In the Templates pane, select Console Application.
-
(Optional) In the Name box, type the name of the new application.
-
Click OK to load the Visual Basic console application template.
-
On the Project menu, select Add Reference item. The Add Reference dialog box appears. Select Browse and locate the SMO assemblies in the C:/Program Files/Microsoft SQL Server/90/SDK/Assemblies folder. Select the following files:
Imports Microsoft.SqlServer.Management.SMO
Imports Microsoft.SqlServer.Management.Common
Imports Microsoft.SqlServer.Management.SMO.Agent
On the View menu, click Code.-Or-Select the Form1.vb window to display the code window.
-
In the code, before any declarations, type the following Imports statements to qualify the types in the SMO namespace:
1 ' Connect to the local, default instance of SQL Server.
2 Dim srv As Server
3 srv = New Server
4 ' Define an Operator object variable by supplying the Agent (parent JobServer object) and the name in the constructor.
5 Dim op As [Operator]
6 op = New [Operator](srv.JobServer, " Test_Operator " )
7 ' Set the Net send address.
8 op.NetSendAddress = " Network1_PC "
9 ' Create the operator on the instance of SQL Server Agent.
10 op.Create()
11 ' Define a Job object variable by supplying the Agent and the name arguments in the constructor and setting properties.
12 Dim jb As Job
13 jb = New Job(srv.JobServer, " Test_Job " )
14 ' Specify which operator to inform and the completion action.
15 jb.OperatorToNetSend = " Test_Operator "
16 jb.NetSendLevel = CompletionAction.Always
17 ' Create the job on the instance of SQL Server Agent.
18 jb.Create()
19 ' Define a JobStep object variable by supplying the parent job and name arguments in the constructor.
20 Dim jbstp As JobStep
21 jbstp = New JobStep(jb, " Test_Job_Step " )
22 jbstp.Command = " Test_StoredProc "
23 jbstp.OnSuccessAction = StepCompletionAction.QuitWithSuccess
24 jbstp.OnFailAction = StepCompletionAction.QuitWithFailure
25 ' Create the job step on the instance of SQL Server Agent.
26 jbstp.Create()
27 ' Define a JobSchedule object variable by supplying the parent job and name arguments in the constructor.
28 Dim jbsch As JobSchedule
29 jbsch = New JobSchedule(jb, " Test_Job_Schedule " )
30 ' Set properties to define the schedule frequency and duration.
31 jbsch.FrequencyTypes = FrequencyTypes.Daily
32 jbsch.FrequencySubDayTypes = FrequencySubDayTypes.Minute
33 jbsch.FrequencySubDayInterval = 30
34 Dim ts1 As TimeSpan
35 ts1 = New TimeSpan( 9 , 0 , 0 )
36 jbsch.ActiveStartTimeOfDay = ts1
37 Dim ts2 As TimeSpan
38 ts2 = New TimeSpan( 17 , 0 , 0 )
39 jbsch.ActiveEndTimeOfDay = ts2
40 jbsch.FrequencyInterval = 1
41 Dim d As Date
42 d = New Date ( 2003 , 1 , 1 )
43 jbsch.ActiveStartDate = d
44 ' Create the job schedule on the instance of SQL Server Agent.
45 jbsch.Create()
[引用]SQL Server 2005 Books Online How to: Create a Job with Steps and a Schedule in Visual Basic .NET
最新推荐文章于 2016-01-01 20:31:13 发布
Creating a job with steps and a schedule