如何写入脚本组件中的变量

发布于 2024-10-28 04:13:27 字数 774 浏览 3 评论 0原文

我想使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

拥抱影子 2024-11-04 04:13:27

看来这是数据流任务中的脚本组件。不幸的是,你不能在这个意义上使用变量。您要么必须创建逐行保留这些值的列,要么必须发挥创意。

这里的一种方法是将数据流通过管道传输到记录集目标,创建对象类型的变量,以及用于保存此数据流中所需的每一列的变量。然后,您可以对现在存储在 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

雨后咖啡店 2024-11-04 04:13:27

您在问题中尝试实现的过程可以借助脚本组件转换任务中的输出列来完成,而不是使用变量。下面的示例解释了如何实现这一点,并演示了如何在脚本组件中使用变量。不过,使用派生列转换任务也可以实现相同的功能。

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

分步过程:

  1. 创建一个 CSV 文件,如屏幕截图 #1 所示。如果第一列的值为 Customer,我们将从第二列值的第 9 个字符中提取 6 个字符。如果第一列的值为 Vendor,我们将从第二列值的 21 字符中提取 7 个字符。对于每个其他值,程序包将假定第二列值为零。

  2. 使用SQL 脚本部分下的脚本创建一个名为dbo.Destination的表。该包会将 CSV 数据插入到该表中。

  3. 在 SSIS 包上,创建 6 个变量,如屏幕截图 #2 所示。配置 OLE DB 连接 以连接到 SQL Server 实例。配置平面文件连接以读取 CSV 文件,如屏幕截图 #3 - #6 所示。另外,将数据流任务放置在包的控制流选项卡上。

  4. 使用平面文件源脚本组件转换任务(请参阅屏幕截图#7)和配置数据流选项卡OLE DB 目标

  5. 配置平面文件源任务,如屏幕截图#8和#9所示。

  6. 配置脚本组件转换任务,如屏幕截图#10和#11所示。创建一个名为 SNumber 数据类型为四字节有符号整数 [DT_I4] 的新输出列来存储提取的值。将脚本组件代码替换为脚本组件代码部分下显示的代码。 PreExecute 方法读取包变量的值,Input0_ProcessInputRow 方法处理逻辑以填充输出列 SNumber

  7. 配置 OLE DB 目标 任务,如屏幕截图 #12 和 #13 所示。

  8. 屏幕截图 #14 显示示例包执行。

  9. Screenshot #15 显示包执行后表 dbo.Destination 中的数据。

希望有帮助。

SQL 脚本:

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

脚本组件代码:

C#代码,只能在SSIS 2008及更高版本中使用。

/* 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:

1

屏幕截图 #2:

2

屏幕截图 #3:

3

屏幕截图 #4:

4

屏幕截图 #5 :

5

屏幕截图#6:

6

屏幕截图 #7:

7

屏幕截图 #8:

8

屏幕截图 #9:

< img src="https://i.sstatic.net/uHbBE.png" alt="9">

屏幕截图 #10:

10

屏幕截图 #11:

11

屏幕截图#12:

12

屏幕截图 #13:

13

屏幕截图 #14:

14

屏幕截图 #15:

15

The process that you are trying to achieve in the question can be done with the help of the Output Columns in Script 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 using Derived 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:

  1. 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 value Vendor, 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.

  2. Create a table named dbo.Destination using the script under SQL Scripts section. The package will insert the CSV data into this table.

  3. 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 a Flat File connection to read the CSV file as shown in screenshots #3 - #6. Also, place a Data Flow Task on the Control Flow tab of the package.

  4. Configure the Data Flow Tab with a Flat File Source, Script Component Transformation Task (Refer screenshot #7) and an OLE DB Destination.

  5. Configure the Flat File Source task as shown in screenshots #8 and #9.

  6. Configure the Script Component transformation task as shown in screenshots #10 and #11. A new Output Column named SNumber of data type four-byte signed integer [DT_I4] is created to store the extracted value. Replace the Script Component code with code displayed under Script Component Code section. PreExecute method reads the package variables' values and the method Input0_ProcessInputRow processes the logic to populate the Output Column SNumber.

  7. Configure the OLE DB Destination task as shown in screenshots #12 and #13.

  8. Screenshot #14 shows sample package execution.

  9. Screenshot #15 shows the data in the table dbo.Destination after the package execution.

Hope that helps.

SQL Scripts:
.

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

Script Component Code:

C# code that can be used only in SSIS 2008 and above.
.

/* 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;
        }
    }

}

Screenshot #1:

1

Screenshot #2:

2

Screenshot #3:

3

Screenshot #4:

4

Screenshot #5:

5

Screenshot #6:

6

Screenshot #7:

7

Screenshot #8:

8

Screenshot #9:

9

Screenshot #10:

10

Screenshot #11:

11

Screenshot #12:

12

Screenshot #13:

13

Screenshot #14:

14

Screenshot #15:

15

倾城泪 2024-11-04 04:13:27

在转换编辑器上选择变量作为 ReadWriteVariable,然后使用以下代码:

this.ReadWriteVariables["User::YourVariable"].Value = "New Value";

Select your variable as ReadWriteVariable on the Transform Editor then use the code below:

this.ReadWriteVariables["User::YourVariable"].Value = "New Value";
懷念過去 2024-11-04 04:13:27

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

[Col1]=="MyName" ? ltrim(Substring([Column4],122,5)) : NULL(DT_WSTR,5)

,依此类推。

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

[Col1]=="MyName" ? ltrim(Substring([Column4],122,5)) : NULL(DT_WSTR,5)

and so on.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文