Script component 用法

在SSIS中,可以使用C#编写脚本,这是十分激动人心的事,能够使用C#代码,使得Script Component无所不能。

第一部分:组件简介
Script Component 有三种类型:Source, Destination and Transformation

1,每种类型的脚本,都有两种类型的参数:ReadOnly 和ReadWrite,在脚本中可以使用 this.Variables.VariableName 来获取或设置参数变量的值

示例:创建四个Variable,并传递给Script component

SSIS十分友好,在脚本中自动生成了一个子类,将Variable Name作为属性添加到子类中,引用Variable Name十分简单

public class ScriptMain : UserComponent

在脚本代码中,使用 this.Variables.VariableName 来获取或设置参数变量的值

2,可以为 Script component 指定connection ,如果在脚本中使用Ado.net,可以直接创建Ado.net connection manager,在脚本中,使用以下代码来引用connection

IDTSConnectionManager100 cnManager = this.Connections.Connection;

3,Script component 不仅有输入的Variable,而且还有output / input columns,设置output / input columns 以便输出或输入表数据

示例中增加两列Code和name,分别是string类型

第二部分:Source 组件示例

4,如果Script Component 作为Source,那么使用脚本获取数据之后,可以使用将数据逐行添加到Source 的输出buff中。

在将获得的数据集插入到output buff中时,SSIS使用的代码逻辑是:先向output buff中插入一行,然后为该行的字段赋值

    DataRow dr=dt.Rows[0];

    this.Output0Buffer.AddRow();
            this.Output0Buffer.Code = dr["code"].ToString();
            this.Output0Buffer.Name = dr["name"].ToString();

示例Code

    DataTable dt;
    IDTSConnectionManager100 cnManager;    SqlConnection cnn;
    /// <summary>
    /// This method is called once, before rows begin to be processed in the data flow.
    ///
    /// You can remove this method if you don‘t need to do anything here.
    /// </summary>
    public override void PreExecute()
    {
        base.PreExecute();

        cnManager = this.Connections.Connection;
        cnn = (SqlConnection)cnManager.AcquireConnection(null);

        SqlCommand cmd = cnn.CreateCommand();
        cmd.CommandText = "select code,name from [dbo].[tbExcel]";
        cmd.CommandType = CommandType.Text;
        cmd.CommandTimeout = 60000;

        dt = new DataTable("dt");
        SqlDataAdapter sda = new SqlDataAdapter(cmd);
        sda.Fill(dt);
    }

    /// <summary>
    /// This method is called after all the rows have passed through this component.
    ///
    /// You can delete this method if you don‘t need to do anything here.
    /// </summary>
    public override void PostExecute()
    {
        base.PostExecute();
             cnManager.ReleaseConnection(cnn);
    }

    public override void CreateNewOutputRows()
    {
        foreach (DataRow dr in dt.Rows)
        {
            this.Output0Buffer.AddRow();
            this.Output0Buffer.Code = dr["code"].ToString();
            this.Output0Buffer.Name = dr["name"].ToString();
        }
    }

5,Script Component做为Destination,既然是作为Destination,那么肯定是有input column,用以接收上个数据源组件或转换组件的输出数据流。

示例代码如下

    SqlCommand cmd = new SqlCommand();
    DataTable dt = new DataTable("dt");
    IDTSConnectionManager100 cnManager;    SqlConnection cnn;
    /// <summary>
    /// This method is called once, before rows begin to be processed in the data flow.
    ///
    /// You can remove this method if you don‘t need to do anything here.
    /// </summary>
    public override void PreExecute()
    {
        base.PreExecute();

        cnManager = this.Connections.Connection;
        cnn = (SqlConnection)cnManager.AcquireConnection(null);

        cmd.Connection = cnn;
        cmd.CommandType = CommandType.Text;
        cmd.CommandTimeout = 60000;

        dt.Columns.Add("code", typeof(string));
        dt.Columns.Add("name", typeof(string));
    }

    /// <summary>
    /// This method is called after all the rows have passed through this component.
    ///
    /// You can delete this method if you don‘t need to do anything here.
    /// </summary>
    public override void PostExecute()
    {
        base.PostExecute();

        foreach(DataRow dr in dt.Rows)
        {
            string strSql = string.Format(@"
insert into dbo.tbExcel2(code,name)
values(‘{0}‘,‘{1}‘)"
                , dr["code"].ToString(), dr["name"].ToString());

            cmd.CommandText = strSql;
            cmd.ExecuteNonQuery();
        }

cnManager.ReleaseConnection(cnn);
    }

    /// <summary>
    /// This method is called once for every row that passes through the component from Input0.
    ///
    /// Example of reading a value from a column in the the row:
    ///  string zipCode = Row.ZipCode
    ///
    /// Example of writing a value to a column in the row:
    ///  Row.ZipCode = zipCode
    /// </summary>
    /// <param name="Row">The row that is currently passing through the component</param>
    public override void Input0_ProcessInputRow(Input0Buffer Row)
    {
        DataRow dr = dt.NewRow();
        dr["code"] = Row.code;
        dr["name"] = Row.name;

        dt.Rows.Add(dr);
    }

6,Script Component 作为 Transformation ,转换,顾名思义是将输入进行转换成符合要求的输出,所以,作为 Transformation 的Script Component 既有input columns,也有output columns。

示例代码如下,Input0Buffer 这个类中即包含了InputColumns,也包含了OutputColumns,InputColumns的Column是ReadOnly的,通过Input0Buffer 实例对OutputColumns进行赋值,转换数据流。

    /// <summary>
    /// This method is called once, before rows begin to be processed in the data flow.
    ///
    /// You can remove this method if you don‘t need to do anything here.
    /// </summary>
    public override void PreExecute()
    {
        base.PreExecute();
        /*
         * Add your code here
         */
    }

    /// <summary>
    /// This method is called after all the rows have passed through this component.
    ///
    /// You can delete this method if you don‘t need to do anything here.
    /// </summary>
    public override void PostExecute()
    {
        base.PostExecute();
        /*
         * Add your code here
         */
    }

    /// <summary>
    /// This method is called once for every row that passes through the component from Input0.
    ///
    /// Example of reading a value from a column in the the row:
    ///  string zipCode = Row.ZipCode
    ///
    /// Example of writing a value to a column in the row:
    ///  Row.ZipCode = zipCode
    /// </summary>
    /// <param name="Row">The row that is currently passing through the component</param>
    public override void Input0_ProcessInputRow(Input0Buffer Row)
    {
        Row.codeout = Row.code + "_out";
        Row.nameout = Row.name + "_out";
    }
时间: 2024-07-31 10:43:40

Script component 用法的相关文章

Script Component 引用package variable

ScriptComponet 的变量分为两种类型,ReadOnly和ReadWrite,使用C#引用这两种类型的变量,有一点不同. 1,创建两个变量 2,将变量传递给script component 3,在script component中引用变量有两种方式 3.1 使用变量名作为来引用变量 int code = this.Variables.VarCode; //string name = this.Variables.VarName; //this.Variables.VarName = "

微软BI 之SSIS 系列 - 使用 Script Component Destination 和 ADO.NET 解析不规则文件并插入数据

开篇介绍 这一篇文章是 微软BI 之SSIS 系列 - 带有 Header 和 Trailer 的不规则的平面文件输出处理技巧 的续篇,在上篇文章中介绍到了对于这种不规则文件输出的处理方式.比如下图中的这种不规则文件,第一行,第二行 Header 部分,第三行的内容 Content 部分,最后一行的 Trailer 部分. 在前几个课程 微软BI SSIS 2012 ETL 控件与案例精讲 第43,44,45,46 课中,我分别讲解了如何使用 .Script Component Source 解

Script component 简单应用

在SSIS中,可以使用C#编写脚本,这是十分激动人心的事,能够使用C#代码,使得Script Component无所不能. 第一部分:组件简介Script Component 有三种类型:Source, Destination and Transformation 1,每种类型的脚本,都有两种类型的参数:ReadOnly 和ReadWrite,在脚本中可以使用 this.Variables.VariableName 来获取或设置参数变量的值 示例:创建四个Variable,并传递给Script

SSIS Script Component

Why Script Component Script Component使我们具有在SSIS中使用.net自定义代码的功能,我们可以使用它达到以下的目的,或者说在ETL中有如下需要的时候我们应该考虑使用ScriptComponent: 如果我们需要对数据做多重转换(比如在两个列上分别加上值然后求平均等),我们可以使用ScriptComponent而不是在数据流中使用多个转换空间 访问一些已经存在于.net程序及中的业务规则 使用以下自定义的公式或函数,比如使用银行卡校验算法(Luhn)来验证信

使用Script Component源处理不规则平面文件

微软 BI 系列随笔 - SSIS 2012 高级应用 - Script Component处理不规则平面文件 场景介绍 在使用SSIS从平面文件导入源数据时,最常遇到的是以下两种情况: 导入规则的平面文件,这种文件的每一行数据的解析规则都是一样的 导入不规则的平面文件,这种文件可能包含多种数据结构,比如某些行是头(Head),某些行是内容(Content),某些行是尾(Tail),三种数据的解析格式都不一样 源文件的字符编码集不一样,例如文件是来自于不同的系统(或区域),这些文件的编码有可能是

05_Fiddler的Script 脚本用法

现在我们来看看Fiddler的高级用法. Fiddler Script.   Fiddler中的script 可以让我们自动修改Http request和Response 的内容.  而不用手动地去下"断点"来修改http Request或Response中的值. Fiddler的作者 Fiddler 的作者是 Eric Lawrence 是个大师级的人物, 目前在微软总部西雅图工作. 他的博客是: http://www.ericlawrence.com/Eric/ 博客中能看到他的简

Lookup component 用法

Lookup component 类似于Tsql的join子句, select a.* ,b.* from dbo.tis a left join dbo. tdes b on a.code=b.code Lookup component的组成分析 两个输入,一个是上游数据流的输入 dbo.tis,一个是要查找的数据集 dbo.tdes: 两个输出,一个是输出匹配成功的数据,一个是输出匹配不成功的数据.上游数据流的一行数据跟整个查找集进行匹配,如果匹配成功,那么输出匹配成功的数据,否则,输出匹配

Conditional Split component 用法

Conditional Split 用于将数据流按照条件进行拆分,每一个output 都有name和condition. 数据流逐行按照condition进行match,如果match,那么改行会进入相应的output.如果数据行跟所有的condition都不match,那么改行会进入defaultoutput,这种逻辑类似于C语言中的switch语句.进行match的时候,字符串常量使用双引号标识:“string”,等于号使用==. SSIS在Conditional Split Editor中

Derived Column 用法

Derived Column Component 用法是为数据流增加派生列,Derived column  有两种用法:add as new column 或 replace . 图中,增加一个 Derived Column Name 是Country add as new column:derived column 是增加数据流的列 replace:代替数据流中的column 下游 component 从 derived column component 获取的 column metadata