在C#中的cookie编程
本文将关注列出、执行DTS包和改变DTS包的属性或者全局变量。
首先我们看看如何列出保存在本地(local or (local)\NetSDK)服务器为本地包。在DOTNET中处理DTS包的关键是增加对“Microsoft DTSPackage Object Library”的引用(dts.dll)。
以下代码可以列出服务器上的本地包。
static void Main(string[] args)
{
DTS.Application App = new DTS.Application();
DTS.PackageSQLServer package = App.GetPackageSQLServer("(local)\\NetSDK", "sa", "", DTS.DTSSQLServerStorageFlags.DTSSQLStgFlag_Default);
DTS.PackageInfos Infos;
Infos = package.EnumPackageInfos("", false, "");
foreach (DTS.PackageInfo Info in Infos)
{
Console.WriteLine("Name : {0}\tVersionID : {1}", Info.Name, Info.VersionID);
}
Console.Read();
}
取保存在格式化存储文件中的dts包属性代码如下:
static void Main(string[] args)
{
DTS.Package2Class package = new DTS.Package2Class();
DTS.SavedPackageInfos Infos =
package.GetSavedPackageInfos("C:\\Pubs2Pubs2Package.dts");
foreach (DTS.SavedPackageInfo Info in Infos)
{
Console.WriteLine("Name : {0}\tVersionID : {1}", Info.PackageName, Info.VersionID);
}
package.UnInitialize();
Console.Read();
}
通过Package2Class对象可以执行dts包,或者操作dts包的属性。代码如下:
static void Main(string[] args)
{
DTS.Package2Class package = new DTS.Package2Class();
try
{
Object MIA = System.Reflection.Missing.Value;
package.LoadFromSQLServer("(local)\\NetSDK",
"sa", "", DTS.DTSSQLServerStorageFlags.DTSSQLStgFlag_Default, "", "",
"{F580DFC4-1F6A-4D70-B6A1-1C200B0D7890}", "CustomTaskPKG", ref MIA);
Console.WriteLine(package.Description);
package.Description = "My description";
Console.WriteLine(package.Description);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
finally
{
package.UnInitialize();
package = null;
}
Console.Read();
}
如果要使用最新的版本信息,则不填写版本id信息,代码如下:
package.SaveToSQLServer("(local)\\NetSDK", "sa", "",
DTS.DTSSQLServerStorageFlags.DTSSQLStgFlag_Default, "", "","",ref MIA,false);
以下是设置dts包全局变量的过程:
static void Main(string[] args)
{
DTS.Package2Class package = new DTS.Package2Class();
try
{
Object MIA = System.Reflection.Missing.Value;
package.LoadFromStorageFile("C:\\Pubs2Pubs2Package.dts", "", "", "",
"Pubs2Pubs2Package", ref MIA);
package.GlobalVariables.AddGlobalVariable("MyVariable", "MyValue");
foreach (DTS.GlobalVariable GVar in package.GlobalVariables)
Console.WriteLine("Name : {0}\tValue : {1}", GVar.Name, GVar.Value);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
finally
{
package.UnInitialize();
}
Console.Read();
}
修改全局变量值语句:
package.GlobalVariables.Item("MyVariable").let_Value("Hello from C#");
以下语句将改动保存到dts包中。
package.SaveToStorageFile("C:\\Pubs2Pubs2Package.dts","","",ref MIA,false);
执行包:
package.Execute();