小2博客

.NET(C#)

导航

通過VS.NET調用 SQL 的DTS

Posted on 2006-05-24 16:23  店小2£  阅读(721)  评论(2编辑  收藏  举报

Method1:

直接訪問 Server


string packageName = "Z_Test1";
   object pVarPersistStgOfHost = null;

   DTS.PackageClass package = new DTS.PackageClass();
   package.LoadFromSQLServer("YourServerName","UserName","Password", DTS.DTSSQLServerStorageFlags.DTSSQLStgFlag_UseTrustedConnection, null, null, null, packageName, ref pVarPersistStgOfHost);
   try
   {
    // Execute the package
    Console.WriteLine("DTS Package Executing..");
    package.Execute();
    Console.WriteLine("DTS Package Completed");
    Console.ReadLine();
          
   }
   catch (Exception ex)
   {
    Console.WriteLine(ex.Message);
   }

   finally
   {
    package.UnInitialize();
    package = null;
   }




Method2:

通過打包DTS為 .dts文件來調用

try
   {
    DTS.Package2Class package = new DTS.Package2Class();
   
    string filename = @"c:\test1.dts";
    string password = null;
    string packageID = null;
    string versionID = null;
    string packageName = "DTSTestPackage";
    object pVarPersistStfOfHost = null;
   
    package.LoadFromStorageFile(
     filename,
     password,
     packageID,
     versionID,
     name, ref
     pVarPersistStfOfHost);
   
    package.Execute();
    package.UnInitialize();
   
    package = null;
   }
   catch(System.Runtime.InteropServices.COMException e)
   {
    Console.WriteLine(e);
   }
   catch(System.Exception e)
   {
    Console.WriteLine(e);
   }


以上,Method1已經通過測試OK,Method2測試總是有問題。

雖然可以調用DTS很方便,但是如果我們把多個DTS集成到一個Jobs裏以後,可以使用SQL Server的排程讓DTS自己運行。這樣的話就會更方便,不需要使用Method1來使用循環來調用DTS,而只需要調用一個Jobs就OK。

但是本人還沒有找到  C#來調用 Sql Jobs的資料,望有資料的朋友能夠提供,謝謝。