Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/design-patterns/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何从以编程方式生成的SSIS包中获取调用应用程序的已处理行数?_C#_Sql Server_Ssis_Event Handling - Fatal编程技术网

C# 如何从以编程方式生成的SSIS包中获取调用应用程序的已处理行数?

C# 如何从以编程方式生成的SSIS包中获取调用应用程序的已处理行数?,c#,sql-server,ssis,event-handling,C#,Sql Server,Ssis,Event Handling,我正在使用C#和SQL Server 2012以编程方式生成和执行SSIS包。生成的每个包都包含一个数据流任务,其中包含一个用于读取CSV的平面文件源,以及一个连接到SQL Server的OLE DB目标。它们之间是一个连接了SSIS变量的行计数组件 在包执行结束后,我希望将行计数中的值返回给调用应用程序 似乎只需在代码中创建变量&Row Count,如下所示: [...] // Row count: Create a package variable to store the row cou

我正在使用C#和SQL Server 2012以编程方式生成和执行SSIS包。生成的每个包都包含一个数据流任务,其中包含一个用于读取CSV的平面文件源,以及一个连接到SQL Server的OLE DB目标。它们之间是一个连接了SSIS变量的行计数组件

在包执行结束后,我希望将行计数中的值返回给调用应用程序

似乎只需在代码中创建变量&Row Count,如下所示:

[...]

// Row count: Create a package variable to store the row count value
var ssisRowCountVariable = package.Variables.Add("MySsisVar", false, "User", 0);

// Row count: Create Row component
IDTSComponentMetaData100 componentRowCount = dataFlowTask.ComponentMetaDataCollection.New();
componentRowCount.Name = "RowCount";
componentRowCount.ComponentClassID = "DTSTransform.RowCount.4";

// Row count: Get row count design-time instance, and initialize component
CManagedComponentWrapper instanceRowCount = componentRowCount.Instantiate();
instanceRowCount.ProvideComponentProperties();

// Row count: Set the variable name property
instanceRowCount.SetComponentProperty("VariableName", "User::MySsisVar");

// Hooking up pipeline Paths
[...]

// Execute package
package.Execute()
然后在执行包后尝试读取值:

int Rows = Convert.ToInt32(ssisRowCountVariable.Value);
不起作用


如何将行计数组件的值返回到调用应用程序?

不幸的是,您无法直接从
Package.Execute()
call获取包变量的运行时值。但是,您获取已处理记录的#的任务可以通过以下方式之一实现:

  • 向包中添加一个任务,该任务将已处理行变量的值保存到某个存储器中。任务可以在数据流之后执行,也可以在其PostExecute处理程序中执行。
    存储可以是SQL数据库或其他类似Web服务的东西;值将与SQL DB的执行SQL任务或Web服务的脚本任务/Web服务任务一起保存。下面是这种方法的一个例子
  • 使用数据流标准PostExecute信息消息
    [SSIS.Pipeline]信息:“已写入N行。
    执行包捕获事件,如中所述,然后检查结果

请注意,第二种方法仅在以
包的形式运行包时有效。在SSIS中执行
,而不使用SSIS目录。如果您自己生成包并在生成后立即执行包,则表示您没有生成和部署SSIDB项目。

不幸的是,您无法直接从
Package.Execute()
调用中获取包变量的运行时值。但是,您获取已处理记录的#的任务可以通过以下方式之一实现:

  • 向包中添加一个任务,该任务将已处理行变量的值保存到某个存储器中。任务可以在数据流之后执行,也可以在其PostExecute处理程序中执行。
    存储可以是SQL数据库或其他类似Web服务的东西;值将与SQL DB的执行SQL任务或Web服务的脚本任务/Web服务任务一起保存。下面是这种方法的一个例子
  • 使用数据流标准PostExecute信息消息
    [SSIS.Pipeline]信息:“已写入N行。
    执行包捕获事件,如中所述,然后检查结果

请注意,第二种方法仅在以
包的形式运行包时有效。在SSIS中执行
,而不使用SSIS目录。如果您自己生成包并在生成后立即执行它,那么您就不会生成和部署SSIDB项目。

在进一步使用第二种方法之后,我发现最好的方法似乎是连接行计数组件,如问题中所述,并添加代码,使其在设置或更改其值时触发事件:

ssisRowCountVariable.RaiseChangeEvent = true;
现在设置一个从标准DefaultEvents类派生的事件处理程序,以捕获OnVariableValueChanged事件:

class MySsisEvents : DefaultEvents
{
    public int Rows { get; private set; }

    public override void OnVariableValueChanged(DtsContainer DtsContainer, Variable variable, ref bool fireAgain)
    {
        this.Rows = Convert.ToInt32(variable.Value);
    }
}
必须修改对
package.Execute()
的调用以连接事件处理程序,如下所示:

// Execute package
var mySsisEventHandler = new MySsisEvents();
package.Execute(null, null, mySsisEventHandler, null, null);

处理的行数现在可以作为mySsisEventHandler获得。行数

在使用第二种方法之后,我发现最好的方法似乎是如问题中所述连接行计数组件,并添加代码,以便在其值设置或更改时触发事件:

ssisRowCountVariable.RaiseChangeEvent = true;
现在设置一个从标准DefaultEvents类派生的事件处理程序,以捕获OnVariableValueChanged事件:

class MySsisEvents : DefaultEvents
{
    public int Rows { get; private set; }

    public override void OnVariableValueChanged(DtsContainer DtsContainer, Variable variable, ref bool fireAgain)
    {
        this.Rows = Convert.ToInt32(variable.Value);
    }
}
必须修改对
package.Execute()
的调用以连接事件处理程序,如下所示:

// Execute package
var mySsisEventHandler = new MySsisEvents();
package.Execute(null, null, mySsisEventHandler, null, null);

处理的行数现在可用作mySsisEventHandler。行

我尝试了第二种方法,但PostExecute是错误的事件,因为它没有提供您提到的信息消息的参数。OnInformation事件没有(参数说明,但当事件多次激发时,没有文本为
的事件)写入了N行。
是否触发过。我尝试了第二种方法,但PostExecute是错误的事件,因为它没有提供您提到的信息消息的参数。OnInformation事件有(参数说明,但当事件多次触发时,没有文本为
的事件)已写入N行。
已触发。