我想在C#中获取与SQL Server完全相同的hashbytes / checksum函数。我们的SPA将大量数据发布到我们的后端,我想在插入之前检查数据库中的重复数据。虽然最好的方法是计算我想插入的值的哈希/校验和,并在where子句中使用它们。唯一行是使用列组合的标识符。
where hashbytes('sha1', a+b+c) in (
0x40BD001563085FC35165329EA1FF5C5ECBDBBEEF
,0x5F6955D227A320C7F1F6C7DA2A6D96A851A8118F
,0x91DFDE1D6E005E422F64A59776234F1F4C80B5E4
,0x19187DC98DCE52FA4C4E8E05B341A9B77A51FD26
,0xEADC1DD8FC279583D5552700AE5D248E3FA123BD
,0xA93C168323147D1135503939396CAC628DC194C5)
当然,我可以构建一个非常具体的where子句来检查重复,但认为这会更快。我已经在堆栈上看到了很多关于这个问题的线程,但我对如何做到这一点感到困惑。
select a,b,c, CHECKSUM(a,b,c) _checkSum,hashbytes('sha1', a+b+c) _hash
from (
select '1' as a, '2' as b, '3' as c, 4 as d
union all
select '3' as a, '2' as b, '1' as c, 4 as d
union all
select '1' as a, '3' as b, '2' as c, 4 as d
union all
select '2' as a, '1' as b, '3' as c, 4 as d
union all
select '2' as a, '3' as b, '1' as c, 4 as d
union all
select '3' as a, '1' as b, '2' as c, 4 as d
) tbl
在我的c#app中,我可以在数据表或字符串数组中提供数据。即
var data = new string[] {"1", "2", "3"};
问题是如何将上面的数据字符串数组转换为校验和/哈希?