1

处理 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>-->
4

1 回答 1

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-哈希.aspx

于 2015-01-22T16:59:53.503 回答