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