The DTS Object Model
- Package2 Object
- Connection2 Object
- Task Object
- Step Object
- Summary
In this second article of the DTS series, we introduce you to the main objects of the DTS Object Model and their properties. We will delve into each major object and show the code for a simple yet complete DTS Package. There are numerous properties you can set for each DTS object, and discussing each of them in detail isn't feasible for this article. Instead, we concentrate on the major properties that you'll need to set in almost every package. Please refer to Microsoft SQL Server Books On Line (BOL) for additional information.
Many DTS Packages can be designed using the DTS designer and executed in the same manner every time. But if any of the details are not known at design time, it may be easier to design a package that can obtain needed pieces of information at runtime.
Many DTS Packages will be built directly in Visual Basic (VB) code. The VB component can create a package that will transfer and transform the data. This way, you can store information in a database or pass it to a VB component at runtime. Storing the information in the database allows you to set different parameters within each database, and use the same component to build and run the package.
As we build the sample package for this article, you can follow along if you want by building your own package. You can download all of the code as well. If you will build one from scratch, you must add the following references in your VB project: Microsoft DTSPackage Object Library, Microsoft DTS Custom Tasks Object Library, and Microsoft DTSDataPump Scripting Object Library. Your package may not need the last two, depending on your package, but we show all of them for your convenience.
Package2 Object
The topmost object in the DTS object model is the Package2 object. All other DTS objects are related to the Package object. For example, if you add a connection to SQL server, the Connection object must be told that it is a part of a specific package. So let's start by examining the Package object more closely.
Many parameters of the Package2 object will remain the same from one run to the next on a specific system, so we can hard-code properties that are not likely to change. The PackagePriorityClass property sets the thread priority for the package in the operating system. Microsoft has provided three constants that can be used to set the value of this property: DTSPackagePriorityClass_High, DTSPackagePriorityClass_Low, and DTSPackagePriorityClass_Normal. If you have packages that you want to run at different priorities, then you can add the logic in the code to set priorities accordingly; otherwise, we can just hard code the setting as follows:
.PackagePriorityClass = DTSPriorityClass_Normal
Data lineage is used to track meta data about the objects used in the package. You can use the LineageOptions property to tell how the data lineage will be written. Again you have several constants to choose from: DTSLineage_AddLineageVariables, DTSLineage_None (default), DTSLineage_WriteToReposIfAvailable, and DTSLineage_WriteToReposRequired.
Look for "data lineage" in the index of (BOL) for more information about using data lineage. In our example, we hard-code this value: LineageOptions = DTSLineage_None. The TransactionIsolationLevel is used to set the isolation level if the UseTransaction property is set to true. We have the four standard SQL Server Isolation Levels, plus we get several additional ones to choose from for non-SQL Server databases (see Table 1 for the available constants). Set yours to whatever meets your needs for your specific package.
We set ours like this:
.TransactionIsolationLevel = DTSIsoLevel_ReadUncommitted
We chose the ReadUncommited level because we're only exporting data to a file in our example.
Table 1 DTS Transaction Isolation Level Constants
TransactionIsolationLevel |
Description |
DTSIsoLevel_ReadCommitted |
Prevents dirty reads and allows phantom reads. |
DTSIsoLevel_ReadUncommitted |
No shared locks are issued; exclusive locks are ignored. Least restrictive. |
DTSIsoLevel_RepeatableRead |
All data is locked, but allows inserts by other users. |
DTSIsoLevel_Serializable |
Prevents all other user access, insert, update, or delete. Most restrictive. |
DTSIsoLevel_Browse |
Similar to ReadUncommitted; not for SQL Server. |
DTSIsoLevel_Chaos |
You can see uncommitted changes by other users, but update locks are not held to the end of the transaction. Rollbacks are not allowed. Not for SQL Server. |
DTSIsoLevel_CursorStability |
Locks a single row of data as rows are FETCHed using a browse cursor. |
DTSIsoLevel_Isolated |
Similar to Serializable. Not for SQL Server. |
The RepositoryMetadataOptions property has to do with scanning database objects into meta data services. We don't need to adjust it for this example because we don't intend to store meta data about the sample package, so set it to the default:
.RepositoryMetadataOptions = DTSReposMetadata_Default
We also have several constants that we can choose for this property.
In SQL Server 2000, we have two kinds of authentication: Windows and SQL Server security. We indicate the type of authentication we are using with the LogServerFlags property. This property sets or returns the kind of authentication to be used for a package. We'll use the SQL Server authentication and set it in the following manner:
.LogServerFlags = DTSSQLStgFlag_Default
If you want to use Windows authentication, use the DTSSQLStgFlag_UseTrustedConnection constant. Because there are several ways that a DTS package can be created, we have a property that lets us specify how the package was created. The PackageType property indicates how the DTS package was created; we'll set ours to .PackageType = DTSPkgType_Default to indicate that it was a custom package.
The MaxConcurrentSteps property can be used to tell the package how many steps may be ran simultaneously. If the system has only one CPU, then setting this value higher than one has no effect. Set this value to a value up to the number of CPUs on the system that the package will run on. If the package will run on different computers, then having this value set higher than the number of processors has no effect, so you can set it up to the number of processors on the computer that has the most processors.
BOL has information on all of the properties we use, and much more. It also contains a list of all the predefined constants that are available for each property. You can also see the available properties through your intellisense in VB. To find the properties in the BOL, review the Package and Package2 objects.
If you see an object with a 2 on the end, it just means that it is a DTS 2000 object and cannot be used in earlier DTS versions.
We can create a method to set these values, and pass in any of the values that change during each execution of the package. Most of these values will remain the same for a particular system, so many of them can be set at design time. Also, the Description property is not needed, but I like to give a short description, so that if I save the package, I can read that description for all of the packages at once from the Local Packages section within Enterprise Manager. Actually, we can make many of the object creation processes general enough to put them into a method, which saves us from writing out all of the properties each time we want to create a new object. It also allows our objects to be consistent in how they are created.