BIML代码生成HASHBYTE MD5列

时间:2015-01-21 15:57:48

标签: ssis biml hashbytes

我在处理HASHBYTE逻辑的代码的以下部分出错。

<ScriptComponentTransformation Name ="SCR HashByte Data Thread 1">

<ScriptComponentProject>
<ScriptComponentProject Name="SCRHashByteDataThread1">

<AssemblyReferences>
<AssemblyReference AssemblyPath="Microsoft.SqlServer.DTSPipelineWrap" />
<AssemblyReference AssemblyPath="Microsoft.SqlServer.DTSRuntimeWrap" />
<AssemblyReference AssemblyPath="Microsoft.SqlServer.PipelineHost" />
<AssemblyReference AssemblyPath="Microsoft.SqlServer.TxScript" />
<AssemblyReference AssemblyPath="System.dll" />
<AssemblyReference AssemblyPath="System.AddIn.dll" />
<AssemblyReference AssemblyPath="System.Data.dll" />
<AssemblyReference AssemblyPath="System.Xml.dll" />
</AssemblyReferences>

<InputBuffer Name="Input0">
<Columns>
<Column Name="RowNumber" DataType ="Int64" UsageType ="ReadWrite"></Column>
</Columns>
</InputBuffer>

<OutputBuffers>

<OutputBuffer Name="Output0">
<Columns>
<Column Name="MD5" DataType ="AnsiStringFixedLength"></Column>
</Columns>
</OutputBuffer>
</OutputBuffers>

<Files>
<File Path ="AssemblyInfo.cs">
using System.Reflection;
using System.Runtime.CompilerServices;
[assembly: AssemblyTitle("SC_AddRowNumber")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("SC_AddRowNumber")]
[assembly: AssemblyCopyright("Copyright @ Homeserve 2015")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
[assembly: AssemblyVersion("1.0.*")]

</File>



<File Path ="main.cs">
using System;
using System.Data;
using Microsoft.SqlServer.Dts.Pipeline.Wrapper;
using Microsoft.SqlServer.Dts.Runtime.Wrapper;
using System.Text;
using System.IO;

[Microsoft.SqlServer.Dts.Pipeline.SSISScriptComponentEntryPointAttribute]
public class ScriptMain : UserComponent
{
    private System.Security.Cryptography.MD5 _md5 =new System.Security.Cryptography.MD5CryptoServiceProvider();

    public override void PreExecute()
    {
        base.PreExecute();
    }

    public override void PostExecute()
    {
        base.PostExecute();
    }


    public override void Input0_ProcessInputRow(Input0Buffer Row)
    {
        var errorMsg = string.Empty;
        byte[] hashBytes;
        System.Text.StringBuilder sb = new System.Text.StringBuilder();

        try
        {
            string hashSource =
            (Row.ColA_IsNull ? "" : Row.ColA.ToString())
            + (Row.ColB_IsNull ? "" : Row.ColB.ToString())
            + (Row.ColC_IsNull ? "" : Row.ColC.ToString()));

            hashBytes = _md5.ComputeHash(
            System.Text.UnicodeEncoding.Unicode.GetBytes(hashSource));

            for (int i = 0; i <![CDATA[<]]> hashBytes.Length; i++)
            {
                sb.Append(hashBytes[i].ToString("X2"));
            }

            //Row.MD5 = "0x" + sb.ToString();
        }
        catch (Exception e)
        {
            errorMsg = e.Message;
        }
        finally
        {
            Row.MD5 = "0x" + sb.ToString();
        }

        if (errorMsg != null <![CDATA[&&]]> errorMsg != string.Empty)
            Row.MD5 = "Error";
    }
} 

</File>
</Files>



</ScriptComponentProject>
</ScriptComponentProject>
</ScriptComponentTransformation>-->

1 个答案:

答案 0 :(得分:0)

乍一看,你的hashSource最后有太多的右括号。

如果还没有,我会在我从SSIS包调用的DLL中执行类似的操作。也许这会有所帮助:

        byte[] bs = System.Text.Encoding.ASCII.GetBytes(hashSource);
        bs = _md5.ComputeHash(bs);
        System.Text.StringBuilder s = new System.Text.StringBuilder();
        foreach (byte b in bs)
        {
            s.Append(b.ToString("x2").ToLower());
        }
        string md5hash = s.ToString();

比较.Net和SQL Server哈希时,请记住这一点:http://weblogs.sqlteam.com/mladenp/archive/2009/04/28/Comparing-SQL-Server-HASHBYTES-function-and-.Net-hashing.aspx