SSIS Script Component 的脚本可基本要以让我们做任何事,如同Asp.net的CodeBehind,基类的虚方法有相应的执行顺序
在DataFlow中添加一个Script Componet控件
点Edit Scipt进入代码编辑
添加如下代码
/* 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; using System.IO; [Microsoft.SqlServer.Dts.Pipeline.SSISScriptComponentEntryPointAttribute] public class ScriptMain : UserComponent { string filePath = @"d:\\temp\test.txt"; private void WriteFile(string s) { if (!File.Exists(filePath)) { File.Create(filePath); } using (StreamWriter sw = new StreamWriter(filePath, true)) { sw.WriteLine(s +": "+ DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss fff")); } } public override void PreExecute() { base.PreExecute(); WriteFile("PreExecute"); /* Add your code here for preprocessing or remove if not needed */ } public override void PostExecute() { base.PostExecute(); WriteFile("PostExecute"); /* Add your code here for postprocessing or remove if not needed You can set read/write variables here, for example: Variables.MyIntVar = 100 */ } public override void CreateNewOutputRows() { WriteFile("CreateNewOutputRows"); /* Add rows by calling the AddRow method on the member variable named "<Output Name>Buffer". For example, call MyOutputBuffer.AddRow() if your output was named "MyOutput". */ } public override void AcquireConnections(object Transaction) { //base.AcquireConnections(Transaction); WriteFile("AcquireConnections"); } public override void FinishOutputs() { base.FinishOutputs(); WriteFile("FinishOutputs"); } public override void PrimeOutput(int Outputs, int[] OutputIDs, Microsoft.SqlServer.Dts.Pipeline.PipelineBuffer[] Buffers) { base.PrimeOutput(Outputs, OutputIDs, Buffers); WriteFile("PrimeOutput"); } public override void ProcessInput(int InputID, Microsoft.SqlServer.Dts.Pipeline.PipelineBuffer Buffer) { base.ProcessInput(InputID, Buffer); WriteFile("ProcessInput"); } public override void ReleaseConnections() { base.ReleaseConnections(); WriteFile("ReleaseConnections"); } }
将基类方法的执行顺序记录到test.txt文件中,运行后,得到如下结果
第1行到第6行可以看到是AcquireConnections和ReleaseConnections执行了三次,基类的方法有的没有override,但常用的方法从第7行后可以看到,PreExecute执行前调用一次AcquireConnections方法,PostExecute方法执行后再调用一次ReleaseConnections方法。
ScriptComponent类请参见MSDN: http://msdn.microsoft.com/en-us/library/microsoft.sqlserver.dts.pipeline.scriptcomponent.aspx