将一个C#生成的校验和与一个SQL Server比较 [英] Comparing a C# generated Checksum with a SQL Server one

查看:108
本文介绍了将一个C#生成的校验和与一个SQL Server比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从c#向我的数据库发送大量数据,并将计算出的校验和作为最后一个值,这应与SQL Server的存储过程从这些值计算出来的值进行比较。

I want to send a lot of data from c# to my database, together with a calculated checksum as the last value, which should be compared to the one that the SQL Server's stored procedure will compute from those values.

为了达到这个目的,我尝试了很多不同的方式,比如SQL内置的 Checksum 方法和不同的hashings,但是C#和SQL会计算一个不同的值,或者哈希不能用于我所需的nvarchar和整数(fx。MD5哈希)。

To achieve this, I have tried a lot of different ways like the SQL built-in Checksum method and different hashings, but either the C# and SQL compute a different value, or the hashing doesn't work with nvarchars and integers (fx. MD5 hashing) which is required for me.

有没有人真的设法做到这一点,或知道如何?

Did anyone actually manage to do this, or know how to?

引用我们失败的MD5尝试:
SQL Server HASHBYTES转换不一致性?

Reference to our failed MD5 attempt: SQL Server HASHBYTES conversion inconsistency?

在这里,计算结果与使用nvarchars的c#不一样

here, the computed result is different than the c# one when it uses nvarchars

散列法:

Set @result = convert(nvarchar(32), hashbytes('MD5', @DataID + @Data1 + @Data2 + @Data3), 2)

另外,当你给哈希方法一个整数作为参数时(如DataID),它会抱怨:

Also, when you give the hashing method an integer as parameter (like DataID), it complains:

参数数据类型int对于参数2的哈希函数函数是无效的。

"Argument data type int is invalid for argument 2 of hashbytes function."

推荐答案

<考虑到这些注释,你可以这样做( / 用作保护字符,因为int总是4字节,所以只需要一个):

Comments considered, here is how you can do it (/ is used as a guard char, only one is needed as the int is always 4 bytes):

这篇关于将一个C#生成的校验和与一个SQL Server比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆