2014-10-17 102 views
3

我想計算散列函數(使用MD5)並將結果添加到現有表中。我正在SSIS中使用腳本任務來編寫一個簡短的C#腳本。 這裏是我的腳本:通過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包看起來是這樣的: How my SSIS package looks like this 我只是從數據庫中抓住一個表中的一行。我想散列所有的列,創建另一列並在那裏存儲散列的結果。我能夠生成散列,但是我不知道如何向結果集中添加列,並將哈希值插入到該列中。任何幫助,將不勝感激。 謝謝。

+0

在腳本組件,看看第三個選項卡,以及OUTPUT0集合中添加新列 – billinkc 2014-10-17 20:24:28

+0

@billinkc謝謝你。如何在腳本中爲該列分配一個值? – 2014-10-17 20:27:25

+0

有關此答案,請參閱[腳本任務](http://stackoverflow.com/questions/18210181/system-text-regularexpressions-regex-replace-error-in-c-sharp-for-ssis/18222568#18222568)部分。相同的步驟,然後您將分配您有mbox調用的值 – billinkc 2014-10-17 20:32:18

回答

1

要分配在C#中的列,你在回答詢問billinkc你的代碼看起來像:

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); 
     } 
    }