[引用]SQL Server 2005 Books Online How to: Create a Job with Steps and a Schedule in Visual Basic .NET

Creating a job with steps and a scheduleCreating a job with steps and a schedule
  1. Start Visual Studio 2005.

  2. From the File menu, select New Project. The New Project dialog box appears.

  3. In the Project Types pane, select Visual Basic. In the Templates pane, select Console Application.

  4. (Optional) In the Name box, type the name of the new application.

  5. Click OK to load the Visual Basic console application template.

  6. 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.

  7. 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 =   New   Date ( 2003 1 1 )
    43 jbsch.ActiveStartDate  =  d
    44 ' Create the job schedule on the instance of SQL Server Agent.
    45 jbsch.Create()

     

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值