C# 调用sqlserver(作业) job

public object B2COutboundDownFile()
        {
            var obj = new object();
            try
            {
              var jobConnection = new SqlConnection(JobDBConString);
                ServerConnection conn = new ServerConnection(jobConnection); //Create SQL server conn, Windows Authentication
                Server server = new Server(conn); //Connect SQL Server
                var job = server.JobServer.Jobs["Sustainable_B2C_OutBound"];//Get the specified job
                var JobStatus = job.CurrentRunStatus.ToString();
                //var dataTime = job.LastRunDate.ToString();
                if (JobStatus == "Idle")
                {
                    obj = new
                    {
                        code = 0,
                        msg = "Run Start"
                    };
                    job.Start();
                }
                else
                {
                    //该job正在运行中
                    obj = new
                    {
                        code = 1,
                        msg = "The job is executing,Please try again later!"
                    };
                }
                jobConnection.Close();
            }
            catch (Exception ex)
            {
                obj = new
                {
                    code = 500,
                    msg = "Run failed"
                };
                log.Error("调用Sql Job" + ex);
            }
            return obj;
        }

 

posted @ 2021-04-20 17:00  看花人墨客  阅读(370)  评论(0编辑  收藏  举报