0

I have formed a hash key with the help of HashBytes as below with the multiple fields combination on SQL.

hashbytes('MD5', PID+PNumber+CONVERT(VARCHAR(50),cast(datestamp as  binary)) +CONVERT(VARCHAR(50), TCount)+OCD+OSeqNbr+TID) AS VARBINARY(80)

All the above fileds datatypes are below

PID - nvarhcar(30)
PNumber - nvarhcar(30)
datestamp  - Datetime 
TCount  - Int
OCD - varhcar(2)
OSeqNbr - varhcar(10)
TID  - nvarhcar(100)

But i need C# equivalent function to get the value , so that i can get the value using c# code on the field values and then passes value to sql then compare the value against to the hash key column.

I have tried as below to get the byte[] value which is equivalent to the hash key binary value. But it is not the same value as sql hash key value.

var key = System.Text.Encoding.Unicode.GetBytes(Event.PID + Event.PNumber +
                                                     Event.DateStamp + Event.TCount +
                                                     Event.OCD + Event.OSeqNbr +
                                                     Event.TID);

Can anybody help on this what i am missing?

Chandra Mohan
  • 729
  • 2
  • 10
  • 29
  • Tag the dbms used. (This seems to be a product specific question.) – jarlh Aug 03 '16 at 09:07
  • Have you tried to search for something like "c# hashbytes md5" ? [Click](http://stackoverflow.com/q/6506910/1997232). – Sinatr Aug 03 '16 at 09:09
  • Yes I did. What i found is they are doing on single column. But i need to have it on multiple columns with different datatypes so here it is not matching – Chandra Mohan Aug 03 '16 at 09:10
  • Actully the datetime conversion is causing problem which i did while generating hashbytes to avoid the Non deterministic error. But becuse of that i am not able to get c# equivalent now. Any workaround please? CONVERT(VARCHAR(50),cast(datestamp as binary)) – Chandra Mohan Aug 03 '16 at 11:22
  • Here is the info why i made datetime as binary http://stackoverflow.com/questions/38722783/hashbytes-with-datetime2-field?noredirect=1#comment64821906_38722783. But i got strucked now to get the c# equivalent value. Any help would be appreciated – Chandra Mohan Aug 03 '16 at 11:27
  • Probably a duplicate of https://stackoverflow.com/questions/14146636/how-do-i-calculate-the-equivalent-to-sql-server-hashbytessha1-columnname – Andrey Belykh Nov 06 '17 at 19:55

0 Answers0