T-SQL 可以存储 ulong 吗?

发布于 2024-09-04 16:27:54 字数 197 浏览 6 评论 0原文

我想将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

过去的过去 2024-09-11 16:27:54

这应该回答您的问题:

http://social.msdn.microsoft.com/Forums/en-US/adodotnetdataproviders/thread/ff08c190-a981-4896-9542-3f64b95a84a2/

您将使用 BigInt,您只需小心如何使用在 C# 中将有符号类型转换回无符号类型

// This has not been tested
unchecked
{
    myUlong = myDataReader.GetInt64(...);
}

...

另一种可能性是使用长度为 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#

// This has not been tested
unchecked
{
    myUlong = myDataReader.GetInt64(...);
}

...

The other possibility is to use VarBinary with a length of 8, then convert the bytes to a ulong in C#

酒与心事 2024-09-11 16:27:54

我知道它不一样,但是这样做可以吗:

select convert(decimal(38, 0), 12345678901234567890123456789012345678)

无符号长整型的最大值似乎是 18,446,744,073,709,551,615,这明显小于这个十进制可以存储的值。转换可能会出现问题,但我确信应用程序中的几个包装函数可以很快对其进行排序。

I know it's not the same, but would this do:

select convert(decimal(38, 0), 12345678901234567890123456789012345678)

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.

送舟行 2024-09-11 16:27:54

对我来说,尼尔·N 的解决方案不起作用。 Visual Studio 一直抱怨我无法将 long 值分配给 ulong 变量,无论未选中如何。

最终有效的方法很简单

ulong fileSize = (ulong)(long)reader["fileSize"];

(没有未检查)。

在 SQLite DB 中,该值存储为long,因此非常高的值会变成负数,并且在 SQL 端对它们进行排序会失败。但是在获得 ulong 值之后,Linq 就可以发挥它的魔力了

items = items.OrderByDescending(x => x.FileSize).ToList();

For me Neil N's solution didn't work. Visual Studio kept complaining that I can't assign a long value to a ulong variable, no matter the unchecked.

What did work in the end was simply

ulong fileSize = (ulong)(long)reader["fileSize"];

(withoutunchecked).

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 the ulong values Linq can do its magic

items = items.OrderByDescending(x => x.FileSize).ToList();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文