zoukankan      html  css  js  c++  java
  • 查询Sql Server Agent 的job的执行情况。

    //有关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执行完成。

  • 相关阅读:
    Configure文件学习
    实用文章:常用开源协议详细解析
    openwrt的sysupgrade和factory固件的区别
    Linux块设备和字符设备
    eclipse代码补全按键修改成Tab
    Hadoop环境搭载
    比特币中难度调整
    共识机制
    比特币交易本质--UTXO(Unspent Transaction Output)
    多重签名
  • 原文地址:https://www.cnblogs.com/sxypeace/p/4871767.html
Copyright © 2011-2022 走看看