首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在脚本组件中写入变量

如何在脚本组件中写入变量
EN

Stack Overflow用户
提问于 2011-03-31 23:17:23
回答 4查看 21.2K关注 0票数 2

我想使用脚本组件使用C#将一个值写入到包中的变量中。

目前,我正在使用这些代码:

代码语言:javascript
复制
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是我在packages.After中的全局变量,脚本组件iam使用派生列来赋值。但是这个变量没有value.Plz帮我。谢谢

EN

回答 4

Stack Overflow用户

发布于 2011-04-01 03:20:20

这似乎是数据流任务中的脚本组件。不幸的是,您不能在这种意义上使用变量。您要么必须创建逐行保留这些值的列,要么必须更有创造性。

这里的一种方法是通过管道将数据流发送到记录集目标,创建一个对象类型的变量,并创建变量来保存数据流中需要的每一列。然后,您可以在记录集目标上使用Foreach循环,该目标现在存储在Object变量中。如果您有一些事情需要逐行执行,而不是数据流本身,那么现在就可以实现这一点,因为数据流任务每行调用一次。(这很麻烦,而且更适合您以外的其他需求)

您应该能够在派生列中实现上面要做的事情。

对数据流中脚本组件的引用:http://msdn.microsoft.com/en-us/library/aa337079.aspx

票数 4
EN

Stack Overflow用户

发布于 2011-06-02 12:05:49

您在问题中尝试实现的过程可以在Output Columns in Script Component Transformation任务的帮助下完成,而不是使用变量。下面的示例解释了如何实现这一点,并演示了如何在脚本组件中使用变量。但是,使用Derived Column Transformation任务也可以实现相同的功能。

此示例读取CSV文件,并基于第一列的值,从给定的位置和长度开始从第二列提取值。所有这些值都将存储在一个变量中。

分步过程:

  1. 创建CSV文件,如screenshot #1所示。如果第一列的值为Customer,我们将从第二列值的第9个字符中提取6个字符。如果第一列的值为Vendor,我们将从第二列值的21 character中提取7字符。对于其他值,使用SQL Scripts部分下的脚本,包将假定名为dbo.Destination的表的第二列的值为0。软件包将把CSV数据插入到这个表中。
  2. 在SSIS软件包上,创建6变量,如屏幕截图#2所示。配置OLE DB connection以连接到SQL Server实例。配置Flat File connection读取CSV文件,如截图#3 - #6所示。另外,在包的Control Flow选项卡上放置一个Data Flow Task。如OLE DB Destination.
  3. Configure
  4. Flat File Source #8和#<Flat File Source>E150Script Component Transformation Task >9中所示,为数据流选项卡配置屏幕快照、代码(请参阅screenshot #7)和screenshot

Flat File Source任务。

  1. 配置Script Component转换任务,如截图#10和#11所示。将创建一个名为SNumber、数据类型为four-byte signed integer [DT_I4]的新Output Column来存储提取的值。将Script Component代码替换为Script Component Code部分下显示的代码。PreExecute方法读取包变量的值,方法Input0_ProcessInputRow处理逻辑以填充OLE DB Destination任务的输出列,如屏幕截图#12和#13.
  2. Screenshot #14显示示例包execution.
  3. Screenshot #15显示包执行后表中的数据。<代码>H284<代码>G285

希望这能有所帮助。

SQL脚本:。

代码语言:javascript
复制
CREATE TABLE [dbo].[Destination](
    [Id] [int] IDENTITY(1,1) NOT NULL,
    [Header] [varchar](50) NOT NULL,
    [Value] [varchar](50) NOT NULL,
    [SNumber] [int] NOT NULL,
 CONSTRAINT [PK_Destination] PRIMARY KEY CLUSTERED ([Id] ASC)) ON [PRIMARY]
GO

脚本组件代码:

只能在SSIS 2008 and above.中使用的C#代码。

代码语言:javascript
复制
/* Microsoft SQL Server Integration Services Script Component
*  Write scripts using Microsoft Visual C# 2008.
*  ScriptMain is the entry point class of the script.*/

using System;
using System.Data;
using Microsoft.SqlServer.Dts.Pipeline.Wrapper;
using Microsoft.SqlServer.Dts.Runtime.Wrapper;

[Microsoft.SqlServer.Dts.Pipeline.SSISScriptComponentEntryPointAttribute]
public class ScriptMain : UserComponent
{
    IDTSVariables100 varCollection = null;
    string customer = string.Empty;
    int customerPosition = 0;
    int customerLength = 0;
    string vendor = string.Empty;
    int vendorPosition = 0;
    int vendorLength = 0;

    public override void PreExecute()
    {
        this.VariableDispenser.LockForRead("User::Customer");
        this.VariableDispenser.LockForRead("User::CustomerPosition");
        this.VariableDispenser.LockForRead("User::CustomerLength");
        this.VariableDispenser.LockForRead("User::Vendor");
        this.VariableDispenser.LockForRead("User::VendorPosition");
        this.VariableDispenser.LockForRead("User::VendorLength");
        this.VariableDispenser.GetVariables(out varCollection);

        customer = varCollection["User::Customer"].Value.ToString();
        customerPosition = Convert.ToInt32(varCollection["User::CustomerPosition"].Value);
        customerLength = Convert.ToInt32(varCollection["User::CustomerLength"].Value);
        vendor = varCollection["User::Vendor"].Value.ToString();
        vendorPosition = Convert.ToInt32(varCollection["User::VendorPosition"].Value);
        vendorLength = Convert.ToInt32(varCollection["User::VendorLength"].Value);

        base.PreExecute();
    }

    public override void PostExecute()
    {
        base.PostExecute();
    }

    public override void Input0_ProcessInputRow(Input0Buffer Row)
    {
        if (Row.Header.ToString().Trim() == customer)
        {
            Row.SNumber = Convert.ToInt32(Row.Value.Substring(customerPosition, customerLength));
        }
        else if (Row.Header.ToString().Trim() == vendor)
        {
            Row.SNumber = Convert.ToInt32(Row.Value.Substring(vendorPosition, vendorLength));
        }
        else
        {
            Row.SNumber = 0;
        }
    }

}

截图#1:

截图#2:

截图#3:

截图#4:

截图#5:

截图#6:

截图#7:

截图#8:

截图#9:

截图#10:

截图#11:

截图#12:

截图#13:

截图#14:

截图#15:

票数 4
EN

Stack Overflow用户

发布于 2011-03-31 23:34:35

看起来您可以在这里完全避免使用脚本组件,只需使用派生的Column Task,为SNumber、RNumber和MNumber创建3个新列,表达式如下

代码语言:javascript
复制
[Col1]=="MyName" ? ltrim(Substring([Column4],122,5)) : NULL(DT_WSTR,5)

诸若此类。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5502189

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档