Udostępnij za pośrednictwem


Planowanie automatycznych zadań administracyjnych w agenta programu SQL Server

W przypadku obiektów SMO SQL Server Agent jest reprezentowany przez następujące obiekty:

  • The JobServer object has three collections of jobs, alerts and operators.

  • The OperatorCollection object represents a list of pager, e-mail addresses and net send operators that can be notified of events automatically by the Microsoft SQL Server Agent.

  • The AlertCollection object represents a list of circumstances such as system events or performance conditions that are monitored by SQL Server.

  • The JobCollection object is slightly more complex.Reprezentuje listy multi-step zadań, które są uruchamiane w określonych harmonogramów.Kroki i informacji o harmonogramie są przechowywane w JobStep i JobSchedule obiekty.

The SQL Server Agent objects are in the Microsoft.SqlServer.Management.Smo.Agent namespace.

Przykłady

Aby używać dostarczonych przykładów kodu źródłowego, należy wybrać środowisko, szablon oraz język programowania, które będą używane do tworzenia aplikacji.Aby uzyskać więcej informacji zobacz Jak Tworzenie obiektów SMO projektu Visual Basic w programie Visual Studio .NET lub Jak Tworzenie projektu programu Visual C# obiekty SMO w programie Visual Studio .NET.

  1. Dla programów korzystających z SQL Server Agent, muszą zawierać Imports Instrukcja, aby móc skorzystać z obszaru nazw agenta. Wstawić instrukcję po drugim Imports instrukcje przed wszelkimi deklaracjami w aplikacji, takich jak:

Imports Microsoft.SqlServer.Management.Smo

Imports Microsoft.SqlServer.Management.Common

Imports Microsoft.SqlServer.Management.Smo.Agent

Tworzenie zadanie przy użyciu kroków i harmonogram w języku Visual Basic

Ten przykładowy kod tworzy zadanie z kroków i zgodnie z harmonogramem i informuje następnie operator.

Tworzenie zadanie przy użyciu kroków i harmonogram w środowisku Visual C#

Ten przykładowy kod tworzy zadanie z kroków i zgodnie z harmonogramem i informuje następnie operator.

//Connect to the local, default instance of SQL Server. 
{ 
Server srv = default(Server); 
srv = new Server(); 
//Define an Operator object variable by supplying the Agent (parent JobServer object) and the name in the constructor. 
Operator op = default(Operator); 
op = new Operator(srv.JobServer, "Test_Operator"); 
//Set the Net send address. 
op.NetSendAddress = "Network1_PC"; 
//Create the operator on the instance of SQL Server Agent. 
op.Create(); 
//Define a Job object variable by supplying the Agent and the name arguments in the constructor and setting properties. 
Job jb = default(Job); 
jb = new Job(srv.JobServer, "Test_Job"); 
//Specify which operator to inform and the completion action. 
jb.OperatorToNetSend = "Test_Operator"; 
jb.NetSendLevel = CompletionAction.Always; 
//Create the job on the instance of SQL Server Agent. 
jb.Create(); 
//Define a JobStep object variable by supplying the parent job and name arguments in the constructor. 
JobStep jbstp = default(JobStep); 
jbstp = new JobStep(jb, "Test_Job_Step"); 
jbstp.Command = "Test_StoredProc"; 
jbstp.OnSuccessAction = StepCompletionAction.QuitWithSuccess; 
jbstp.OnFailAction = StepCompletionAction.QuitWithFailure; 
//Create the job step on the instance of SQL Agent. 
jbstp.Create(); 
//Define a JobSchedule object variable by supplying the parent job and name arguments in the constructor. 
JobSchedule jbsch = default(JobSchedule); 
jbsch = new JobSchedule(jb, "Test_Job_Schedule"); 
//Set properties to define the schedule frequency, and duration. 
jbsch.FrequencyTypes = FrequencyTypes.Daily; 
jbsch.FrequencySubDayTypes = FrequencySubDayTypes.Minute; 
jbsch.FrequencySubDayInterval = 30; 
TimeSpan ts1 = default(TimeSpan); 
ts1 = new TimeSpan(9, 0, 0); 
jbsch.ActiveStartTimeOfDay = ts1; 
TimeSpan ts2 = default(TimeSpan); 
ts2 = new TimeSpan(17, 0, 0); 
jbsch.ActiveEndTimeOfDay = ts2; 
jbsch.FrequencyInterval = 1; 
System.DateTime d = default(System.DateTime); 
d = new System.DateTime(2003, 1, 1); 
jbsch.ActiveStartDate = d; 
//Create the job schedule on the instance of SQL Agent. 
jbsch.Create(); 
} 

Tworzenie alert w języku Visual Basic

Ten przykładowy kod tworzy alert, zostanie wywołany przez warunek wydajności.Warunek, należy podać w określonym formacie:

ObjectName|CounterName|Instance|ComparisionOp|CompValue

Operator jest wymagane w przypadku alert powiadomienia.The Operator type requires square parentheses because operator is a Visual Basic keyword.

Tworzenie alertu w środowisku Visual C#

Ten przykładowy kod tworzy alert, zostanie wywołany przez warunek wydajności.Warunek, należy podać w określonym formacie:

ObjectName|CounterName|Instance|ComparisionOp|CompValue

Operator jest wymagane w przypadku alert powiadomienia.The Operator type requires square parentheses because operator is a Visual C# keyword.

//Connect to the local, default instance of SQL Server. 
{ 
Server srv = default(Server); 
srv = new Server(); 
//Define an Alert object variable by supplying the SQL Server Agent and the name arguments in the constructor. 
Alert al = default(Alert); 
al = new Alert(srv.JobServer, "Test_Alert"); 
//Specify the performance condition string to define the alert. 
al.PerformanceCondition = "SQLServer:General Statistics|User Connections||>|3"; 
//Create the alert on the SQL Agent. 
al.Create(); 
//Define an Operator object variable by supplying the SQL Server Agent and the name arguments in the constructor. 
Operator op = default(Operator); 
op = new Operator(srv.JobServer, "Test_Operator"); 
//Set the net send address. 
op.NetSendAddress = "NetworkPC"; 
//Create the operator on the SQL Agent. 
op.Create(); 
//Run the AddNotification method to specify the operator is notified when the alert is raised. 
al.AddNotification("Test_Operator", NotifyMethods.NetSend); 
}

Umożliwienie dostępu użytkownika do podsystemu przy użyciu konto proxy w języku Visual Basic

W tym przykładzie kodu pokazano, jak umożliwić użytkownikowi dostęp do podsystemu określony za pomocą AddSubSystem(AgentSubSystem) Metoda ProxyAccount obiekt.

Umożliwia dostęp użytkownika do podsystemu przy użyciu konta serwera proxy w programie Visual C#

W tym przykładzie kodu pokazano, jak umożliwić użytkownikowi dostęp do podsystemu określony za pomocą AddSubSystem(AgentSubSystem) Metoda ProxyAccount obiekt.

//Connect to the local, default instance of SQL Server. 
{ 
Server srv = default(Server); 
srv = new Server(); 
//Declare a JobServer object variable and reference the SQL Server Agent. 
JobServer js = default(JobServer); 
js = srv.JobServer; 
//Define a Credential object variable by supplying the parent server and name arguments in the constructor. 
Credential c = default(Credential); 
c = new Credential(srv, "Proxy_accnt"); 
//Set the identity to a valid login represented by the vIdentity string variable. 
//The sub system will run under this login. 
c.Identity = vIdentity; 
//Create the credential on the instance of SQL Server. 
c.Create(); 
//Define a ProxyAccount object variable by supplying the SQL Server Agent, the name, the credential, the description arguments in the constructor. 
ProxyAccount pa = default(ProxyAccount); 
pa = new ProxyAccount(js, "Test_proxy", "Proxy_accnt", true, "Proxy account for users to run job steps in command shell."); 
//Create the proxy account on the SQL Agent. 
pa.Create(); 
//Add the login, represented by the vLogin string variable, to the proxy account. 
pa.AddLogin(vLogin); 
//Add the CmdExec subsytem to the proxy account. 
pa.AddSubSystem(AgentSubSystem.CmdExec); 
} 
//Now users logged on as vLogin can run CmdExec job steps with the specified credentials.