怎樣通過Visual C#.net創(chuàng)建一個DTS任務(wù)
2024-07-10 13:00:13
供稿:網(wǎng)友
怎樣通過visual c#.net創(chuàng)建一個dts任務(wù)
一、摘要
這篇文章描述了如何利用visual c#.net 創(chuàng)建一個dts 自定義的任務(wù)。你可以通過c#.net創(chuàng)建自定義的任務(wù)的方式擴展dts的功能。之后你可以安裝并注冊任務(wù),他講出現(xiàn)在dts設(shè)計其中,就像默認的dts任務(wù)。總之,你能夠使用.net framework創(chuàng)建自定義的任務(wù)。
這篇文章除了講創(chuàng)建一個dts自定義任務(wù)外,還包括一下部分內(nèi)容:
1、這篇文章中自定義的代碼分為編譯、注冊和安裝自定義任務(wù);
2、這部分一個有特色的部分就是你可以運行自定義的任務(wù);
3、在開發(fā)過程中你可以使用本文提及的一些工具(除非另有說明,這些工具已經(jīng)包含在.net中了,您可以通過.net的命令行運行這些工具)。
二、為dtsspkg.dll創(chuàng)建一個及時的包
如果一個基于微軟.net客戶機訪問一個com組件,你必須使用一個包(這個組件包含的)。這類的包是及時的運行包(rcw)并且你也可以通過開放dtspkg.dll的類型庫編譯。你也可以使用類型庫導(dǎo)出工具(tlbimp.exe)編譯rcw,如:
tlbimp.exe “c:/programfiles/microsoft sqlserver/80/tools/binn/dtspkg.dll”/out:microsoft.sqlservver.dtspkg80.dll/keyfile:dtspkg.snk
“/keyfile”參數(shù)代表microsoft.sqlserver.dtspkg80.dll帶有強類型名用public或private關(guān)鍵字。使用強類型名工具(sn.exe)在dtspkg.snk 前創(chuàng)建關(guān)鍵字:
sn.exe –k dtspkg.snk
你應(yīng)該使用一個強類型名像其他的全局集合緩存,因為你安裝了運行包。
在全局集合緩存中安裝運行包
用全局集合緩存工具(gacutil.exe)安裝運行包:
gacutil.exe /i microsoft.sqlserver.dtspkg80.dll
安裝了運行包后,你可以像添加.netc#工程中的引用一樣的添加。
為自定義的任務(wù)添加代碼
代碼的自定義注冊。.net沒有開放dllreginsterserver 和dllunregisterserver 像com組件的入口,但是你可以使用comregisterfunctionattribute 類執(zhí)行任務(wù)注冊和撤銷注冊。在自定義類聲明之前添加下面代碼:
[guid("a39847f3-5845-4459-a25e-de73a8e3cd48"), comvisible(true)]
[progid("dts.simpletask")]
public class simpletask : customtask
{
//implementation of custom task
}
下面的代碼是一個函數(shù)注冊的范例執(zhí)行。函數(shù)的全部代碼在自定義任務(wù)的編譯、注冊和安裝部分 。
[system.runtime.interopservices.comregisterfunctionattribute()]
static void registerserver(type t)
{
//code to register custom task
}
注冊函數(shù)增加下面的鍵值用來注冊。
hkey_classes_root/clsid/a39847f3-5845-4459-a25e-de73a8e3cd48/implemented categories/{10020200-eb1c-11cf-ae6e-00aa004a34d5}
10020200-eb1c-11cf-ae6e-00aa004a34d5是dts包對象的類編號。因為所有的自定義的任務(wù)執(zhí)行自定義的接口所以必須注冊。注冊函數(shù)添加下面的注冊鍵值:
hkey_current_user/software/microsoft/microsoft sql server/80/dts/enumeration/tasks/a39847f3-5845-4459-a25e-de73a8e3cd48
下面的dts任務(wù)緩存目錄列表,使自定義的任務(wù)出現(xiàn)在dts設(shè)計器中:
hkey_current_user/software/microsoft/microsoft sql server/80/dts/enumeration/tasks/
下面的代碼示范非注冊函數(shù)的任務(wù)移出的執(zhí)行。面注冊函數(shù)是comunregisterfunctionattribute類在.net運行庫的一部分。想瀏覽這個函數(shù)的完整代碼,你可以看“編譯、注冊和安裝自定義任務(wù)”部分:
[system.runtime.interopservices.comunregisterfunctionattribute()]
static void unregisterserver(type t)
{
//code to unregister custom task
}
免注冊函數(shù)通過從注冊表中刪除下面鍵值從dts任務(wù)緩存中移出任務(wù)
hkey_current_user/software/microsoft/microsoft sql server/80/dts/enumeration/tasks/a39847f3-5845-4459-a25e-de73a8e3cd48
最后,自定義的任務(wù)像dual_interface com組件一樣被開放。您從所有的類的public,非靜態(tài)的字段,屬性和方法創(chuàng)建一個默認的接口。在下面的一行代碼在自定義任務(wù)源文件中using應(yīng)用之后:
[assembly:classinterface(classinterfacetype.autodual)]
這部分的代碼已經(jīng)完全列舉了。
增加功能性的自定義任務(wù)
本文“編譯、注冊和安裝自定義任務(wù)”部分包含一個簡單的dts自定義任務(wù)代碼。 任務(wù)有兩個屬性:name 和description,description屬性的值就會出現(xiàn)在消息框中。這個例子的描述了一個最小化的代碼你可以使用已有的功能性的dts定義任務(wù)。然而,你可以通過執(zhí)行customtaskui接口創(chuàng)建一個用戶界面,但是那并不作討論。通過只執(zhí)行自定義的接口,dts設(shè)計者為自定義任務(wù)創(chuàng)建一個默認的有戶界面。
所有的dts自定義任務(wù)執(zhí)行自定義任務(wù)接口。自定義的用戶接口是由兩個屬性,一個集合和一個方法:
1、 name和description屬性;
2、 properties集;
3、 execute方法。
所有的自定義任務(wù)應(yīng)該執(zhí)行屬性、屬性集和execute方法。
編譯、注冊和安裝自定義任務(wù)
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());
}
}
}
}