如何写入脚本组件中的变量
我想使用 C# 使用脚本组件向包中的变量写入一个值。
目前我正在使用这些代码:
public class ScriptMain : UserComponent
{
string s, r, m;
public override void PreExecute()
{
base.PreExecute();
}
public override void PostExecute()
{
base.PostExecute();
Variables.SNumber = s;
Variables.RNumber = r;
Variables.MNumber = m;
}
public override void Input0_ProcessInputRow(Input0Buffer Row)
{
if (Row.Col1.Equals("MyName"))
{
s = Row.Column4.Substring(122, 5).Trim();
r = Row.Column8.Substring(5, 14).Trim();
m = Row.Column8.Substring(66, 4).Trim() + "." + Row.Column8.Substring(70,
2).Trim();
}
}
}
SNumber、Mnumber 和 RNumber 是包中的全局变量。在脚本组件之后,我使用派生列来分配值。 但变量没有价值。请帮助我。 谢谢
I want to write a value to a variable in package using script component using C#.
presently i am using these code:
public class ScriptMain : UserComponent
{
string s, r, m;
public override void PreExecute()
{
base.PreExecute();
}
public override void PostExecute()
{
base.PostExecute();
Variables.SNumber = s;
Variables.RNumber = r;
Variables.MNumber = m;
}
public override void Input0_ProcessInputRow(Input0Buffer Row)
{
if (Row.Col1.Equals("MyName"))
{
s = Row.Column4.Substring(122, 5).Trim();
r = Row.Column8.Substring(5, 14).Trim();
m = Row.Column8.Substring(66, 4).Trim() + "." + Row.Column8.Substring(70,
2).Trim();
}
}
}
SNumber,Mnumber and RNumber are my global variables in the packages.After the script component iam using derived column to assign the values.
But the variables has no value.Plz help me.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
看来这是数据流任务中的脚本组件。不幸的是,你不能在这个意义上使用变量。您要么必须创建逐行保留这些值的列,要么必须发挥创意。
这里的一种方法是将数据流通过管道传输到记录集目标,创建对象类型的变量,以及用于保存此数据流中所需的每一列的变量。然后,您可以对现在存储在 Object 变量中的 Recordset 目标使用 Foreach 循环。如果您需要逐行执行某些操作,而该操作本身不是数据流,那么您现在就可以实现这一点,因为数据流任务会为每一行调用一次。 (这很麻烦,并且更适合您以外的其他需求)
您应该能够在派生列中实现上面想要做的事情。
数据流中的脚本组件参考:http://msdn.microsoft.com/en -us/library/aa337079.aspx
It seems this is a script compoent in a data flow task. You cannot use variables in this sense, unfortunately. You'll either have to create columns that keep these values for a row-by-row basis, or you'll have to get creative.
One way here is to pipe your data flow to a recordset destination, create a variable of Object type, and variables to hold each column you need in this data flow. You can then use a Foreach Loop on the Recordset destination that's now stored in the Object variable. If you have something that needs to do stuff on a row-by-row basis that isn't a data flow per se, you can achieve this now, as the data flow task is called once for each row. (This is cumbersome, and more suitable for other needs than yours)
You should be able to achieve what you want to do above in a Derived Column.
Reference on Script component in Data Flow : http://msdn.microsoft.com/en-us/library/aa337079.aspx
您在问题中尝试实现的过程可以借助
脚本组件转换
任务中的输出列
来完成,而不是使用变量。下面的示例解释了如何实现这一点,并演示了如何在脚本组件中使用变量。不过,使用派生列转换
任务也可以实现相同的功能。此示例读取 CSV 文件,并根据第一列值,从给定位置和长度开始从第二列中提取值。所有这些值都将存储在一个变量中。
分步过程:
创建一个 CSV 文件,如屏幕截图 #1 所示。如果第一列的值为
Customer
,我们将从第二列值的第 9 个字符中提取 6 个字符。如果第一列的值为Vendor
,我们将从第二列值的 21 字符中提取 7 个字符。对于每个其他值,程序包将假定第二列值为零。使用SQL 脚本部分下的脚本创建一个名为
dbo.Destination
的表。该包会将 CSV 数据插入到该表中。在 SSIS 包上,创建 6 个变量,如屏幕截图 #2 所示。配置
OLE DB 连接
以连接到 SQL Server 实例。配置平面文件连接
以读取 CSV 文件,如屏幕截图 #3 - #6 所示。另外,将数据流任务
放置在包的控制流
选项卡上。使用
平面文件源
、脚本组件转换任务
(请参阅屏幕截图#7)和配置数据流选项卡OLE DB 目标
。配置
平面文件源
任务,如屏幕截图#8和#9所示。配置
脚本组件
转换任务,如屏幕截图#10和#11所示。创建一个名为 SNumber 数据类型为四字节有符号整数 [DT_I4]
的新输出列
来存储提取的值。将脚本组件
代码替换为脚本组件代码
部分下显示的代码。PreExecute
方法读取包变量的值,Input0_ProcessInputRow
方法处理逻辑以填充输出列SNumber
。配置
OLE DB 目标
任务,如屏幕截图 #12 和 #13 所示。屏幕截图 #14 显示示例包执行。
Screenshot #15 显示包执行后表
dbo.Destination
中的数据。希望有帮助。
SQL 脚本:
。
脚本组件代码:
C#代码,只能在
SSIS 2008及更高版本
中使用。。
屏幕截图 #1:
屏幕截图 #2:
屏幕截图 #3:
屏幕截图 #4:
屏幕截图 #5 :
屏幕截图#6:
屏幕截图 #7:
屏幕截图 #8:
屏幕截图 #9:
< img src="https://i.sstatic.net/uHbBE.png" alt="9">
屏幕截图 #10:
屏幕截图 #11:
屏幕截图#12:
屏幕截图 #13:
屏幕截图 #14:
屏幕截图 #15:
The process that you are trying to achieve in the question can be done with the help of the
Output Columns
inScript Component Transformation
task instead of using the variables. The below example explains how this can be achieved and also it demonstrates how variables can be used within the script component. However the same functionality can be achieved usingDerived Column Transformation
task as well.This example reads a CSV file and based on the first column value, it will extract value from second column starting from a give position and length. All these values will be stored in a variable.
Step-by-step process:
Create a CSV file as shown in screenshot #1. If the first column has the value
Customer
, we will extract 6 characters from 9th character on the second column value. If the first column has the valueVendor
, we will extract 7 characters from 21st character on the second column value. For every other value, the package will assume value zero for second column value.Create a table named
dbo.Destination
using the script under SQL Scripts section. The package will insert the CSV data into this table.On the SSIS package, create 6 variables as shown in screenshot #2. Configure an
OLE DB connection
to connect to the SQL Server instance. Configure aFlat File connection
to read the CSV file as shown in screenshots #3 - #6. Also, place aData Flow Task
on theControl Flow
tab of the package.Configure the Data Flow Tab with a
Flat File Source
,Script Component Transformation Task
(Refer screenshot #7) and anOLE DB Destination
.Configure the
Flat File Source
task as shown in screenshots #8 and #9.Configure the
Script Component
transformation task as shown in screenshots #10 and #11. A newOutput Column
named SNumber of data typefour-byte signed integer [DT_I4]
is created to store the extracted value. Replace theScript Component
code with code displayed underScript Component Code
section.PreExecute
method reads the package variables' values and the methodInput0_ProcessInputRow
processes the logic to populate the Output ColumnSNumber
.Configure the
OLE DB Destination
task as shown in screenshots #12 and #13.Screenshot #14 shows sample package execution.
Screenshot #15 shows the data in the table
dbo.Destination
after the package execution.Hope that helps.
SQL Scripts:
.
Script Component Code:
C# code that can be used only in
SSIS 2008 and above
..
Screenshot #1:
Screenshot #2:
Screenshot #3:
Screenshot #4:
Screenshot #5:
Screenshot #6:
Screenshot #7:
Screenshot #8:
Screenshot #9:
Screenshot #10:
Screenshot #11:
Screenshot #12:
Screenshot #13:
Screenshot #14:
Screenshot #15:
在转换编辑器上选择变量作为
ReadWriteVariable
,然后使用以下代码:Select your variable as
ReadWriteVariable
on the Transform Editor then use the code below:看起来您可以在这里完全避免脚本组件,而只需使用派生列任务,为 SNumber、RNumber 和 MNumber 创建 3 个新列,并使用类似的表达式
,依此类推。
Looks like you could avoid a Script Component completely here and just use a Derived Column Task, creating 3 new columns for SNumber, RNumber and MNumber, with an expression like
and so on.