在 C# 中使用 SQL 查询执行 SSIS 包
Executing an SSIS package with SQL query in C#
所以,我想通过 C# 中的 SQL 查询来执行 SSIS 包。
我已经尽量简化了,所以缺少一些代码。
我的主要方法如下所示:
private static void Main(string[] args)
{
ExecuteSSIS("SSISPACKAGE.dtsx");
}
然后我们有带有连接字符串的 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());
}
}
SQL 查询本身工作正常,但是当我尝试 运行 程序时我得到错误
"Cannot access the package or the package does not exist. Verify that
the package exists and that the user has permissions to it."
我快要发疯了,想找出原因。
哇,好的,所以我修好了。原来是一个简单的 error/miss.
EXEC [SSISDB].[catalog].[create_execution] @package_name=N'@packageName'
C# 似乎不能 insert/find 参数@packagename,因为它在 ' ' 里面(撇号)
所以当我把它改成
EXEC [SSISDB].[catalog].[create_execution] @package_name= @packageName
它按预期工作。
所以,我想通过 C# 中的 SQL 查询来执行 SSIS 包。
我已经尽量简化了,所以缺少一些代码。
我的主要方法如下所示:
private static void Main(string[] args)
{
ExecuteSSIS("SSISPACKAGE.dtsx");
}
然后我们有带有连接字符串的 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());
}
}
SQL 查询本身工作正常,但是当我尝试 运行 程序时我得到错误
"Cannot access the package or the package does not exist. Verify that the package exists and that the user has permissions to it."
我快要发疯了,想找出原因。
哇,好的,所以我修好了。原来是一个简单的 error/miss.
EXEC [SSISDB].[catalog].[create_execution] @package_name=N'@packageName'
C# 似乎不能 insert/find 参数@packagename,因为它在 ' ' 里面(撇号) 所以当我把它改成
EXEC [SSISDB].[catalog].[create_execution] @package_name= @packageName
它按预期工作。