Through Visual C #. NET to build a DTS task

Source: Internet
Author: User
Tags command line execution microsoft sql server
Visual article describes how to use visual c#.net to create a DTS customization task. You can extend DTS functionality in a way that c#.net create custom tasks. You can then install and register the task, and he speaks the DTS design which is now, like the default DTS task. In short, you can use the. NET framework to create a custom task.

In addition to creating a DTS Customization task in this article, there are some things to include:

1, the custom code in this article is divided into compiling, registering and installing custom tasks;

2, this part of a distinctive part is that you can run a custom task;

3, in the development process you can use some of the tools mentioned in this article (unless otherwise noted, these tools are already included in the. NET, you can pass. NET to run these tools on the command line.

Create a timely package for DTSSpkg.dll

If one is based on Microsoft. NET client to access a COM component, you must use a package (which the component contains). This kind of package is a just-in-time running package (RCW) and you can also compile it through an open Dtspkg.dll type library. You can also compile the RCW 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 a strong type name with public or private keywords. Use the strongly typed name tool (Sn.exe) to create a keyword before dtspkg.snk:

Sn.exe–k DTSPkg.snk

You should use a strongly typed name like the other global collection cache because you installed the run package.

Installing the run package in the Global collection cache

To install the run package with the Global Collection Cache tool (GaCutil.exe):

gacutil.exe/i Microsoft.SQLServer.DTSPkg80.dll

After installing the run package, you can add it like. Netc# the same additions as references in the project.
  
Add code for a custom task

Custom registration for code. NET does not open Dllreginsterserver and dllunregisterserver like COM components, but you can use the ComRegisterFunctionAttribute class to perform task registration and unregister. Add the following code before customizing the class declaration:

[Guid (' a39847f3-5845-4459-a25e-de73a8e3cd48 '), ComVisible (true)]

[ProgId (DTS). Simpletask ")]

public class Simpletask:customtask

{

Implementation of custom Task

}

The following code is an example of a function registration execution. The entire code of the function is in the compilation, registration, and Installation section of the custom task.

[System.Runtime.InteropServices.ComRegisterFunctionAttribute ()]

static void RegisterServer (Type t)

{

Code to register custom task

}

The registration function adds the following key value for registration.

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 DTS package object. Because all custom tasks perform custom interfaces, they must be registered. The registration function adds the following registry key value:

HKEY_CURRENT_USER\Software\Microsoft\Microsoft SQL server\80\dts\enumeration\tasks\ A39847f3-5845-4459-a25e-de73a8e3cd48

The following DTS task caches a list of directories so that customized tasks appear in DTS Designer:

HKEY_CURRENT_USER\Software\Microsoft\Microsoft SQL server\80\dts\enumeration\tasks\

The following code demonstrates the execution of a task that is not a registered function. The face registration function is part of the Comunregisterfunctionattribute class in the. NET Runtime. To view the complete code for this function, you can see the "compiling, registering, and Installing Custom Tasks" section:

[System.Runtime.InteropServices.ComUnregisterFunctionAttribute ()]

static void UnregisterServer (Type t)

{

Code to unregister custom task

}

Registration-free function to remove a 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 opened like dual_interface COM components. You create a default interface from all of the class's public, Non-static fields, properties, and methods. After the following line of code is applied using the custom task source file:

[Assembly:classinterface (Classinterfacetype.autodual)]

This part of the code has been completely enumerated.

Add Functional Customization Tasks

The "compiling, registering, and Installing custom Tasks" section of this article contains a simple DTS custom task code. The task has two properties: the value of the Name and Description,description property appears in the message box. This example describes a minimized code that you can use to define tasks with existing functional DTS. However, you can create a user interface by executing the CustomTaskUI interface, but that is not discussed. The DTS Designer creates a default user interface for custom tasks by executing only custom interfaces.

All DTS customization tasks perform custom task interfaces. A custom user interface is made up of two properties, a collection, and a method:

1, name and description attributes;

2, properties set;

3, execute method.

All custom tasks should perform properties, property sets, and Execute methods.

Compiling, registering, and installing 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 = (packageevents) ppackageevents;
PackageLog PackageLog = (packagelog) ppackagelog;
MessageBox.Show (description);
}
The A-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\\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 SimpleCustomTask ();
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\\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 ());
}
}
}

}




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.