How to create a DTS task via Visual C # .NET
First, summary
This article describes how to create a DTS custom task using Visual C # .NET. You can extend the functionality of the DTS by creating custom tasks through C # .NET. You can then install and register the task, and he speaks in the DTS design, just like the default DTS task. In short, you can create custom tasks using .NET Framework.
In addition to creating a DTS custom task, this article also includes some of the content:
1. The custom code in this article is divided into compilation, registration, and installation of custom tasks;
2, this part of a distinctive part is that you can run custom tasks;
3. In the development process you can use some of these tools (unless otherwise stated, these tools are included in .NET, you can run these tools through the .NET command line).
Second, create a timely package for dtsspkg.dll
If a Microsoft's NET client accesss a COM component, you must use a package (this component is included). This type of package is a timely running package (RCW) and you can also compile by open DTSPKG.DLL. You can also compile RCWs using the Type Library Export Tool (TLBIMP.exe), such as:
Tlbimp.exe "C: / ProgramFiles / Microsoft SQLSERVER / 80 / TOOLS / BINN / DTSPKG.DLL" /out:microsoft.sqlservver.dtspkg80.dll/keyfile: dtspkg.snk
The "/ keyfile" parameter represents Microsoft.SQLServer.dtspkg80.dll with strong type name Public or Private keyword. Create keywords before dtspkg.snk using strong type name tool (sn.exe)
sn.exe -k dtspkg.snk
You should use a strong type of name like a global collection cache because you have installed the run package.
Install the run package in the global collection cache
Install the running package with the global set cache tool (Gacutil.exe):
Gacutil.exe / I Microsoft.Sqlserver.dtspkg80.dll
After installing the run package, you can add the same addition. Netc # project.
Add code for custom tasks
Custom registration of code. .NET does not open DLLREGINSTERSERVER and DLLUNREGISTERSERVER Like COM components, you can use ComregIsterFunctionAttribute class to perform task registration and revocation registration. Add the following code before the custom class declaration:
[GUID ("A39847F3-5845-4459-A25E-DE73A8E3CD48"), Comvisible (TRUE)]
[PROGID ("DTS.SIMPLETASK")]]]
Public Class SimpleTask: CustomTask
{
//Mplementation of custom Task
}
The following code is an example of a function registration. The full code of the function is compiled, registered, and installed parts of custom tasks.
[System.Runtime.InteropServices.comRegisterFunctionAttribute ()]
Static void RegisterServer (Type T)
{
// Code to Register Custom Task
}
The registration function adds the following key value to register.
HKEY_CLASS_ROOT / CLSID / A39847F3-5845-4459-A25E-DE73A8E3CD48 / Implement Categories / {10020200-EB1C-11CF-AE6E-00AA004A34D5}
10020200-EB1C-11CF-AE6E-00AA004A34D5 is the class number of the DTS package object. Since all custom tasks perform custom interfaces, you must register. Registration function Add the following registration key value: hkey_current_user / Software / Microsoft / Microsoft SQL Server / 80 / DTS / ENUMERATION / TASKS / A39847F3-5845-4459-a25E-de73a8e3cd48
The following DTS task caching the directory list so that the custom task appears in the DTS designer:
HKEY_CURRENT_USER / SOFTWARE / Microsoft / Microsoft SQL Server / 80 / DTS / Enumeration / Tasks /
The following code demonstrates the execution of the task of the non-registration function. The registration function is part of the ComunRegisterFunctionAttribute class in the .NET running library. Want to browse the full code of this function, you can see the "Compile, Registration, and Install Custom Tasks" section:
[System.Runtime.Interopservices.comunregisterFunctionATTRIBUTE ()]
Static void unregisterServer (Type T)
{
// CODE to Unregister Custom Task
}
The registration function removes the task from the DTS task cache by removing the following key values from the registry
HKEY_CURRENT_USER / SOFTWARE / Microsoft / Microsoft SQL Server / 80 / DTS / ENUMERATION / TASKS / A39847F3-5845-4459-A25E-DE73A8E3CD48
Finally, custom tasks are open like the Dual_Interface COM component. Create a default interface from all PUBLICs, non-static fields, properties, and methods of all classes. The following line of code is after the USING application in the custom task source file:
[Assembly: ClassInterface (ClassInterfacePe.autodu)]
This part of the code has been fully listed.
Increase functional custom task
This article "Compile, Registration, and Install Custom Tasks" section contains a simple DTS custom task code. The task has two properties: Name and Description, the value of the Description attribute will appear in the message box. This example describes a minimized code you can use existing DTS definition tasks. However, you can create a user interface by performing a CustomTaskUI interface, but that is not discussed. By performing a custom interface, the DTS designer creates a default vision interface for custom tasks.
All DTS custom tasks perform a custom task interface. Custom user interface is made from two properties, a collection and a method:
1, Name and Description attribute;
2, Properties set;
3, Execute method.
All custom tasks should perform attributes, attribute sets, and Execute methods.
Compile, registration, and install custom tasks
Using system;
Using system.Runtime.InteropServices;
USING Microsoft.SqlServer.dtspkg80;
USING Microsoft.win32;
Using system.windows.forms;
[Assembly: ClassInterface (ClassInterfacePe.autodu)]
Namespace DTS
{
[GUID ("38ed4f80-9ef4-4752-8478-65d2db3ba7dd"), comVisible (true)] // guid is created by useing guidgen.exe
[PROGID ("DTS.SIMPLECUSTOSK")] Public Class SimpleCustomTask: CustomTask
{
PRIVATE STRING NAME;
Private string description;
Public SimpleCustomTask ()
{
Name = ""
Description = "SimpleCustom Tomtask Description";
}
Public void Execute (Object Ppackage, Object PpackageEvents, Object PpackageLog, Ref Microsoft.Sqlser.dtspkg80.dtstaskexecResult PTASKRESULT)
{
// Assume Failure at the outset
PtaskResult = dtstaskexecResult.dtStaskexecResult_Failure;
Try
{
Package2 package = (package2) ppackage;
PackageEvents packageevents = (packageevents) PPackageEvents;
Packagelog packagelog = (packagelog) PPackagelog;
MessageBox.Show (Description);
}
// first catch com exceptions, and the all Other Exceptions
Catch (System.Runtime.Interopservices.Comexception E)
{
Console.writeline (e);
}
Catch (System.exception E)
{
Console.writeline (e);
}
// Return SUCCESS
PtaskResult = dtstaskexecResult.dtStaskexecResult_suCcess;
}
Public String Description, PUBLIC STRING DESCRIPTION
{
Get {return this.description;
Set {this.description = value;
}
Public String Name
{
Get {return name;}
Set {this.name = value;
}
Public microsoft.sqlser.dtspkg80.properties Properties
{
Get {return null;}
}
[System.Runtime.Interopservices.comVisible (false)]
OVERRIDE PUBLIC STRING TOSTRING ()
{
Return Base.toString ();
}
// registration function for custom.
[System.Runtime.InteropServices.comRegisterFunctionAttribute ()]
Static void RegisterServer (Type T)
{
Try
{
Const string task_cache = "Software // Microsoft // Microsoft SQL Server // 80 // DTS // ENUMERATION / / TASKS";
Const string catid_dtscustomtask = "{10020200-EB1C-11CF-AE6E-00AA004A34D5}";
String Guid = "{" t.guid.toString () "}"; guid = guid.toupper ();
Console.writeline ("RegisterServer {0}", Guid);
RegistryKey root;
RegistryKey RK;
RegistryKey NRK;
// Add com category in hkey_classes_root
Root = registry.classessroot;
RK = root.opensubkey ("CLSID //" Guid "// Implement Categories", TRUE
NRK = rk.createSubkey (catid_dtscustomtask);
NRK.CLOSE ();
RK.Close ();
Root.close ();
// add to dts cache in hkey_current_user
Root = registry.currentuser;
RK = root.opensubkey; Task_Cache, TRUE;
NRK = RK.CREATESUBKEY (GUID);
NRK.SetValue ("", t.fullname);
NRK.CLOSE ();
RK.Close ();
Root.close ();
SimpleCustomTask CT = New SimpleCustomTask ();
Root = registry.classessroot;
RK = root.opensubkey ("CLSID //" Guid, True);
RK.SetValue ("DTSTASKDESCRIPTION", CT.DESCRIPTION;
NRK.CLOSE ();
RK.Close ();
Root.close ();
}
Catch (Exception E)
{
System.console.writeline (e.tostring ());
}
}
// Unregistration function for custom task.
[System.Runtime.Interopservices.comunregisterFunctionATTRIBUTE ()]
Static void unregisterServer (Type T)
{
Try
{
Const string task_cache = "Software // Microsoft // Microsoft SQL Server // 80 // DTS // ENUMERATION / / TASKS";
String Guid = "{" T.Guid.toTString () "}";
Guid = guid.toupper ();
Console.writeline ("UnregisterServer {0}", Guid);
RegistryKey root;
RegistryKey RK;
// delete from DTS Cache in HKEY_CURRENT_USER
Root = registry.currentuser;
RK = root.opensubkey; Task_Cache, TRUE;
RK.DELETESUBKEY (GUID, FALSE);
RK.Close ();
Root.close ();
}
Catch (Exception E)
{
System.console.writeline (e.tostring ());
}
}
}