通过SSIS中的C#脚本将派生列添加到表中

时间:2014-10-17 20:19:33

标签: 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 我只是从一行数据库中获取一个表。我想哈希所有列,创建另一列并在那里存储哈希的结果。我能够生成哈希,但我不知道如何向结果集添加列并将哈希值插入该列。任何帮助,将不胜感激。 感谢。

1 个答案:

答案 0 :(得分:1)

要在回复billinkc时询问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);
        }
    }