2011-09-12 42 views
1

得到它的工作 - 發佈低於我的解決方案,但會想知道是否有更好的辦法SSIS腳本組件:獲取子記錄創建對象

您好所有

我想創建域事件爲我的數據庫中新創建(遷移後)域對象。

對於沒有任何內部子對象的對象,通過使用腳本組件可以正常工作。問題在於如何讓子行向事件對象添加信息。

Ex。客戶 - >客戶地點。

我在腳本組件創建事件 - 作爲tranformation-(引用我的域事件模塊),然後創建發送關於事件的序列化信息作爲列值。輸入行當前爲父對象提供數據。

請指教。

問候,

的三月

編輯1

我想補充一點,現在我做processsing在

public override void Input0_ProcessInputRow(Input0Buffer Row) 

我在尋找的東西,如創建在此功能中的數據讀取器

循環遍歷數據行 - >創建子對象並將其添加到父級colelction

仍然在google和PreExecute和ProcessInput似乎要看的東西。 enter image description here

回答

1

這是我的解決方案。我是SSIS的新手,所以這可能不是最好的解決方案。

[Microsoft.SqlServer.Dts.Pipeline.SSISScriptComponentEntryPointAttribute] 
public class ScriptMain : UserComponent 
{ 
    IDTSConnectionManager100 connectionManager; 
    SqlCommand cmd = null; 
    SqlConnection conn = null; 
    SqlDataReader reader = null; 


    public override void AcquireConnections(object Transaction) 
    { 

     try 
     { 
      connectionManager = this.Connections.ScriptConnectionManager; 
      conn = connectionManager.AcquireConnection(Transaction) as SqlConnection; 

      // Hard to debug failure- better off logging info to file 
      //using (StreamWriter outfile = 
      // new StreamWriter(@"f:\Migration.txt")) 
      //{ 
      // outfile.Write(conn.ToString()); 
      // outfile.Write(conn.State.ToString()); 
      //} 
     } 
     catch (Exception ex) 
     { 
      //using (StreamWriter outfile = 
      // new StreamWriter(@"f:\Migration.txt")) 
      //{ 
      // outfile.Write(" EEEEEEEEEEEEEEEEEEEE"+ ex.ToString()); 
      //} 
     } 




    } 


    public override void PreExecute() 
    { 
     base.PreExecute(); 

     cmd = new SqlCommand("SELECT [CustomerLocation fields] FROM customerlocationView where [email protected]", conn); 
     cmd.Parameters.Add("CustId", SqlDbType.UniqueIdentifier); 

    } 

    public override void PostExecute() 
    { 
     base.PostExecute(); 
     /* 
      Add your code here for postprocessing or remove if not needed 
      You can set read/write variables here, for example: 
      Variables.MyIntVar = 100 
     */ 
    } 

    public override void Input0_ProcessInputRow(Input0Buffer Row) 
    { 
     Collection<CustomerLocation> locations = new Collection<CustomerLocation>(); 
     cmd.Parameters["CustId"].Value = Row.id; 

     // Any error always saw that reader reamians open on connection 
     if (reader != null) 
     { 
      if (!reader.IsClosed) 
      { 
       reader.Close(); 
      } 
     } 

     reader = cmd.ExecuteReader(); 

     if (reader != null) 
     { 
      while (reader.Read()) 
      { 
       // Get Child Details 
       var customerLocation = new CustomerLocation(....,...,...,); 
       customerLocation.CustId = Row.id; 
       locations.Add(customerLocation); 
      } 



     } 






     var newCustomerCreated = new NewCustomerCreated(Row.id,,...,...,locations); 

     var serializedEvent = JsonConvert.SerializeObject(newCustomerCreated, Formatting.Indented, 
                    new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.Objects, ReferenceLoopHandling = ReferenceLoopHandling.Ignore }); 

     Row.SerializedEvent = serializedEvent; 
     Row.EventId = newCustomerCreated.EventId; 
     ... 
     ... 
     ... 
     .... 
     .. 
     . 
     Row.Version = 1; 



     // using (StreamWriter outfile = 
     //  new StreamWriter(@"f:\Migration.txt", true)) 
     // { 
     //  if (reader != null) 
     // { 
     //  outfile.WriteLine(reader.HasRows); 
      //outfile.WriteLine(serializedEvent); 
      // } 
      // else 
      // { 
      //  outfile.Write("reader is Null"); 
      // } 
     //} 
     reader.Close(); 
    } 



    public override void ReleaseConnections() 
    { 
     base.ReleaseConnections(); 
     connectionManager.ReleaseConnection(conn); 
    } 
} 

有一點要注意的是,不同的方法來創建連接是 從得到的ConnectionManager連接字符串,並用它來創建OLEDB連接。