Int64 不等于 C# 中的 long 吗?

发布于 2024-10-21 08:56:40 字数 1190 浏览 7 评论 0 原文

我一直在通过 SqlCeConnection。我一直在使用 ExecuteReader 读取记录 ID 的结果和 BigInt 值被读入长整型。

今天,我一直在使用基于 COUNT 的语句('SELECT COUNT(*) FROM X')的 SQL 语句,并且一直在使用 ExecuteScalar 读取这些单值结果。

但是,我遇到了一个问题。我似乎无法将这些值存储到我到目前为止一直在使用的 Long 数据类型中。我可以将它们存储到 Int64 中。

我一直使用 BigInt 作为记录 ID 来获取最大潜在记录数。

因此,8 个字节的 BigInt 是一个 Int64。 Long 不等于 Int64 因为两者都是 64 位有符号整数吗?

因此,为什么我不能将 Int64 转换为 Long ?

long recordCount =0;

recordCount = (long)selectCommand.ExecuteScalar();

错误是:

指定的演员阵容无效。

我可以将 BigInt 读入 Long。这不是问题。 我无法将 SQL COUNT 读取为 long。

COUNT 返回一个 Int (Int32),因此问题实际上是将 Int32 转换为 long。

I have been playing around with SQL and databases in C# via SqlCeConnection. I have been using ExecuteReader to read results and BigInt values for record IDs which are read into Longs.

Today I have been playing with SQL statements that use COUNT based statements ('SELECT COUNT(*) FROM X') and have been using ExecuteScalar to read these single valued results.

However, I ran into an issue. I can't seem to store the values into a Long data type, which I have been using up to now. I can store them into Int64's.

I have been using BigInt for record IDs to get the maximum potential number of records.

A BigInt 8 bytes therefore is an Int64. Isn't a Long equal to an Int64 as both are 64-bit signed integers?

Therefore, why can't I cast an Int64 into a Long?

long recordCount =0;

recordCount = (long)selectCommand.ExecuteScalar();

The error is:

Specified cast is not valid.

I can read a BigInt into a Long. It is not a problem.
I can't read an SQL COUNT into a long.

COUNT returns an Int (Int32), so the problem is in fact casting an Int32 into a long.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

桃扇骨 2024-10-28 08:56:40

longInt64 在 .NET 中;它只是 C# 中的别名。您的问题是将返回值转换为 long ,除非我们确定从您的查询返回的类型,否则我们不会知道您为什么会收到错误。 SQL BigInt 必须可转换为long

如果返回的是 COUNT(*),则它是 Int32。您需要使用 转换 类:

long l = Convert.ToInt64(selectCommand.ExecuteScalar());

long is Int64 in .NET; it is just an alias in C#. Your problem is casting the return value to long and unless we know the type coming back from your query for sure, we would not know why you get an error. SQL BigInt must be convertable to long.

If it is the COUNT(*) which is coming back, then it is Int32. You need to use the Convert class:

long l = Convert.ToInt64(selectCommand.ExecuteScalar());
路还长,别太狂 2024-10-28 08:56:40

如果您认为计数将溢出 int/Int32,则应该使用 COUNT_BIG() - 它具有正确的返回类型。


至于为什么演员不起作用,我不确定。以下 C#:

System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand();
long lCount = (long)cmd.ExecuteScalar();
Int64 iCount = (Int64)cmd.ExecuteScalar();

编译为该 IL:

L_0000: nop 
L_0001: newobj instance void [System.Data]System.Data.SqlClient.SqlCommand::.ctor()
L_0006: stloc.0 
L_0007: ldloc.0 
L_0008: callvirt instance object [System.Data]System.Data.Common.DbCommand::ExecuteScalar()
L_000d: unbox.any int64
L_0012: stloc.1 
L_0013: ldloc.0 
L_0014: callvirt instance object [System.Data]System.Data.Common.DbCommand::ExecuteScalar()
L_0019: unbox.any int64
L_001e: stloc.2 
L_001f: ret 

也就是说,它们似乎编译为相同的代码。

If you're thinking that your counts are going to overflow an int/Int32, you ought to use COUNT_BIG() in your SQL instead - it has the correct return type.


As to why the casts aren't working, I'm not sure. The following C#:

System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand();
long lCount = (long)cmd.ExecuteScalar();
Int64 iCount = (Int64)cmd.ExecuteScalar();

Compiles to this IL:

L_0000: nop 
L_0001: newobj instance void [System.Data]System.Data.SqlClient.SqlCommand::.ctor()
L_0006: stloc.0 
L_0007: ldloc.0 
L_0008: callvirt instance object [System.Data]System.Data.Common.DbCommand::ExecuteScalar()
L_000d: unbox.any int64
L_0012: stloc.1 
L_0013: ldloc.0 
L_0014: callvirt instance object [System.Data]System.Data.Common.DbCommand::ExecuteScalar()
L_0019: unbox.any int64
L_001e: stloc.2 
L_001f: ret 

That is, they appear to compile to identical code.

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