1

得到它的工作 - 在下面发布我的解决方案,但想知道是否有更好的方法

大家好

我正在尝试为我的数据库中新创建的(迁移后)域对象创建域事件。

对于没有任何内部子对象的对象,使用脚本组件可以正常工作。问题在于如何让子行向事件对象添加信息。

前任。客户-> 客户位置。

我在脚本组件中创建事件-作为转换-(参考我的域事件模块),然后创建发送有关事件的序列化信息作为列值。输入行当前为父对象提供数据。

请指教。

问候,

三月

编辑 1

我想补充一下我正在处理的当前

public override void Input0_ProcessInputRow(Input0Buffer Row)

我正在寻找类似 create aa data reader in this function

循环遍历数据行->创建子对象并将其添加到父集合

仍然在 google 和 PreExecute 和 ProcessInput 似乎有些东西要看。在此处输入图像描述

4

1 回答 1

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 custid=@CustId", 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 连接。

于 2011-09-13T14:32:44.217 回答