码迷,mamicode.com
首页 > 其他好文 > 详细

Script component 简单应用

时间:2015-05-06 19:40:26      阅读:150      评论:0      收藏:0      [点我收藏+]

标签:

在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;

    /// <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();

        IDTSConnectionManager100 cnManager = this.Connections.Connection;
        SqlConnection 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();
        /*
         * Add your code here
         */
    }

    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();
        }
    }

 

Script component 简单应用

标签:

原文地址:http://www.cnblogs.com/ljhdo/p/4460249.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!