IS Extending Packages With Scripting - .microsoft

Transcription

Integration Services:Extending Packages with ScriptingSQL Server 2012 Books OnlineSummary: You can extend the power of Integration Services (SSIS) by adding codewithin the wrappers provided by the Script task and the Script component. This sectionof the Developer Reference provides instructions and examples for extending thecontrol flow and data flow of an SSIS package using the Script task and the Scriptcomponent.Category: ReferenceApplies to: SQL Server 2012Source: SQL Server 2012 Books Online (link to source content)E-book publication date: January 2013

Copyright 2012 by Microsoft CorporationAll rights reserved. No part of the contents of this book may be reproduced or transmitted in any form or by any meanswithout the written permission of the publisher.Microsoft and the trademarks listed lectualProperty/Trademarks/EN-US.aspx are trademarks of theMicrosoft group of companies. All other marks are property of their respective owners.The example companies, organizations, products, domain names, email addresses, logos, people, places, and eventsdepicted herein are fictitious. No association with any real company, organization, product, domain name, email address,logo, person, place, or event is intended or should be inferred.This book expresses the author’s views and opinions. The information contained in this book is provided without anyexpress, statutory, or implied warranties. Neither the authors, Microsoft Corporation, nor its resellers, or distributorswill be held liable for any damages caused or alleged to be caused either directly or indirectly by this book.

ContentsExtending Packages with Scripting . 3Comparing the Script Task and the Script Component . 5Comparing Scripting Solutions and Custom Objects . 9Referencing Other Assemblies in Scripting Solutions . 10Debug a Script by Setting Breakpoints in a Script Task and Script Component . 12Extending the Package with the Script Task. 13Configuring the Script Task in the Script Task Editor . 15Coding and Debugging the Script Task . 17Using Variables in the Script Task . 25Connecting to Data Sources in the Script Task . 29Raising Events in the Script Task. 33Logging in the Script Task. 37Returning Results from the Script Task . 40Script Task Examples . 41Detecting an Empty Flat File with the Script Task . 43Gathering a List for the ForEach Loop with the Script Task. 47Querying the Active Directory with the Script Task . 55Monitoring Performance Counters with the Script Task. 58Working with Images with the Script Task . 62Finding Installed Printers with the Script Task . 70Sending an HTML Mail Message with the Script Task. 74Working with Excel Files with the Script Task . 78Sending to a Remote Private Message Queue with the Script Task . 92Extending the Data Flow with the Script Component . 95Configuring the Script Component in the Script Component Editor . 97Coding and Debugging the Script Component . 102Understanding the Script Component Object Model. 109Using Variables in the Script Component . 115Connecting to Data Sources in the Script Component . 117Raising Events in the Script Component. 119Logging in the Script Component. 122Developing Specific Types of Script Components . 123Creating a Source with the Script Component . 124Creating a Synchronous Transformation with the Script Component. 136Creating an Asynchronous Transformation with the Script Component. 145Creating a Destination with the Script Component. 153Additional Script Component Examples . 164

Simulating an Error Output for the Script Component . 165Enhancing an Error Output with the Script Component . 168Creating an ODBC Destination with the Script Component. 171Parsing Non-Standard Text File Formats with the Script Component . 176

Extending Packages with ScriptingIf you find that the built-in components Integration Services do not meet your requirements,you can extend the power of Integration Services by coding your own extensions. You have twodiscrete options for extending your packages: you can write code within the powerful wrappersprovided by the Script task and the Script component, or you can create custom IntegrationServices extensions from scratch by deriving from the base classes provided by the IntegrationServices object model.This section explores the simpler of the two options — .The Script task and the Script component let you extend both the control flow and the data flowof an Integration Services package with very little coding. Both objects use the Microsoft VisualStudio Tools for Applications (VSTA) development environment and the Microsoft Visual Basicor Microsoft Visual C# programming languages, and benefit from all the functionality offered bythe Microsoft .NET Framework class library, as well as custom assemblies. The Script task and theScript component let the developer create custom functionality without having to write all theinfrastructure code that is typically required when developing a custom task or custom data flowcomponent.In This SectionComparing the Script Task and the Script ComponentDiscusses the similarities and differences between the Script task and the Script component.Comparing Scripting Solutions and Custom ObjectsDiscusses the criteria to use in choosing between a scripting solution and the development ofa custom object.Using Other Assemblies in Scripting SolutionsDiscusses the steps required to reference and use external assemblies and namespaces in ascripting project.Extending the Package with the Script TaskDiscusses how to create custom tasks by using the Script task. A task is typically called onetime per package execution, or one time for each data source opened by a package.Extending the Data Flow with the Script ComponentDiscusses how to create custom data flow sources, transformations, and destinations byusing the Script component. A data flow component is typically called one time for each rowof data that is processed.3

ReferenceIntegration Services Error ReferenceLists the predefined Integration Services error codes with their symbolic names anddescriptions.Related SectionsExtending Packages with Custom ObjectsDiscusses how to create program custom tasks, data flow components, and other packageobjects for use in multiple packages.Working with Packages ProgrammaticallyDescribes how to create, configure, run, load, save, and manage Integration Servicespackages programmatically.Stay Up to Date with IntegrationServicesFor the latest downloads,articles, samples, and videosfrom Microsoft, as well asselected solutions from thecommunity, visit the IntegrationServices page on MSDN:Visit the IntegrationServices page onMSDNFor automatic notification ofthese updates, subscribe to theRSS feeds available on the page.See AlsoSQL Server Integration Services (SSIS)4

Comparing the Script Task and the ScriptComponentThe Script task, available in the Control Flow window of the Integration Services designer, andthe Script component, available in the Data Flow window, have very different purposes inan Integration Services package. The task is a general-purpose control flow tool, whereas thecomponent serves as a source, transformation, or destination in the data flow. Despite theirdifferent purposes, however, the Script task and the Script component have some similarities inthe coding tools that they use and the objects in the package that they make available to thedeveloper. Understanding their similarities and differences may help you to use both the taskand the component more effectively.Similarities between the Script Task and the Script ComponentThe Script task and the Script component share the following common features.FeatureDescriptionTwo design-time modesIn both the task and the component, youbegin by specifying properties in the editor,and then switch to the developmentenvironment to write code.Microsoft Visual Studio Tools forApplications (VSTA)Both the task and the component use thesame VSTA IDE, and support code writtenin either Microsoft Visual Basic or MicrosoftVisual C#.Precompiled scriptsBeginning in SQL Server 2008 IntegrationServices (SSIS), all scripts are precompiled.In earlier versions, you could specifywhether scripts were precompiled.The script is precompiled into binary code,permitting faster execution, but at the costof increased package size.DebuggingBoth the task and the component supportbreakpoints and stepping through codewhile debugging in the designenvironment. For more information,see Coding and Debugging the Script Taskand Coding and Debugging the ScriptComponent.5

Differences between the Script Task and the Script ComponentThe Script task and the Script component have the following noteworthy differences.FeatureScript TaskScript ComponentControlflow / DataflowThe Script task is configured on theControl Flow tab of the designer andruns outside the data flow of thepackage.The Script component is configured onthe Data Flow page of the designer andrepresents a source, transformation, ordestination in the Data Flow task.PurposeA Script task can accomplish almostany general-purpose task.You must specify whether you want tocreate a source, transformation, ordestination with the Script component.ExecutionA Script task runs custom code atsome point in the package workflow.Unless you put it in a loop containeror an event handler, it only runs once.A Script component also runs once, buttypically it runs its main processingroutine once for each row of data in thedata flow.EditorThe Script Task Editor has threepages: General, Script, andExpressions. Only theReadOnlyVariables andReadWriteVariables, andScriptLanguage properties directlyaffect the code that you can write.The Script Transformation Editor hasup to four pages: Input Columns, Inputsand Outputs, Script, and ConnectionManagers. The metadata and propertiesthat you configure on each of thesepages determines the members of thebase classes that are autogenerated foryour use in coding.Interactionwith thepackageIn the code written for a Script task,you use the Dts property to accessother features of the package. The Dtsproperty is a member of theScriptMain class.In Script component code, you use typedaccessor properties to access certainpackage features such as variables andconnection managers.The PreExecute method can access onlyread-only variables. The PostExecutemethod can access both read-only andread/write variables.For more information about thesemethods, see Coding and Debugging theScript Component.UsingvariablesThe Script task uses theP:Microsoft.SqlServer.Dts.Tasks.ScriptTThe Script component uses typedaccessor properties of the autogenerated6

FeatureScript TaskScript Componentask.ScriptObjectModel.Variablesbased class, created from thecomponent’sproperty of the Dts object to accessvariables that are available throughthe .ScriptTask.ReadOnlyVariables ptTask.ReadWriteVariablesproperties. For example:Dim myVar as StringmyVar ngstring ponent.ReadOnlyVariables ent.ReadWriteVariables properties.For example:Dim myVar as StringmyVar Me.Variables.MyStringVariablestring myVar;myVar this.Variables.MyStringVariable;myVar ;UsingThe Script task uses theconnections P:Microsoft.SqlServer.Dts.Tasks.ScriptTproperty of the Dts object to accessconnection managers defined in thepackage. For example:The Script component uses typedaccessor properties of the autogeneratedbase class, created from the list ofconnection managers entered by the useron the Connection Managers page of theeditor. For example:Dim myFlatFileConnection AsStringDim connMgr AsIDTSConnectionManager100myFlatFileConnection connMgr nnections("Test Flat ),IDTSConnectionManager100 connMgr;ask.ScriptObjectModel.ConnectionsconnMgr this.Connections.MyADONETConnection;String)string myFlatFileConnection;myFlatFileConnection (Dts.Connections["Test FlatFile7

FeatureScript TaskScript ction) as String);RaisingeventsThe Script task uses ptObjectModel.Events propertyof the Dts object to raise events. Forexample:Dts.Events.FireError(0, "EventSnippet",ex.Message &ControlChars.CrLf &ex.StackTrace,"", 0)Dts.Events.FireError(0, "EventSnippet", ex.Message "\r" ex.StackTrace, "", 0);LoggingThe Script task uses theThe Script component raises errors,warnings, and informational messages byusing the methods of ComponentMetaData100 interfacereturned by ent.ComponentMetaData property.For example:Dim myMetadata asIDTSComponentMetaData100myMetaData Me.ComponentMetaDatamyMetaData.FireError(.)The Script component uses theM:Microsoft.SqlServer.Dts.Tasks.ScriptT ptObjectModel.Log(System.Stri nt32,System.Byte[]) method ,System.Byte[]) method of theof the Dts object to log information to autogenerated base class to logenabled log providers. For example:information to enabled log providers. Forexample:Dim bt(0) As ByteDts.Log("Test Log Event",0,bt)byte[] bt new byte[0];Dts.Log("Test Log Event", 0,bt);[Visual Basic]Dim bt(0) As ByteMe.Log("Test Log Event",0,bt)byte[] bt new byte[0];this.Log("Test Log Event", 0,bt);ReturningresultsThe Script task uses both ptObjectModel.TaskResultproperty and the optionalThe Script component runs as a part ofthe Data Flow task and does not reportresults using either of these properties.8

FeatureScript TaskScript k.ScriptObjectModel.ExecutionValueproperty of the Dts object to notifythe runtime of its results.Stay Up to Date with IntegrationServicesFor the latest downloads,articles, samples, and videosfrom Microsoft, as well asselected solutions from thecommunity, visit the IntegrationServices page on MSDN:Visit the IntegrationServices page onMSDNFor automatic notification ofthese updates, subscribe to theRSS feeds available on the page.See AlsoExtending the Package with the Script TaskExtending the Data Flow with the Script ComponentComparing Scripting Solutions and CustomObjectsAn Integration Services Script task or Script component can implement much of the samefunctionality that is possible in a custom managed task or data flow component. Here are someconsiderations to help you choose the appropriate type of task for your needs: If the configuration or functionality is specific to an individual package, you should use theScript task or the Script component instead of a developing a custom object.9

If the functionality is generic, and might be used in the future for other packages and byother developers, you should create a custom object instead of using a scripting solution.You can use a custom object in any package, whereas a script can be used only in thepackage for which it was created. If the code will be reused within the same package, you should consider creating a customobject. Copying code from one Script task or component to another leads to reducedmaintainability by making it more difficult to maintain and update multiple copies of thecode. If the implementation will change over time, consider using a custom object. Custom objectscan be developed and deployed separately from the parent package, whereas an updatemade to a scripting solution requires the redeployment of the whole package.Stay Up to Date with IntegrationServicesFor the latest downloads,articles, samples, and videosfrom Microsoft, as well asselected solutions from thecommunity, visit the IntegrationServices page on MSDN:Visit the IntegrationServices page onMSDNFor automatic notification ofthese updates, subscribe to theRSS feeds available on the page.See AlsoExtending Packages with Custom ObjectsReferencing Other Assemblies in ScriptingSolutionsThe Microsoft .NET Framework class library provides the script developer with a powerful set oftools for implementing custom functionality in Integration Services packages. The Script taskand the Script component can also use custom managed assemblies.10

NoteTo enable your packages to use the objects and methods from a Web service, use theAdd Web Reference command available in Microsoft Visual Studio Tools forApplications (VSTA). In earlier versions of Integration Services, you had to generate aproxy class to use a Web service.Using a Managed AssemblyFor Integration Services to find a managed assembly at design time, you must do the followingsteps:1. Store the managed assembly in any folder on your computer.NoteIn earlier versions of Integration Services, you could only add a reference to amanaged assembly that was stored in the%windir%\Microsoft.NET\Framework\vx.x.xxxxx folder or the%ProgramFiles%\Microsoft SQL Server\100\SDK\Assemblies folder.2. Add a reference to the managed assembly.To add the reference, in VSTA, in the Add Reference dialog box, on the Browse tab, locateand add the managed assembly.For Integration Services to find the managed assembly at run time, you must do the followingsteps:1. Sign the managed assembly with a strong name.2. Install the assembly in the global assembly cache on the computer on which the package isrun.For more information, see Building, Deploying, and Debugging Custom Objects.Using the Microsoft .NET Framework Class LibraryThe Script task and the Script component can take advantage of all the other objects andfunctionality exposed by the .NET Framework class library. For example, by using the .NETFramework, you can retrieve information about your environment and interact with thecomputer that is running the package.This list describes several of the more frequently used .NET Framework classes: System.Data Contains the ADO.NET architecture. System.IO Provides an interface to the file system and streams. System.Windows.Forms Provides form creation. System.Text.RegularExpressions Provides classes for working with regular expressions. System.Environment Returns information about the local computer, the current user, andcomputer and user settings. System.Net Provides network communications.11

System.DirectoryServices Exposes Active Directory. System.Drawing Provides extensive image manipulation libraries. System.Threading Enables multithreaded programming.For more information about the .NET Framework, see the MSDN Library.Stay Up to Date with IntegrationServicesFor the latest downloads,articles, samples, and videosfrom Microsoft, as well asselected solutions from thecommunity, visit the IntegrationServices page on MSDN:Visit the IntegrationServices page onMSDNFor automatic notification ofthese updates, subscribe to theRSS feeds available on the page.See AlsoDebug a Script by Setting Breakpoints in a ScriptTask and Script ComponentThis procedure describes how to set breakpoints in the scripts that are used in the Script taskand Script component.After you set breakpoints in the script, the Set Breakpoints - object name dialog box liststhe breakpoints, along with the built-in breakpoints.ImportantUnder certain circumstances, breakpoints in the Script task and Script component areignored. For more information, see the Debugging the Script Task section in Codingand Debugging the Script Task and the Debugging the Script Component sectionin Coding and Debugging the Script Component.12

ProceduresTo set a breakpoint in script1. In SQL Server Data Tools (SSDT), open the Integration Services project that contains thepackage you want.2. Double-click the package that contains the script in which you want to set breakpoints.3. To open the Script task, click the Control Flow tab, and then double-click the Scripttask.4. To open the Script component, click the Data Flow tab, and then double-click theScript component.5. Click Script and then click Edit Script.6. In Microsoft Visual Studio Tools for Applications (VSTA), locate the line of script onwhich you want to set a breakpoint, right-click that line, point to Breakpoint, and thenclick Insert Breakpoint.The breakpoint icon appears on the line of code.7. On the File menu, click Exit.8. Click OK.9. To save the package, click Save Selected Items on the File menu.Extending the Package with the Script TaskThe Script task extends the run-time capabilities of Microsoft Integration Services packages withcustom code written in Microsoft Visual Basic or Microsoft Visual C# that is compiled andexecuted at package run time. The Script task simplifies the development of a custom run-timetask when the tasks included with Integration Services do not fully satisfy your requirements.The Script task writes all the required infrastructure code for you, letting you focus exclusively onthe code that is required for your custom processing.A Script task interacts with the containing package through the global Dts object, an instance ofthe bjectModel class that is exposed in thescripting environment. You can write code in a Script task that modifies the values stored inIntegration Services variables; later, the package can use those updated values to determine thepath of its workflow. The Script task can also use the Visual Basic namespace and the .NETFramework class library, as well as custom assemblies, to implement custom functionality.The Script task and the infrastructure code that it generates for you simplify significantly theprocess of developing a custom task. However, to understand how the Script task works, youmay find it useful to read the section Extending the Control Flow with Custom Tasks tounderstand the steps that are involved in developing a custom task.13

If you are creating a task that you plan to reuse in multiple packages, you should considerdeveloping a custom task instead of using the Script task. For more information, see ComparingScripting Solutions and Custom Objects.In This SectionThe following topics provide more information about the Script task.Configuring the Script TaskExplains how the properties that you configure in the Script Task Editor affect thecapabilities and the performance of the code in the Script task.Coding the Script TaskExplains how to use Microsoft Visual Studio Tools for Applications (VSTA) to develop thescripts that are contained in the Script task.Using Variables in the Script TaskExplains how to use variables through ptObjectModel.Variablesproperty.Using Connections in the Script TaskExplains how to use connections through ptObjectModel.Connectionsproperty.Raising Events in the Script TaskExplains how to raise events through ptObjectModel.Events property.Logging in the Script TaskExplains how to log information through m.Byte[]) method.Returning Results from the Script TaskExplains how to return results through the .ScriptObjectModel.TaskResult criptObjectModel.ExecutionValueproperty.Script Task ExamplesProvides simple examples that demonstrate several possible uses for a Script task.14

Stay Up to Date with IntegrationServicesFor the latest downloads,articles, samples, and videosfrom Microsoft, as well asselected solutions from thecommunity, visit the IntegrationServices page on MSDN:Visit the IntegrationServices page onMSDNFor automatic notification ofthese updates, subscribe to theRSS feeds available on the page.See AlsoScript TaskComparing the Script Task and the Script ComponentConfiguring the Script Task in the Script Task EditorBefore you write custom code in the Script task, you configure its principal properties in thethree pages of the Script Task Editor. You can configure additional task properties that are notunique to the Script task by using the Properties window.NoteUnlike earlier versions where you could indicate whether scripts were precompiled, allscripts are precompiled beginning in SQL Server 2008 Integration Services (SSIS).General Page of the Script Task EditorOn the General page of the Script Task Editor, you assign a unique name and a description forthe Script task.Script Page of the Script Task EditorThe Script page of the Script Task Editor displays the custom properties of the Script task.ScriptLanguage PropertyMicrosoft Visual Studio Tools for Applications (VSTA) supports the Microsoft Visual Basic orMicrosoft Visual C# programming languages. After you create a script in the Script task, youcannot change value of the ScriptLanguage property.15

To set the default script language for Script tasks and Script components, use theScriptLanguage property on the General page of the Options dialog box. For moreinformation, see Select Variables Page (VSTA).EntryPoint PropertyThe EntryPoint property specifies the method on the ScriptMain class in the VSTA project thatthe Integration Services runtime calls as the entry point into the Script task code. TheScriptMain class is the default class generated by the script templates.If you change the name of the method in the VSTA project, you must change the value of theEntryPoint property.ReadOnlyVariables and ReadWriteVariables PropertiesYou can enter comma-delimited lists of existing variables as the values of these properties tomake the variables available for read-only or read/write access within the Script task code.Variables of both types are accessed in code through ptObjectModel.Variables property of the Dtsobject. For more information, see Using Variables in the Script Task.NoteVariable names are case-sensitive.To select the variables, click the ellipsis ( ) button next to the property field. For moreinformation, see Select Variables Page.Edit Script ButtonThe Edit Script button launches the VSTA development environment in which you write yourcustom script. For more information, see Coding the Script Task.Expressions Page of the Script Task EditorOn the Expressions page of the Script Task Editor, you can use expressions to provide valuesfor the properties of the Script task listed above and for many other task properties. For moreinformation, see Using Expressions in Packages.16

Stay Up to Date with IntegrationServicesFor the latest downloads,articles, samples, and videosfrom Microsoft, as well asselected solutions from thecommunity, visit the IntegrationServices page on MSDN:Visit the IntegrationServices page onMSDNFor automatic notification ofthese updates, subscribe to theRSS feeds a

Integration Services: Extending Packages with Scripting SQL Server 2012 Books Online Summary: You can extend the power of Integration Services (SSIS) by adding code within the wrappers provided by the Script task and the Script component. This section of the Developer Reference provides instructions and examples for extending the