c# - 在 SSIS 中通过 C# 脚本向表添加派生列

标签 c# .net ssis ssis-2012

我想计算一个散列函数(使用 MD5)并将结果添加到现有表中的一列。我在 SSIS 中使用脚本任务来编写一个简短的 C# 脚本。 这是我的脚本:

using System;
using System.Data;
using Microsoft.SqlServer.Dts.Pipeline.Wrapper;
using Microsoft.SqlServer.Dts.Runtime.Wrapper;
using System.Security.Cryptography;
using System.Text;
using System.Windows.Forms;

[Microsoft.SqlServer.Dts.Pipeline.SSISScriptComponentEntryPointAttribute]
public class ScriptMain : UserComponent
{
    public override void PreExecute()
    {
        base.PreExecute();
        /*
         * Add your code here
         */
    }

    public override void PostExecute()
    {
        base.PostExecute();
        /*
         * Add your code here
         */
    }

    public override void Input0_ProcessInputRow(Input0Buffer Row)
    {
        /*
         * Add your code here
         */
        using (MD5 md5Hash = MD5.Create())
        {
            string hash = GetMd5Hash(md5Hash, Row);
            MessageBox.Show(hash);
        }
    }

    static string GetMd5Hash(MD5 md5Hash, Input0Buffer input)
    {

        // Convert the input string to a byte array and compute the hash. 
        byte[] data = md5Hash.ComputeHash(Encoding.UTF8.GetBytes(input.ToString()));

        // Create a new Stringbuilder to collect the bytes 
        // and create a string.
        StringBuilder sBuilder = new StringBuilder();

        // Loop through each byte of the hashed data  
        // and format each one as a hexadecimal string. 
        for (int i = 0; i < data.Length; i++)
        {
            sBuilder.Append(data[i].ToString("x2"));
        }

        // Return the hexadecimal string. 
        return sBuilder.ToString();
    }

}

我的 SSIS 包如下所示: How my SSIS package looks like this 我只是从一行数据库中获取一张表。我想散列所有列,创建另一列并将散列结果存储在那里。我能够生成散列,但我不知道如何将列添加到结果集中并将散列​​的值插入该列。任何帮助,将不胜感激。 谢谢。

最佳答案

要按照您在回复 billlinkc 时的要求在 C# 中分配列,您的代码如下所示:

public override void Input0_ProcessInputRow(Input0Buffer Row)
    {
        /*
         * Add your code here
         */
        using (MD5 md5Hash = MD5.Create())
        {
            //string hash = GetMd5Hash(md5Hash, Row);
            Row.MyOutputColumn = GetMd5Hash(md5Hash, Row);
            //MessageBox.Show(hash);
        }
    }

关于c# - 在 SSIS 中通过 C# 脚本向表添加派生列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26432854/

相关文章:

c# - 检查 C# 中的任何条件编译符号

c# 更新语句与 where 子句

c# - Odbc Paradox 驱动程序 WHERE 子句 日期

c# - Entity Framework 平均值,按查询分组

ms-access - 如何以编程方式获取SSIS包中的MS Access表列表?

c# - 使用 Interop 从 excel 获取最后一个非空列和行索引

c# - MySql插入忽略性能

c# - WPF DataBinding 不更新属性(仅在启动时更新一次)

.net - NUnit API 和以编程方式运行测试

sql-server - 构建 SSIS 项目时未创建 ispac