Search code examples
c#sql-serverssisetl

Executing an SSIS package with SQL query in C#


So, I want to execute an SSIS package through an SQL query in C#.

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

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

"Cannot access the package or the package does not exist. Verify that the package exists and that the user has permissions to it."

And I'm going crazy trying to figure out why.


Solution

  • 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

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

    It worked as inteended.