通过Visual C#.NET建一个DTS任务之二(转)

本文介绍如何在.NET环境中为Data Transformation Services (DTS) 注册自定义任务。通过使用ComRegisterFunctionAttribute和ComUnregisterFunctionAttribute,可以实现自定义任务的注册与卸载。此外,还提供了一个简单的自定义任务示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

为自定义的任务添加代码

   代码的自定义注册。.NET没有开放DllReginsterServer 和DllUnregisterServer 像COM组件的入口,但是你可以使用ComRegisterFunctionAttribute 类执行任务注册和撤销注册。在自定义类声明之前添加下面代码:

[Guid("A39847F3-5845-4459-A25E-DE73A8E3CD48"), ComVisible(true)]

[ProgId("DTS.SimpleTask")]

public class SimpleTask : CustomTask

{

//implementation of custom task

}

   下面的代码是一个函数注册的范例执行。函数的全部代码在自定义任务的编译、注册和安装部分 。

[System.Runtime.InteropServices.ComRegisterFunctionAttribute()]

static void RegisterServer(Type t)

{

//code to register custom task

}

   注册函数增加下面的键值用来注册。

HKEY_CLASSES_ROOTCLSIDA39847F3-5845-4459-A25E-DE73A8E3CD48Implemented Categories{10020200-EB1C-11CF-AE6E-00AA004A34D5}

10020200-EB1C-11CF-AE6E-00AA004A34D5是DTS包对象的类编号。因为所有的自定义的任务执行自定义的接口所以必须注册。注册函数添加下面的注册键值:

HKEY_CURRENT_USERSoftwareMicrosoftMicrosoft SQL Server80DTSEnumerationTasksA39847F3-5845-4459-A25E-DE73A8E3CD48

   下面的DTS任务缓存目录列表,使自定义的任务出现在DTS设计器中:

HKEY_CURRENT_USERSoftwareMicrosoftMicrosoft SQL Server80DTSEnumerationTasks

   下面的代码示范非注册函数的任务移出的执行。面注册函数是ComUnregisterFunctionAttribute类在.NET运行库的一部分。想浏览这个函数的完整代码,你可以看“编译、注册和安装自定义任务”部分:

[System.Runtime.InteropServices.ComUnregisterFunctionAttribute()]

static void UnregisterServer(Type t)

{

//code to unregister custom task

}

   免注册函数通过从注册表中删除下面键值从DTS任务缓存中移出任务

HKEY_CURRENT_USERSoftwareMicrosoftMicrosoft SQL Server80DTSEnumerationTasksA39847F3-5845-4459-A25E-DE73A8E3CD48

   最后,自定义的任务像dual_interface COM组件一样被开放。您从所有的类的public,非静态的字段,属性和方法创建一个默认的接口。在下面的一行代码在自定义任务源文件中USING应用之后:

[assembly:ClassInterface(ClassInterfaceType.AutoDual)]

   这部分的代码已经完全列举了。

增加功能性的自定义任务

   本文“编译、注册和安装自定义任务”部分包含一个简单的DTS自定义任务代码。 任务有两个属性:Name 和Description,Description属性的值就会出现在消息框中。这个例子的描述了一个最小化的代码你可以使用已有的功能性的DTS定义任务。然而,你可以通过执行CustomTaskUI接口创建一个用户界面,但是那并不作讨论。通过只执行自定义的接口,DTS设计者为自定义任务创建一个默认的有户界面。

   所有的DTS自定义任务执行自定义任务接口。自定义的用户接口是由两个属性,一个集合和一个方法:

   1、 Name和Description属性;

   2、 Properties集;

   3、 Execute方法。

   所有的自定义任务应该执行属性、属性集和Execute方法。

   编译、注册和安装自定义任务

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);
    }
    //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 = "SoftwareMicrosoftMicrosoft SQL Server80DTSEnumerationTasks";
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 = "SoftwareMicrosoftMicrosoft SQL Server80DTSEnumerationTasks";
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());
   }
  }
}

}
[@more@]

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/8781179/viewspace-924596/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/8781179/viewspace-924596/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值