【问题标题】:C# & ASP.NET MVC 5 - execute stored procedure from button click with no return valuesC# & ASP.NET MVC 5 - 从按钮单击执行存储过程,没有返回值
【发布时间】:2020-07-30 05:59:41
【问题描述】:

我正在尝试(我需要)创建小型 Web 应用程序来管理一些 ETL 流程,为我的用户提供几个按钮来查看 SQL Server 数据并运行几个 SSIS 包。

我能够使用 C# ASP.NET MVC CRUD 教程 HERE(非常有用)来处理网站创建并显示我需要的数据。

然后我创建了一个指向我的表和存储过程的数据模型,现在我“只”需要创建一个带有文本框的基本页面,以便为我需要运行的每个存储过程插入一个参数和一个按钮。

每个存储过程都将运行一个 SSIS 包,目前不需要返回任何值。

编辑:我能够收集更多信息并像这样修改代码

控制器

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();

        }}}

查看

@{
    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>

型号

  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);

    }

我在模型中添加了超时,因为在执行时,30 秒后它由于超时而失败。

运行此代码,打包失败(SqlException:包失败。请检查 SSIS 目录日志以获取更多信息)大约 30 秒后,在我看到的 30 秒结束时SQL 跟踪以下消息

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

如果我手动运行上面的代码,它可以工作。

我快到了,但似乎在某个时候找不到触发存储过程执行的正确方法。

提前感谢您的任何建议

【问题讨论】:

  • 根据错误信息,似乎包确实执行了:SqlException: The package failed。查看 SSIS 目录日志以获取更多信息。该执行的 ssis 目录报告中有什么内容?
  • 没什么,其实ssis包没有运行。如果我手动执行相同的 SQL 代码,它会正确启动。
  • 我无法关注你。你说运行包失败的代码,但是在你的 cmets 中你说它没有运行。说明包失败的错误消息来自哪里?
  • 好吧,这确实很奇怪。消息从 vs 调试器返回,但 ssis 甚至没有启动。
  • 也许检查连接详细信息并确保它在您希望它运行的服务器上运行

标签: c# entity-framework stored-procedures ssis asp.net-mvc-5


【解决方案1】:

我设法通过直接从 c# 代码触发 SSIS 而不是执行随后触发 SSIS 执行的存储过程来规避该问题。

也许不是最好的解决方案,但它似乎有效:

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");
    }

无论如何,谢谢! 回复

【讨论】:

  • 经过这么多测试,我发现我陷入了客户端 - SQL Server - 文件服务器(我使用 SSIS 导出文件)之间的双跳问题。我使用本教程暂时绕过了这个问题 (nikolar.com/2015/03/10/…) 我需要在销毁所有内容之前加深 Kerberos 部分!
猜你喜欢
  • 2022-01-16
  • 1970-01-01
  • 1970-01-01
  • 2012-03-31
  • 1970-01-01
  • 1970-01-01
  • 2016-01-11
相关资源
最近更新 更多