2013-05-31 24 views
3

this post一樣,我也試圖從SSIS包中提取SQL。我以爲我會嘗試發佈相同的代碼。這聽起來像代碼爲他工作,但不完整,因爲它沒有處理所有可能的情況。下面的代碼來調用PROCSSIS API:如何知道將什麼接口投射到__COMObject?

var taskHost = (Microsoft.SqlServer.Dts.Runtime.TaskHost)_Package.Executables[0]; 
var innerObject = taskHost.InnerObject; 

List<TaskHost> listOfTaskHosts = new List<TaskHost>(); 
listOfTaskHosts.Add(taskHost); 

string sql = ExtractQueriesFromTasks(listOfTaskHosts); 

從後,這裏的PROC:

public static string ExtractQueriesFromTasks(List<TaskHost> Tasks) 
{ 
    string src_query = ""; 
    foreach (TaskHost executable in Tasks) 
    { 
     DtsContainer Seq_container = (DtsContainer)executable; 

     if (executable.InnerObject is TaskHost) 
     { 
      TaskHost th = (TaskHost)executable.InnerObject; 
      string n = th.InnerObject.GetType().Name; 
     } 


     if (executable.InnerObject.GetType().Name == "__ComObject") 
     { 
       Microsoft.SqlServer.Dts.Pipeline.Wrapper.IDTSPipeline100 sqlTask1 = (Microsoft.SqlServer.Dts.Pipeline.Wrapper.IDTSPipeline100)executable.InnerObject; 
     } 
    } 
    return src_query; 
} 

我得到這個

{System.InvalidCastException: Unable to cast COM object of type 'System.__ComObject' to interface type 'Microsoft.SqlServer.Dts.Pipeline.Wrapper.IDTSPipeline100'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{89CEBA86-EC51-4C62-A2D3-E9AA4FC28900}' failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)). 

我嘗試這樣得到的接口列表,但返回空陣列

 if (executable.InnerObject.GetType().Name == "__ComObject") 
     { 
      Type[] types = (executable.InnerObject.GetType()).GetInterfaces(); 

      foreach (Type currentType in types) 
      { 
       if (typeof(Microsoft.SqlServer.Dts.Pipeline.Wrapper.IDTSPipeline100).IsAssignableFrom(executable.InnerObject.GetType())) 
       { 
        Microsoft.SqlServer.Dts.Pipeline.Wrapper.IDTSPipeline100 sqlTask1 = (Microsoft.SqlServer.Dts.Pipeline.Wrapper.IDTSPipeline100)executable.InnerObject; 
       } 
      } 

我想我的問題是知道什麼接口投這些COM對象。人們如何知道?

由於缺少輸入,我也對我可能需要哪些庫進行任務感到困惑。它看起來像COM版本以及託管封裝。我的希望是,託管的包裝會給我強類型的對象,而不是__COMObject,但可能不會。我剛開始玩,我不知道從哪裏開始。如果有人評論了我可能需要引用哪些庫來完成從SSIS包中提取SQL的任務,那麼我會很感激。我可能還需要提取關於哪些源文件傳輸到哪個目標表的一般信息。

  1. Microsoft.SqlServer.DTSPipelineWrap.dll
  2. Microsoft.SQLServer.DTSRuntimeWrap.dll
  3. Microsoft.SQLServer.ManagedDTS.dll
  4. Microsoft.SqlServer.PipelineHost.dll
  5. Microsoft.SqlServer。 ScriptTask.dll
+2

我不知道這是你問什麼,但我發現'微軟。 VisualBasic.Information.TypeName(obj)'有用(正如這裏提到的http://www.mztools.com/articles/2006/mz2006013.aspx)在處理'__COMObject's之前。 –

+0

謝謝,看起來很有趣。我正在檢查它...... – ChadD

+0

起初,我認爲這是一個工具來幫助我確定投入COM對象的類型,但我認爲這只是一個相關的提示。我試過這個:string name = Microsoft.VisualBasic.Information.TypeName(executable.InnerObject); 正如預期的那樣,這仍然返回「__COMObhect」,就像GetType方法一樣。 – ChadD

回答

0

這是我如何從執行SQl任務中提取SQL的:

   foreach (Executable executable in _Package.Executables) 
       { 
        TaskHost taskHost = executable as TaskHost; 
        if (taskHost != null) 
        { 
         string taskHostName = taskHost.Name; 
         System.Diagnostics.Debug.WriteLine("SSIS Task=" + taskHostName); 

         IDTSExecuteSQL iDTSExecuteSQL; 

         try 
         { 
          iDTSExecuteSQL = (IDTSExecuteSQL)taskHost.InnerObject as IDTSExecuteSQL; 

          if (iDTSExecuteSQL != null) 
          { 

現在,如果我可以弄清楚如何從數據任務中提取sql語句:

MainPipe pipeline = taskHost.InnerObject as MainPipe; 
            if (pipeline != null) 
            { 
             foreach (IDTSComponentMetaData100 componentMetadata in pipeline.ComponentMetaDataCollection) 
             { 
              try 
              {??? 

現在是什麼?

0

猜測你沒有DTS對象的路徑。

試試這個。仔細檢查您是否有系統路徑變量: c:\ Program Files(x86)\ Microsoft SQL Server \ 100 \ Tools \ Binn \; c:\ Program Files \ Microsoft SQL Server \ 100 \ Tools \ Binn \; c:\ Program Files \ Microsoft SQL Server \ 100 \ DTS \ Binn \; C:\ Program Files(x86)\ Microsoft SQL Server \ 100 \ Tools \ Binn \ VSShell \ Common7 \ IDE \; C:\ Program Files(x86)\ Microsoft SQL Server \ 100 \ DTS \ Binn \

路徑中的「100」是sql server 2008.「110」是sql server 2012. 它們需要在路徑中的「110」版本之前出現。