| 本文描述了如何利用Visual C#.net 创建一个DTS 自定义的任务。你可以通过C#.net创建自定义的任务的方式扩展DTS的功能。之后你可以安装并注册任务,他讲出现在DTS设计其中,就像默认的DTS任务。总之,你能够使用.NET Framework创建自定义的任务。 
 为DTSSpkg.dll创建一个及时的包
 
 如果一个基于微软.NET客户机访问一个COM组件,你必须使用一个包(这个组件包含的)。这类的包是及时的运行包(RCW)并且你也可以通过开放DTSpkg.dll的类型库编译。你也可以使用类型库导出工具(Tlbimp.exe)编译RCW,如:
 
 tlbimp.exe “C:ProgramFilesMicrosoft SQLServertoolsBinnDTSpkg.dll”/out:Microsoft.SQLServver.DTSPKG80.dll/keyfile:DTSPkg.snk
 
 “/keyfile”参数代表Microsoft.SQLServer.DTSPkg80.dll带有强类型名用public或private关键字。使用强类型名工具(sn.exe)在DTSPkg.snk 前创建关键字:
 
 sn.exe –k DTSPkg.snk
 
 你应该使用一个强类型名像其他的全局集合缓存,因为你安装了运行包。
 
 在全局集合缓存中安装运行包
 
 用全局集合缓存工具(GaCutil.exe)安装运行包:
 
 
 | gacutil.exe /I Microsoft.SQLServer.DTSPkg80.dll | 
安装了运行包后,你可以像添加.NET
C#工程中的引用一样的添加。 
为自定义的任务添加代码 代码的自定义注册。.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是
DTS包对象的类编号。因为所有的自定义的任务执行自定义的接口所以必须注册。注册函数添加下面的注册键值: 
HKEY_CURRENT_USERSoftwareMicrosoftMicrosoft SQL Server
DTSEnumerationTasksA39847F3-5845-4459-A25E-DE73A8E3CD48 
下面的
DTS任务缓存目录列表,使自定义的任务出现在
DTS设计器中: 
HKEY_CURRENT_USERSoftwareMicrosoftMicrosoft SQL Server
DTSEnumerationTasks 
下面的代码示范非注册函数的任务移出的执行。面注册函数是ComUnregisterFunctionAttribute类在.NET运行库的一部分。想浏览这个函数的完整代码,你可以看“编译、注册和安装自定义任务”部分: 
| [System.Runtime.InteropServices.ComUnregisterFunctionAttribute()] 
 static void UnregisterServer(Type t)
 
 {
 
 //code to unregister custom task
 
 }
 | 
免注册函数通过从注册表中删除下面键值从
DTS任务缓存中移出任务 
HKEY_CURRENT_USERSoftwareMicrosoftMicrosoft SQL Server
DTSEnumerationTasksA39847F3-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方法。 
#p# 
编译、注册和安装自定义任务 
| 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 = "";
 string guid = "{" + t.GUID.ToString() + "}";
 guid = guid.ToUpper();
 Console.WriteLine("RegisterServer ", 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 ", 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());
 }
 }
 }
 }
 | 
|  |