下面的代碼示范非注冊函數的任務移出的執行。面注冊函數是ComUnregisterFunctionAttribute類在.Net運行庫的一部分。想浏覽這個函數的完整代碼,你可以看“編譯、注冊和安裝自定義任務”部分:
[System.Runtime.InteropServices.ComUnregisterFunctionAttribute()]
static void UnregisterServer(Type t)
{
//code to unregister custom task
}
免注冊函數通過從注冊表中刪除下面鍵值從DTS任務緩存中移出任務
HKEY_CURRENT_USER\Software\Microsoft\Microsoft SQL Server\80\DTS\Enumeration\Tasks\A39847F3-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 = "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());
}
}
}
}