C# & ASP.NET MVC 5 - 从没有 return 值的按钮点击执行存储过程
C# & ASP.NET MVC 5 - execute stored procedure from button click with no return values
我正在尝试(我需要)创建小型 Web 应用程序来管理一些 ETL 过程,让我的用户只需几个按钮即可查看 SQL 服务器数据和 运行 几个 SSIS 包.
我能够使用 C# ASP.NET MVC CRUD 教程 HERE(非常有用)处理网站创建并显示我需要的数据。
然后我创建了一个指向我的表和存储过程的数据模型,现在我 "only" 需要创建一个带有文本框的基本页面,以便为我需要的每个存储过程插入一个参数和一个按钮 运行.
每个存储过程将 运行 一个暂时不需要 return 任何值的 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 秒后,在 THE我在 SQL 中看到的 30 秒结束跟踪以下消息
RPC:Completed - exec [dbo].[SPU_RUNSSIS] @parameter='parametervalue'
如果我手动 运行 上面的代码,它就可以工作。
我快到了,但似乎找不到在某个时候触发存储过程执行的正确方法。
提前感谢您的任何建议
我设法通过直接从 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");
}
总之谢谢!
R
我正在尝试(我需要)创建小型 Web 应用程序来管理一些 ETL 过程,让我的用户只需几个按钮即可查看 SQL 服务器数据和 运行 几个 SSIS 包.
我能够使用 C# ASP.NET MVC CRUD 教程 HERE(非常有用)处理网站创建并显示我需要的数据。
然后我创建了一个指向我的表和存储过程的数据模型,现在我 "only" 需要创建一个带有文本框的基本页面,以便为我需要的每个存储过程插入一个参数和一个按钮 运行.
每个存储过程将 运行 一个暂时不需要 return 任何值的 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 秒后,在 THE我在 SQL 中看到的 30 秒结束跟踪以下消息
RPC:Completed - exec [dbo].[SPU_RUNSSIS] @parameter='parametervalue'
如果我手动 运行 上面的代码,它就可以工作。
我快到了,但似乎找不到在某个时候触发存储过程执行的正确方法。
提前感谢您的任何建议
我设法通过直接从 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");
}
总之谢谢! R