Use Visual C #. Net to create a DTS task

Source: Internet
Author: User

Use Visual C #. Net to create a DTS task
 
 

This document describes how to use Visual C #. Net to create a custom DTS task. You can use C #. Net to create custom tasks to extend the data transmission function. After that, you can install and register the task. It shows the DTs design, just like the default DTS task. In short, you can use. NET Framework to create custom tasks.

In addition to creating a DTS custom task, this article also includes the following parts:

1. The custom code in this article is divided into compilation, registration, and installation of custom tasks;

2. You can run custom tasks;

3. You can use the tools mentioned in this article during development (unless otherwise stated, these tools are already included in... net ).

Create a timely package for dtsspkg. dll

If a COM component is accessed based on a Microsoft. NET client, you must use a package (Included in this component ). This type of package is a timely running package (RCW) And you can also compile it by opening the dtspkg. dll Type Library. You can also use the export library tool (tlbimp.exe) to compile RCW, for example:

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 a strong type name using the public or private keyword. Use the strong alias tool (sn.exe) to create a keyword before dtspkg. SNK:

Sn.exe-K dtspkg. SNK

You should use a strong type name like other global set cache, because you have installed the runtime package.

Install the package in the global set Cache

Run the following command to install the package:

Gacutil.exe/I Microsoft. sqlserver. dtspkg80.dll

After the running package is installed, you can add it like a reference in the. NETC # project.
  
Add code for a custom task

Custom registration of code .. Net does not open the portal for the dllreginsterserver and dllunregisterserver components like Com, but you can use the comregisterfunctionattribute class to execute task registration and cancel 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

{

// Implementation of M Task

}

The following code is an example of function registration. All the code of the function is included in the compilation, registration, and installation of the custom task.

[System. runtime. interopservices. comregisterfunctionattribute ()]

Static void registerserver (type T)

{

// Code to register custom task

}

Add the following key values to the registration function.

Hkey_classes_root/CLSID/A39847F3-5845-4459-A25E-DE73A8E3CD48/implemented categories/{10020200-eb1c-11cf-ae6e-00aa004a34d5}

10020200-eb1c-11cf-ae6e-00aa004a34d5 is the class number of the data transmission package object. Registration is required because all custom tasks execute custom interfaces. Add the following registration key value to the registration function:

HKEY_CURRENT_USER/software/Microsoft SQL Server/80/DTS/enumeration/tasks/A39847F3-5845-4459-A25E-DE73A8E3CD48

The following DTS task cache directory list displays the custom task in the DTs designer:

HKEY_CURRENT_USER/software/Microsoft SQL Server/80/DTS/enumeration/tasks/

The following code demonstrates the execution of a non-registered function task removal. The facial registration function is part of the comunregisterfunctionattribute class in the. NET Runtime Library. To view the complete code of this function, you can refer to the "compile, register, and install custom tasks" section:

[System. runtime. interopservices. comunregisterfunctionattribute ()]

Static void unregisterserver (type T)

{

// Code to unregister custom task

}

The registration-free function removes the following key values from the DTs task cache from the registry.

HKEY_CURRENT_USER/software/Microsoft SQL Server/80/DTS/enumeration/tasks/A39847F3-5845-4459-A25E-DE73A8E3CD48

Finally, custom tasks are opened like the dual_interface COM component. You can create a default Interface from the public, non-static fields, attributes, and methods of all classes. Run the following code after using is applied to the custom task source file:

[Assembly: classinterface (classinterfacetype. autodual)]

This part of the code has been fully listed.

Add a functional custom task

The "compile, register, and install custom tasks" section in this article contains a simple DTS custom task code. The task has two attributes: Name and description. The value of the description attribute appears in the message box. This example describes a minimal code. You can use an existing functional DTS to define a task. However, you can create a user interface by executing the customtaskui interface, but that is not discussed. By executing only custom interfaces, the DTs designer creates a default user interface for custom tasks.

All DTS custom tasks execute custom task interfaces. A Custom User Interface consists of two attributes, one set and one method:

1. Name and description attributes;

2. properties set;

3. Execute method.

All custom tasks should execute attributes, attribute sets, and execute methods.

Compile, register, and install custom tasks

Using system;
Using system. runtime. interopservices;
Using Microsoft. sqlserver. dtspkg80;
Using Microsoft. Win32;
Using system. Windows. forms;

[Assembly: classinterface (classinterfacetype. autodual)]

Namespace DTS
{
[GUID ("38ed4f80-9ef4-4752-8478-65d2db3ba7dd"), comvisible (true)] // GUID is created by using guidgen. exe
[Progid ("DTS. simplecustomtask")]
Public class simplecustomtask: customtask
{
Private string name;
Private string description;
Public simplecustomtask ()
{
Name = "";
Description = "simplecustomtask description ";
}
Public void execute (Object ppackage, object ppackageevents, object ppackagelog, ref Microsoft. sqlserver. dtspkg80.dtstaskexecresult ptaskresult)

{
// Assume failure at the outset
Ptaskresult = dtstaskexecresult. dtstaskexecresult_failure;
Try
{
Package2 package = (package2) ppackage;
Packageevents = (packageevents) ppackageevents;
Packagelog = (packagelog) ppackagelog;
MessageBox. Show (description );
}
// First catch com exceptions, and then 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
{
Get {return this. description ;}
Set {This. Description = value ;}
}

Public string name
{
Get {return name ;}
Set {This. Name = value ;}
}

Public Microsoft. sqlserver. dtspkg80.properties Properties
{
Get {return NULL ;}
}

[System. runtime. interopservices. comvisible (false)]
Override Public String tostring ()
{
Return base. tostring ();
}

// Registration function for custom task.
[System. runtime. interopservices. comregisterfunctionattribute ()]
Static void registerserver (type T)
{
Try
{
Const string task_cache = "software // 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. classesroot;
Rk = root. opensubkey ("CLSID //" + guid + "// implemented 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 simplesimplemtask ();
Root = registry. classesroot;
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 SQL Server // 80 // DTS // enumeration // tasks ";
String guid = "{" + T. guid. tostring () + "}";
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 ());
}
}
}

}
Http://hexun.com/metababy

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.