简体   繁体   English

C# & ASP.NET MVC 5 - 从按钮单击执行存储过程,没有返回值

[英]C# & ASP.NET MVC 5 - execute stored procedure from button click with no return values

I'm trying (I'm in need to) to create small web application to manage some ETL processes giving my users few buttons to view SQL Server data and run few SSIS Packages.我正在尝试(我需要)创建小型 web 应用程序来管理一些 ETL 进程,为我的用户提供几个按钮来查看 SQL 服务器数据并运行几个 ZD941BD232618B67D2D8BA96F83CC20 包。

I was able to handle the website creation using a C# ASP.NET MVC CRUD tutorial HERE (really useful) and to show the data I need.我能够使用 C# ASP.NET MVC CRUD 教程HERE (非常有用)来处理网站创建并显示我需要的数据。

I then created a data model pointing to my tables and stored procedures and now I "only" need to create a basic page with a textbox to insert a parameter and a button for each stored procedure I need to run.然后我创建了一个数据 model 指向我的表和存储过程,现在我“只”需要创建一个带有文本框的基本页面,以便为我需要运行的每个存储过程插入一个参数和一个按钮。

Each stored procedure will run an SSIS package that doesn't need to return any value for now.每个存储过程将运行一个 SSIS package,现在不需要返回任何值。

EDIT: I was able to gather some more information and to modify the code like this编辑:我能够收集更多信息并像这样修改代码

Controller Controller

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Threading.Tasks;
using System.Net;
using System.Web;
using System.Web.Mvc;
using Italist_Admin.Models;
using System.Data.SqlClient;
using System.Data.Entity.Core.Objects;

namespace Project.Controllers
{
    public class ToolsController : Controller
    {
        private ProjectEntities db = new ProjectEntities();

        public ActionResult Index()
        {
            ProjectEntities entities = new ProjectEntities();
            //return View(entities.SPU_RUNSSIS(""));
            return View();

        }
        [HttpPost]

        public ActionResult ExecExportOnly(string txtPeriod)  // to get the Student Details  
        {
            ProjectEntities entities = new ProjectEntities();

            entities.SPU_RUNSSIS(parameter);
            return View();

        }}}

View看法

@{
    ViewBag.Title = "Tools";
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Export Only</title>
</head>
<body>
    <div>
        @using (Html.BeginForm("ExecExportOnly", "Tools", FormMethod.Post))
        {
            <span>Period:</span> @Html.TextBox("txtPeriod")
            <input type="submit" value="Run Export" />
        }
    </div>
</body>
</html>

Model Model

  public virtual int SPU_RUNSSIS(string parameter)
    {
        var periodParameter = period != null ?
            new ObjectParameter("parameter", parameter) :
            new ObjectParameter("parameter", typeof(string));

        ((IObjectContextAdapter)this).ObjectContext.CommandTimeout = 300;
        return ((IObjectContextAdapter)this).ObjectContext.ExecuteFunction("SPU_RUNSSIS", parameterParameter);

    }

I added a timeout in the model because on execution, after 30 seconds it was failing due to timeout.我在 model 中添加了超时,因为在执行时,30 秒后由于超时而失败。

Running this code, the packaged fails ( SqlException: The package failed. Check the SSIS catalog logs for more information ) after about 30 seconds anyway and AT THE END of the 30 seconds I see in the SQL Trace the following message运行此代码,打包失败( SqlException: The package failed. Check the SSIS catalog logs for more information )无论如何大约 30 秒后,在 30 秒的末尾我在 Z9778A2ZB0A0741CB2086 中看到跟踪消息

RPC:Completed - exec [dbo].[SPU_RUNSSIS] @parameter='parametervalue' RPC:已完成 - exec [dbo].[SPU_RUNSSIS] @parameter='parametervalue'

If I manually run the code above, it works.如果我手动运行上面的代码,它可以工作。

I'm almost there but it seems I can't find the correct way to trigger the execution of the stored procedure at some point.我快到了,但似乎在某些时候我找不到触发存储过程执行的正确方法。

Thanks in advance for any suggestion提前感谢您的任何建议

I managed to circumvent the problem by firing the SSIS directly from the c# code instead of executing a stored procedure that then triggers the SSIS execution.我设法通过直接从 c# 代码触发 SSIS 而不是执行然后触发 SSIS 执行的存储过程来规避该问题。

Maybe not the best solution, but it seems to work:也许不是最好的解决方案,但它似乎工作:

public ActionResult Index()
    {
        project entities = new project();
        return View();

    }
    public ActionResult ExecExportOnly(string txtPeriod)
    {
        project entities = new project();

        string targetServerName = ConfigurationManager.AppSettings["targetServerName"];
        string folderName = ConfigurationManager.AppSettings["folderName"];
        string projectName = ConfigurationManager.AppSettings["projectName"]; 
        string SSIS_ExportOnly = ConfigurationManager.AppSettings["SSIS_ExportOnly"];

        // Create a connection to the server
        string sqlConnectionString = ConfigurationManager.ConnectionStrings["Variable1"].ConnectionString;

        SqlConnection sqlConnection = new SqlConnection(sqlConnectionString);

        // Create the Integration Services object
        IntegrationServices integrationServices = new IntegrationServices(sqlConnection);

        // Get the Integration Services catalog
        Catalog catalog = integrationServices.Catalogs["SSISDB"];

        // Get the folder
        CatalogFolder folder = catalog.Folders[folderName];

        // Get the project
        ProjectInfo project = folder.Projects[projectName];

        // Get the package
        PackageInfo package = project.Packages[SSIS_ExportOnly];

        //Set Parameters
        // Add a parameter collection for 'system' parameters (ObjectType = 50), package parameters (ObjectType = 30) and project parameters (ObjectType = 20)
        Collection<PackageInfo.ExecutionValueParameterSet> executionParameter = new Collection<PackageInfo.ExecutionValueParameterSet>();

        // Add execution parameter (value) to override the default asynchronized execution. If you leave this out the package is executed asynchronized
        //executionParameter.Add(new PackageInfo.ExecutionValueParameterSet { ObjectType = 50, ParameterName = "SYNCHRONIZED", ParameterValue = 1 });

        // Add execution parameter (value) to override the default logging level (0=None, 1=Basic, 2=Performance, 3=Verbose)
        executionParameter.Add(new PackageInfo.ExecutionValueParameterSet { ObjectType = 50, ParameterName = "LOGGING_LEVEL", ParameterValue = 3 });

        // Add a project parameter (value) to fill a project parameter
        //executionParameter.Add(new PackageInfo.ExecutionValueParameterSet { ObjectType = 20, ParameterName = "MyProjectParameter", ParameterValue = "some value" });

        // Add a project package (value) to fill a package parameter
        executionParameter.Add(new PackageInfo.ExecutionValueParameterSet { ObjectType = 30, ParameterName = "ParamPeriod", ParameterValue = txtPeriod });

        // Get the identifier of the execution to get the log
        //long executionIdentifier = package.Execute(false, null, executionParameter);

        // Run the package
        package.Execute(true, null,executionParameter);

        return View("Index");
    }

Thanks anyways!不管怎么说,多谢拉! R R

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM