我远程连接SQL server 2005用SMo在SQL Server Agent的jobs下创建一个job,然后想对它的Category属性赋值,却一直没有成功,请指教。代码如下, private void BT_Click_Click(object sender, EventArgs e)
{
Server theServer = new Server("139.24.179.170"); this.TB_Content.Visible = true;
this.TB_Content.Text = theServer.Information.VersionString + "\n"; foreach (Database db in theServer.Databases)
{
this.TB_Content.Text += db.Name + " " + db.IsSystemObject.ToString() + "\n";
} Database myDB = theServer.Databases["TryInstall"];
this.TB_Content.Text += "Name: " + myDB.Name.ToString() + "\n";
this.TB_Content.Text += "Owner:" + myDB.Owner.ToString() + "\n"; Job myJob = new Job();
Job myJob1 = new Job(); if (!theServer.JobServer.Jobs.Contains("TestJob11"))
{
myJob = new Job(theServer.JobServer, "TestJob11");
}
else
{
myJob1 = theServer.JobServer.Jobs["TestJob"];
Random a = new Random();
myJob = new Job(theServer.JobServer, "myJob" + a.Next().ToString());
} myJob.Description = "Only for test";
myJob.IsEnabled = true;
myJob .OwnerLoginName ="admin"; if (theServer.JobServer.JobCategories.Contains("Database Maintenance") == true)
{
myJob.Category = "Database Maintenance";
MessageBox.Show("Database Maintenance contains "+myJob .Category .ToString ());
} JobCategory[] myJobCategory = new JobCategory[10]; theServer.JobServer.JobCategories.CopyTo(myJobCategory, 0);
MessageBox.Show(myJob.Category.ToString()); if (myJob.State.ToString() != "Existing")
{
this.TB_Content.Text += myJob.State.ToString() + " ";
myJob.Create();
}
this.TB_Content.Text += myJob.Name.ToString() + " " + myJob.State.ToString();
}谢谢
{
Server theServer = new Server("139.24.179.170"); this.TB_Content.Visible = true;
this.TB_Content.Text = theServer.Information.VersionString + "\n"; foreach (Database db in theServer.Databases)
{
this.TB_Content.Text += db.Name + " " + db.IsSystemObject.ToString() + "\n";
} Database myDB = theServer.Databases["TryInstall"];
this.TB_Content.Text += "Name: " + myDB.Name.ToString() + "\n";
this.TB_Content.Text += "Owner:" + myDB.Owner.ToString() + "\n"; Job myJob = new Job();
Job myJob1 = new Job(); if (!theServer.JobServer.Jobs.Contains("TestJob11"))
{
myJob = new Job(theServer.JobServer, "TestJob11");
}
else
{
myJob1 = theServer.JobServer.Jobs["TestJob"];
Random a = new Random();
myJob = new Job(theServer.JobServer, "myJob" + a.Next().ToString());
} myJob.Description = "Only for test";
myJob.IsEnabled = true;
myJob .OwnerLoginName ="admin"; if (theServer.JobServer.JobCategories.Contains("Database Maintenance") == true)
{
myJob.Category = "Database Maintenance";
MessageBox.Show("Database Maintenance contains "+myJob .Category .ToString ());
} JobCategory[] myJobCategory = new JobCategory[10]; theServer.JobServer.JobCategories.CopyTo(myJobCategory, 0);
MessageBox.Show(myJob.Category.ToString()); if (myJob.State.ToString() != "Existing")
{
this.TB_Content.Text += myJob.State.ToString() + " ";
myJob.Create();
}
this.TB_Content.Text += myJob.Name.ToString() + " " + myJob.State.ToString();
}谢谢
/// 创建包含步骤和计划的作业
/// </summary>
/// <param name="databaseName">数据库名</param>
/// <param name="jobName">作业名称</param>
/// <param name="description">作业说明</param>
/// <param name="strSQl">SQL命令</param>
/// <param name="startDate">开始日期,为null表示当前时间</param>
/// <param name="endDate">截止日期,为null表示无截止日期</param>
public Guid CreateJob(string databaseName, string jobName, string description,string strSQl,DateTime startDate,DateTime endDate)
{
//JobServer 对象具有作业、警报和操作员三个集合。
//OperatorCollection 对象表示可由 Microsoft SQL Server 代理自动向其发送事件通知的寻呼程序、电子邮件地址和 net send 操作员列表。
//AlertCollection 对象表示由 SQL Server 监视的系统事件或性能条件等情况的列表。
//JobCollection 对象稍微复杂一些。它表示按指定计划运行的多步骤任务列表。步骤和计划信息存储在 JobStep 和 JobSchedule 对象中。
////创建SQL代理服务操作员用户
//Operator op = default(Operator);
//op = new Operator(srv.JobServer, "Test_Operator");
//op.NetSendAddress = "Network1_PC";
//op.Create();
#region 创建作业 Job jb = new Job(sqlServer.JobServer, jobName);
jb.Description = description;
//Specify which operator to inform and the completion action.
//jb.OperatorToNetSend = "Test_Operator";
//jb.NetSendLevel = CompletionAction.Always;
jb.Create();
#endregion 创建作业
#region 作业步骤 JobStep jbstp = new JobStep(jb, "作业步骤");
//数据库
jbstp.DatabaseName = databaseName;
//计划执行的SQL命令
jbstp.Command = strSQl;
//成功时执行的操作
jbstp.OnSuccessAction = StepCompletionAction.QuitWithSuccess;
//失败时执行的操作
jbstp.OnFailAction = StepCompletionAction.QuitWithFailure;
//创建 SQL 代理实例的作业步骤.
jbstp.Create(); #endregion 作业步骤
#region 作业计划属性 JobSchedule jbsch = new JobSchedule(jb, "作业计划"); //计划频率,每几天一次
jbsch.FrequencyTypes = FrequencyTypes.Daily;
jbsch.FrequencyInterval = 1;
//每天一次,指定时间运行
jbsch.FrequencySubDayTypes = FrequencySubDayTypes.Once;
jbsch.ActiveStartTimeOfDay = new TimeSpan(12, 0, 0);
//每天多次,每隔指定时间一次
//jbsch.FrequencySubDayTypes = FrequencySubDayTypes.Hour;
//jbsch.FrequencySubDayInterval = 1;
//jbsch.ActiveStartTimeOfDay = new TimeSpan(0, 0, 0);
//jbsch.ActiveEndTimeOfDay = new TimeSpan(23, 59, 59); //持续时间
if (startDate != null)
{ //开始时间
jbsch.ActiveStartDate = startDate;
}
else
{
jbsch.ActiveStartDate = DateTime.Now;
}
if (endDate != null)
{ //结束时间,null表示无结束日期
jbsch.ActiveEndDate = endDate;
}
//创建SQL代理实例的作业调度
jbsch.Create();
#endregion 作业计划属性
//返回作业GUID
return jb.JobID;
}