//有关SqlJob的信息在database(msdb)内查询。
select j.job_id, j.name, j.enabled, jh.run_status, js.last_outcome_message, jh.run_date, jh.step_name, jh.run_time from sysjobs j left join sysjobhistory jh on (j.job_id = jh.job_id) left join sysjobservers js on (j.job_id = js.job_id)
通过C# 调用Sql Job.
class SML_SQLJob { public static string connectionString = "Data Source=DBPABC07;Initial Catalog=master;Integrated Security=True"; static String strSQL= "msdb.dbo.sp_start_job"; protected bool runJob(String _strJobName) { bool bResult = false; using (SqlConnection OrConn = new SqlConnection(connectionString)) { if (OrConn.State == ConnectionState.Closed) OrConn.Open(); using (SqlCommand cmd = new SqlCommand(strSQL, OrConn)) { SqlDataReader sdr = null; cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.Add("@JOB_NAME", SqlDbType.NVarChar, 128).Value = _strJobName; SqlParameter rc = new SqlParameter("rc", System.Data.SqlDbType.Int); rc.Direction = ParameterDirection.ReturnValue; cmd.Parameters.Add(rc); cmd.ExecuteNonQuery(); if ((int)rc.Value == 0) { bResult = true; } else { bResult = false; } } } return bResult; } static void Main(string[] args) { SML_SQLJob SML_SQLJob = new SML_SQLJob(); bool bResult = false; try { bResult = SML_SQLJob.runJob("TestC"); Console.WriteLine( "Job execute "+ bResult.ToString()); } catch (Exception ex) { Console.WriteLine("Failed to start the job :" + ex.Message); } finally { Console.WriteLine("successful " ); } Console.ReadKey(); } } }
此范例只是启动Sql Job, 并不用等待job执行完成。