在MVC中执行ssis包

the*_*heJ 5 ssis asp.net-core-mvc .net-core asp.net-core

下午,

是否可以使用 MVC 执行 SSIS 包?我正在创建的是一个 Web 应用程序,它将有一个按钮,一旦单击 SSIS 包就会运行。

SSIS 项目已设置并部署在 MSSQL 服务器上。

bil*_*nkc 5

SQL Server 2012+ 提供了一种出色的机制,用于通过集成服务目录 SSISDB 管理包及其执行。

以下代码提供了从位于 Demo 文件夹下的 MyProjectName SSIS 项目运行包 (Package2.dtsx) 的示例,并将 IntensityLevel 包参数调至 11。

DECLARE @execution_id bigint;

EXEC SSISDB.catalog.create_execution
    @package_name = N'Package2.dtsx'
,   @execution_id = @execution_id OUTPUT
,   @folder_name = N'Demo'
,   @project_name = N'MyProjectName'
,   @use32bitruntime = False
,   @reference_id = NULL;

DECLARE @var0 int = 11;

EXEC SSISDB.catalog.set_execution_parameter_value
    @execution_id
,   @object_type = 30
,   @parameter_name = N'IntensityLevel'
,   @parameter_value = @var0;

DECLARE @var1 smallint = 1;

EXEC SSISDB.catalog.set_execution_parameter_value
    @execution_id
,   @object_type = 50
,   @parameter_name = N'LOGGING_LEVEL'
,   @parameter_value = @var1;

EXEC SSISDB.catalog.start_execution
    @execution_id;
Run Code Online (Sandbox Code Playgroud)

获取如何构建上述 SQL 的示例的简单方法是打开 SQL Server Management Studio (SSMS) 并配置 SSIS 包的运行。导航到 Integration Services Catalog 并找到要运行的包。右键单击并选择执行...

执行包

配置菜单打开并找到您要指定的参数。提供示例值,但不要单击“确定”。相反,单击该脚本按钮并将脚本指定到新窗口(或剪贴板)

配置执行

现在您拥有 SSMS 为运行您的包而发出的确切命令。获取该代码,使用您选择的参数化方法让您的 MVC 程序为您的参数存根正确的运行时值,然后将所有 TSQL 包装在一个简单的数据库调用中(ole、ado、odbc 都没关系)


the*_*heJ 1

我创建了一个运行 SSIS 包的存储过程。在 MVC 中,它调用存储过程 -

[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Index()
{
    //code that updates DB

    #region Run Stored Procedure
    //connect to the SQL server
    var connection = new SqlConnection(_configuration.GetConnectionString("DatabaseConnection"));
    //command that runs procedure on the SQL server
    var command = new SqlCommand("RebuildSelection", connection)
    {
        CommandType = CommandType.StoredProcedure,
        CommandText = "RebuildSelection"
    };
    //get text from stored procedure to show success/error messages
    SqlParameter text = new SqlParameter("@Text", SqlDbType.NVarChar, 1000)
    {
        //output as its displayed to the user
        Direction = ParameterDirection.Output
    };
    //add the params
    command.Parameters.Add(text);
    connection.Open();
    //run query
    command.ExecuteNonQuery();
    //used to return success/error messages to user
    ViewData["Message"] = text.Value;
    connection.Close();
    #endregion

    return View();
}
Run Code Online (Sandbox Code Playgroud)

链接对于创建该过程有很大帮​​助。

有助于向用户返回消息。