[引用]SQL Server 2005 Books Online How to: Create a Job with Steps and a Schedule in Visual Basic .NET
-
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.AgentOn 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.
2Dim srv As Server
3srv = New Server
4'Define an Operator object variable by supplying the Agent (parent JobServer object) and the name in the constructor.
5Dim op As [Operator]
6op = New [Operator](srv.JobServer, "Test_Operator")
7'Set the Net send address.
8op.NetSendAddress = "Network1_PC"
9'Create the operator on the instance of SQL Server Agent.
10op.Create()
11'Define a Job object variable by supplying the Agent and the name arguments in the constructor and setting properties.
12Dim jb As Job
13jb = New Job(srv.JobServer, "Test_Job")
14'Specify which operator to inform and the completion action.
15jb.OperatorToNetSend = "Test_Operator"
16jb.NetSendLevel = CompletionAction.Always
17'Create the job on the instance of SQL Server Agent.
18jb.Create()
19'Define a JobStep object variable by supplying the parent job and name arguments in the constructor.
20Dim jbstp As JobStep
21jbstp = New JobStep(jb, "Test_Job_Step")
22jbstp.Command = "Test_StoredProc"
23jbstp.OnSuccessAction = StepCompletionAction.QuitWithSuccess
24jbstp.OnFailAction = StepCompletionAction.QuitWithFailure
25'Create the job step on the instance of SQL Server Agent.
26jbstp.Create()
27'Define a JobSchedule object variable by supplying the parent job and name arguments in the constructor.
28Dim jbsch As JobSchedule
29jbsch = New JobSchedule(jb, "Test_Job_Schedule")
30'Set properties to define the schedule frequency and duration.
31jbsch.FrequencyTypes = FrequencyTypes.Daily
32jbsch.FrequencySubDayTypes = FrequencySubDayTypes.Minute
33jbsch.FrequencySubDayInterval = 30
34Dim ts1 As TimeSpan
35ts1 = New TimeSpan(9, 0, 0)
36jbsch.ActiveStartTimeOfDay = ts1
37Dim ts2 As TimeSpan
38ts2 = New TimeSpan(17, 0, 0)
39jbsch.ActiveEndTimeOfDay = ts2
40jbsch.FrequencyInterval = 1
41Dim d As Date
42d = New Date(2003, 1, 1)
43jbsch.ActiveStartDate = d
44'Create the job schedule on the instance of SQL Server Agent.
45jbsch.Create()
posted on 2006-12-14 19:23 freeliver54 阅读(958) 评论(7) 编辑 收藏 举报