3

我想计算一个哈希函数(使用 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 包如下所示: 我的 SSIS 包看起来像这样 我只是从数据库中获取一个包含一行的表。我想对所有列进行哈希处理,创建另一列并将哈希结果存储在那里。我能够生成散列,但我不知道如何将列添加到结果集中并将散列​​的值插入该列。任何帮助,将不胜感激。谢谢。

4

1 回答 1

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);
        }
    }
于 2014-12-30T16:26:20.663 回答