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

转载于:https://www.cnblogs.com/zhengxingpeng/p/6688066.html

Script component 用法相关推荐

  1. C#调用WSC(Windows Script Component)

    WSC是一个很老的东西了,我现在是想用C#去执行一个WSH(javascript)的脚本,却又不想通过Process却调用cscript,直接call函数最好了. WSC 1 <?xml ver ...

  2. Fiddler (二) : Script 的 用法

    Fiddler (二) Script 用法(转):http://www.cnblogs.com/mrzhoushare/articles/4953592.html Fiddler 高级用法:Fiddl ...

  3. html script 设置编码,HTML Script text用法及代码示例

    HTML | DOM脚本文本属性用于设置或返回 用法: 它返回text属性:scriptObject.text 它用于设置文本属性:scriptObject.text = contents 属性值:它 ...

  4. 【重点】React.Component用法

    组件(Components)允许您将UI拆分为独立的可重用的部分,并单独的考虑每个部分. 总览 React.Component是一个抽象基类.这意味着直接引用React.Component是毫无意义的 ...

  5. vue 组件 Vue.component 用法

    Vue插槽,是学习vue中必不可少的一节,当初刚接触vue的时候,对这些掌握的一知半解,特别是作用域插槽一直没明白. 后面越来越发现插槽的好用. 1.插槽内可以放置什么内容? 2.默认插槽 3.具名插 ...

  6. 使用U3D给物体添加脚本时提示Can‘t add script component

    原因是刚创建脚本时没有立即改名,导致自动生成的类型和后来改的名不同,找不到该类名所以导入失败,将C#文件名和代码中类名统一即可

  7. 如何使用Elasticsearch groovy script脚本更新数据

    2019独角兽企业重金招聘Python工程师标准>>> 如何使用Elasticsearch groovy script脚本更新数据 博客分类: 搜索引擎,爬虫 今天细说一下elast ...

  8. 在.NET里用XSLT时怎么使用msxsl:script

    来源:在.NET里用XSLT时怎么使用msxsl:script (思归呓语) 今天在CSDN论坛上看到一个网友在请教关于msxsl:script的用法.感觉这问题也许有点意思,所以在下面简述一下. 在 ...

  9. pyscript的用法

    PyScript 核心特性 Python in the browser:启用 drop-in content.外部文件托管(基于 Pyodide 项目),以及不依赖服务器端配置的应用程序托管. Pyt ...

最新文章

  1. 如何在企业推行OKR?
  2. python控制台颜色输出以及字符串格式化输出
  3. 在grub中添加win7(以及从win7来的win10)的启动项
  4. python wing 免费下载安装
  5. [字符集]Unicode和UTF-8之间的转换详解
  6. jetty 通过配置文件嵌入式启动web服务
  7. 阿里安全开源顶尖技术“猎豹” 计算更快数据更安全
  8. python自关联_django自关联,auth模块
  9. linux中 ls |wc -l
  10. javaweb实训第二天上午——jQuery基础
  11. 用Node操作Firebird。
  12. matlab下载安装教程
  13. 怎样修改MySQL数据库的密码
  14. PyCharm专业版破解
  15. 手把手教你使用Python提取快递信息
  16. mysql+xf01x,[613]redis数据迁移
  17. 读书笔记(八)--货币战争 金权天下
  18. API是用来干什么的
  19. 《寄居者》 - 严歌苓
  20. 富士施乐Fuji Xerox Phaser 6700 驱动

热门文章

  1. 【Java】_2_Java程序入门第五课
  2. citrix 产品上线
  3. java fxml教程_JavaFX 初学入门(一):FXML嵌套与原始控件继承
  4. python3字典菜鸟教程_Python3 字典(map)
  5. 狂神css3笔记,【CSS】CSS3学习笔记(一)——选择器
  6. python cv模块_Python cv包_程序模块 - PyPI - Python中文网
  7. java画虚线_在java中绘制虚线
  8. PostgreSQL 12系统表(3)pg_tablespace
  9. Python机器学习:梯度下降法002模拟实现梯度下降法
  10. java 二维数组位置_java 找到二维数组指定元素的位置