从asp.net mvc 4 Web应用程序运行SQL作业

时间:2021-02-03 03:31:09

I need some help in figuring out how to start a SQL Server Agent Job from an asp.net mvc web application. The Job needs to be started on the click of a button. What is the right way of doing it?

我需要一些帮助来确定如何从asp.net mvc Web应用程序启动SQL Server代理作业。只需单击按钮即可启动作业。这样做的正确方法是什么?

I just installed hangfire and I'm trying to configure it but I'm stuck with the below code. Am i on the right track?

我刚安装了hangfire,我正在尝试配置它,但我坚持使用下面的代码。我是在正确的轨道上吗?

Any leads would be very helpful. Thanks.

任何线索都会非常有帮助。谢谢。

public class MyController : Controller
{    
public void ExecJob(int Id)
    {
   BackgroundJob.Enqueue(() => UpdateDB(Id));  
    }

public void UpdateDB(Id)
    {
  //how to start the sql job
    }
}

1 个解决方案

#1


0  

You can execute a stored procedure from the web application on click of a button

您可以通过单击按钮从Web应用程序执行存储过程

Stored procedure:

存储过程:

    CREATE PROCEDURE RunJob
       @JobName NVARCHAR(100)
    AS
    BEGIN
       -- SET NOCOUNT ON added to prevent extra result sets from
       -- interfering with SELECT statements.
       SET NOCOUNT ON;

       EXEC msdb.dbo.sp_start_job @JobName
    END
    GO

Create this stored procedure in your database and pass the job name as a parameter or you can hardcode the job name in the stored procedure to prevent accidental execution of other SQL jobs.

在数据库中创建此存储过程并将作业名称作为参数传递,或者您可以在存储过程中对作业名称进行硬编码,以防止意外执行其他SQL作业。

#1


0  

You can execute a stored procedure from the web application on click of a button

您可以通过单击按钮从Web应用程序执行存储过程

Stored procedure:

存储过程:

    CREATE PROCEDURE RunJob
       @JobName NVARCHAR(100)
    AS
    BEGIN
       -- SET NOCOUNT ON added to prevent extra result sets from
       -- interfering with SELECT statements.
       SET NOCOUNT ON;

       EXEC msdb.dbo.sp_start_job @JobName
    END
    GO

Create this stored procedure in your database and pass the job name as a parameter or you can hardcode the job name in the stored procedure to prevent accidental execution of other SQL jobs.

在数据库中创建此存储过程并将作业名称作为参数传递,或者您可以在存储过程中对作业名称进行硬编码,以防止意外执行其他SQL作业。