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