【问题标题】:SSIS Script Component Input0Buffer method no GetName()?SSIS 脚本组件 Input0Buffer 方法没有 GetName()?
【发布时间】:2018-03-15 21:20:24
【问题描述】:

我正在寻找一种在 SSIS 数据流任务脚本组件中获取我的属性名称的方法。我一直在寻找高低只找到this。我一直试图让这段代码工作,但我太新手了,无法理解这里发生的事情,我觉得解释得不是很好(无意冒犯)。

此组件之前的源是使用连接两个表的 SQL 查询。在组件内部,我想将列与列进行比较。然后调用我创建的更新方法,使用SqlConnection来执行更新。

public override void Input0_ProcessInputRow(Input0Buffer Row)
{
    if (Row.TableALastName != Row.TableBLastName)

        // Call the update method if the last name did not match.
        this.UpdateRecord("TableBLastName", Row.TableALastName.ToString(), Row.TableAAssociateId.ToString());
    }
}    

private void UpdateRecord(string columnName, string change, string associateId)
{
    SqlConnection sqlConnection;
    sqlConnection = new SqlConnection(this.Variables.Connection);

    string updateQuery = "UPDATE [SomeDataBase].[dbo].[TableB] SET " + columnName + " = " + change + " WHERE [Associate_ID] = " + associateId;

    using (SqlCommand cmd2 = new SqlCommand(updateQuery, sqlConnection))
    {
        sqlConnection.Open();
        cmd2.ExecuteNonQuery();
        sqlConnection.Close();
    }
}

我想以某种方式获得 Row.TableBLastName 的 PropertyName,而不必为我正在做的每个测试硬编码“TableBLastName”,这会很多。

问题是输入缓冲区类没有 Property.GetName() 这也意味着我无法向该类添加方法来获取属性名称,因为每次运行都会重新生成它。

【问题讨论】:

    标签: c# ssis script-component


    【解决方案1】:
    public Input0_ProcessInputRow(Input0Buffer Row)
            {
                Dictionary<string, List<string>> list = new Dictionary<string, List<string>>();
                List<string> propertyList = new List<string>();
                Type myType = typeof(Input0Buffer);
                PropertyInfo[] allPropInfo = myType.GetProperties();
                List<PropertyInfo> SqlPropInfo = allPropInfo.Where(x => !x.Name.Contains("AM_")).ToList();
    
                // Loop through all the Sql Property Info so those without AM_
                for (int i = 0; i < SqlPropInfo.Count(); i++)
                {
                    List<string> group = new List<string>();
                    foreach (var propInfo in allPropInfo)
                    {
                        if (propInfo.Name.Contains(SqlPropInfo[i].Name))
                        {
                            // Group the values based on the property
                            // ex. All last names are grouped.
                            group.Add(propInfo.GetValue(Row, null).ToString());
                        }
                    }
    
                    // The Key is the Sql's Property Name.
                    list.Add(SqlPropInfo[i].Name, group);
                }
    
                foreach (var item in list)
                {
                    // Do a check if there are two values in both SQL and Oracle.
                    if (item.Value.Count >= 2)
                    {
                        if (item.Value.Count() != item.Value.Distinct().Count())
                        {
                            // Duplicates exist do nothing.
                        }
                        else
                        {
                            // The values are different so update the value[0]. which is the SQL Value.
                            UpdateRecord(item.Key, item.Value[0], Row.AssociateId);
                        }
                    }
                }
            }
    

    我将两个表中的值分开,因此 TableA 和 TableB 中有两个列表值。您可以为 TableA 中的值加上“AM_”或不同的前缀,这样您就可以使用反射来获取带和不带前缀的属性,并找出哪些值属于哪个表。然后我只是循环遍历属性并将值与目标值中的属性分组(所以那些没有前缀“AM_”的值)然后循环遍历分组列表并比较两个值,如果不同,则使用 TableB 更新 TableA匹配它们的值

    【讨论】:

    • 我看到你在这里做了什么,我在一个表中添加了一个前缀。这确实奏效了!我需要进行更多测试,但我认为这可行!谢谢PJ!
    【解决方案2】:

    你已经在 SSIS 中,所以我会建议使用它(不管我通常多快跳到 C# 来解决问题)

    这是一个经典的条件拆分场景:

    进行测试,然后将结果运行到 SQL 更新语句中。

    【讨论】:

    • 我知道您实际上想要做什么,但我不确定您如何知道应该匹配哪些列。
    • Keith,我有一个源,它是一个 SQL 查询,它在此脚本组件之前的 ID 上将表 A 连接到表 B。因此,脚本组件正在生成 Input0Buffer 类,其中包含我选择的列作为属性。这就是我比较列的方式。希望这有助于解释。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-07-22
    • 1970-01-01
    • 1970-01-01
    • 2021-04-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多