简体   繁体   English

在 C# 中执行 SSIS package 和 SQL 查询

[英]Executing an SSIS package with SQL query in C#

So, I want to execute an SSIS package through an SQL query in C#.因此,我想通过 C# 中的 SQL 查询执行 SSIS package。

I've simplified it as much as possible, so some of the code is lacking.我尽可能地简化了它,所以缺少一些代码。

My main method looks like the following:我的主要方法如下所示:

private static void Main(string[] args)
{
    ExecuteSSIS("SSISPACKAGE.dtsx");
}

And then we have the ExecuteSSIS method with the connection string然后我们有带有连接字符串的 ExecuteSSIS 方法

private static string SSISDB = "Data source=.; Initial Catalog=SSISDB; Integrated Security=SSPI;";

public static void ExecuteSSIS(string PackageName)

{

string executeLoad = @"
                    DECLARE @execution_id BIGINT

                    EXEC [SSISDB].[catalog].[create_execution] @package_name=N'@packageName'
                        ,@execution_id = @execution_id OUTPUT
                        ,@folder_name = N'CorrectFolderName'
                        ,@project_name = N'CorrectProjectName'
                        ,@use32bitruntime = False
                        ,@reference_id = NULL

                    SELECT @execution_id

                    DECLARE @var0 SMALLINT = 1

                    EXEC [SSISDB].[catalog].[set_execution_parameter_value] @execution_id
                        ,@object_type = 50
                        ,@parameter_name = N'LOGGING_LEVEL'
                        ,@parameter_value = @var0

                    EXEC [SSISDB].[catalog].[start_execution] @execution_id";
                           

using (SqlConnection connection = new SqlConnection(SSISDB))
{
    try
    {
        SqlCommand sqlCommand = new SqlCommand(executeLoad, connection);
        sqlCommand.Parameters.AddWithValue("@packageName", PackageName);
        sqlCommand.Connection.Open();
        sqlCommand.ExecuteNonQuery();
        sqlCommand.Connection.Close();
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex.ToString());
    }
}

The SQL query in of itself works just fine, but when I try to run the program I get the error SQL 查询本身工作正常,但是当我尝试运行该程序时出现错误

"Cannot access the package or the package does not exist. Verify that the package exists and that the user has permissions to it." “无法访问 package 或 package 不存在。验证 package 是否存在以及用户是否有权访问它。”

And I'm going crazy trying to figure out why.我快要发疯了,想找出原因。

Wow okay, so i fixed it.哇好吧,所以我修好了。 Turns out to be a simple error/miss.原来是一个简单的错误/遗漏。

 EXEC [SSISDB].[catalog].[create_execution] @package_name=N'@packageName'

It seems C# can't insert/find the parameter @packagename, while it's inside ' ' (Apostrophe) So when i changed it to似乎 C# 无法插入/找到参数@packagename,而它位于“” (撇号)内所以当我将其更改为

EXEC [SSISDB].[catalog].[create_execution] @package_name= @packageName

It worked as inteended.它按预期工作。

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

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