SSIS 脚本组件:获取用于创建对象的子记录

发布于 2024-12-04 04:17:52 字数 641 浏览 2 评论 0原文

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

大家好,

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

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

前任。客户->客户地点。

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

请指教。

问候,

Mar

编辑 1

我想补充一点,当前我正在进行处理,

public override void Input0_ProcessInputRow(Input0Buffer Row)

我正在寻找类似在此函数中

通过数据行循环创建数据读取器之类的东西 ->创建子 objecta 并将其添加到父集合中

仍然在 google 上,PreExecute 和 ProcessInput 似乎值得一看。 在此处输入图像描述

Got it working - Posted My solution below but will like to know if there is better way

Hello All

I am trying to create Domain Event for a newly created (after migration) domain object in my database.

for Objects without any internal child objects it worked fine by using Script Component. The problem is in how to get the child rows to add information to event object.

Ex. Customer-> Customer Locations.

I am creating Event in Script Component- as tranformation- (have reference to my Domain event module) and then creating sending serialized information about event as a column value. The input rows currently provide data for the parent object.

Please advise.

Regards,

The Mar

Edit 1

I would like to add that current I am doing processsing in

public override void Input0_ProcessInputRow(Input0Buffer Row)

I am looking for something like create a a data reader in this function

loop through data rows -> create child objecta nd add it to parent colelction

Still on google and PreExecute and ProcessInput Seems something to look at . enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

絕版丫頭 2024-12-11 04:17:52

这是我的解决方案。我是 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连接。

This is my solution. I am a total newbie in SSIS , so this may not be the best solution.

[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);
    }
}

One thing to note is that a different approach to create connection is to
get the connection string from connectionManager and use it to create OLEDB connection.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文