是不是一个Int64等于长期在C#中? [英] Isn't an Int64 equal to a long in C#?

查看:140
本文介绍了是不是一个Int64等于长期在C#中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经通过的的SqlCeConnection 。我一直在使用的ExecuteReader 阅读效果和 BigInt有为其读入多头记录ID值。

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.

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

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.

不过,我遇到了一个问题。我似乎无法将值存储到一个Long数据类型,这是我一直使用到现在。我可以将它们存储到的Int64的。

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.

我一直在使用BigInt有备案的ID来获得的最大记录潜在数量。

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

一个BIGINT 8个字节是一个Int64。不长等于为Int64因为二者都是64位有符号整数?

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

所以,我为什么不能投一个Int64变成龙?

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

long recordCount =0;

recordCount = (long)selectCommand.ExecuteScalar();



该错误是:

The error is:

指定的转换是无效的。

我可以读一个BIGINT入长。这不成问题。
我不能读一个SQL COUNT为长。

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

COUNT函数返回一个int(Int32)已,所以这个问题实际上是在铸造一个Int32成长。

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

推荐答案

的Int64 在.NET ;它只是在C#中的别名。你的问题是铸造返回值,除非我们知道未来的类型从查询回来肯定的,我们不知道为什么你会得到一个错误。 SQL BigInt有必须是可转换为

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.

如果它是在回来的COUNT(*),然后它的Int32。您需要使用 转换 类:

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());

这篇关于是不是一个Int64等于长期在C#中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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