在SSIS中,可使用C#編寫腳本,這是十分激動人心的事,可以使用C#代碼,使得Script Component無所不能。ide
第一部分:組件簡介
Script Component 有三種類型:Source, Destination and Transformation
ui
1,每種類型的腳本,都有兩種類型的參數:ReadOnly 和ReadWrite,在腳本中可使用 this.Variables.VariableName 來獲取或設置參數變量的值this
示例:建立四個Variable,並傳遞給Script componentspa
SSIS十分友好,在腳本中自動生成了一個子類,將Variable Name做爲屬性添加到子類中,引用Variable Name十分簡單.net
public class ScriptMain : UserComponent3d
在腳本代碼中,使用 this.Variables.VariableName 來獲取或設置參數變量的值code
2,能夠爲 Script component 指定connection ,若是在腳本中使用Ado.net,能夠直接建立Ado.net connection manager,在腳本中,使用如下代碼來引用connectioncomponent
IDTSConnectionManager100 cnManager = this.Connections.Connection;orm
3,Script component 不只有輸入的Variable,並且還有output / input columns,設置output / input columns 以便輸出或輸入表數據blog
示例中增長兩列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"; }