T-SQL 可以存储 ulong 吗?
我想将 C#.NET ulong
存储到 T-SQL 数据库中。我没有看到任何执行此操作的规定,因为 SQL bigint
具有与普通 long
相同的最小/最大值。
我有什么办法可以做到这一点吗?或者捕获 OverflowException
是我唯一的希望?
I want to store a C#.NET ulong
into a T-SQL database. I don't see any provisions for doing this, as the SQL bigint
has the same Min/Max values as a normal long
.
Is there any way I can do this? Or is catching an OverflowException
my only hope?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这应该回答您的问题:
http://social.msdn.microsoft.com/Forums/en-US/adodotnetdataproviders/thread/ff08c190-a981-4896-9542-3f64b95a84a2/
您将使用 BigInt,您只需小心如何使用在 C# 中将有符号类型转换回无符号类型
...
另一种可能性是使用长度为 8 的 VarBinary,然后在 C# 中将字节转换为 ulong
This should answer your question:
http://social.msdn.microsoft.com/Forums/en-US/adodotnetdataproviders/thread/ff08c190-a981-4896-9542-3f64b95a84a2/
You would use BigInt, you just have to be careful in how you convert the signed type back into an unsigned type in C#
...
The other possibility is to use VarBinary with a length of 8, then convert the bytes to a ulong in C#
我知道它不一样,但是这样做可以吗:
无符号长整型的最大值似乎是 18,446,744,073,709,551,615,这明显小于这个十进制可以存储的值。转换可能会出现问题,但我确信应用程序中的几个包装函数可以很快对其进行排序。
I know it's not the same, but would this do:
The maximum for an unsigned long seems to be 18,446,744,073,709,551,615 which is significantly less than this decimal can store. There might be issues converting, but I'm sure that a couple of wrapper functions in your application would sort it pretty quickly.
对我来说,尼尔·N 的解决方案不起作用。 Visual Studio 一直抱怨我无法将
long
值分配给ulong
变量,无论未选中
如何。最终有效的方法很简单
(没有
未检查
)。在 SQLite DB 中,该值存储为
long
,因此非常高的值会变成负数,并且在 SQL 端对它们进行排序会失败。但是在获得ulong
值之后,Linq 就可以发挥它的魔力了For me Neil N's solution didn't work. Visual Studio kept complaining that I can't assign a
long
value to aulong
variable, no matter theunchecked
.What did work in the end was simply
(without
unchecked
).In the SQLite DB the value is stored as
long
, so very high values become negative and sorting them on SQL-side fails. But after getting theulong
values Linq can do its magic