What is the equivalent of bigint in C#?
Asked Answered
S

12

284

What am I supposed to use when handling a value in C#, which is bigint for an SQL Server database?

Susannsusanna answered 21/1, 2010 at 22:43 Comment(0)
B
453

That corresponds to the long (or Int64), a 64-bit integer.

Although if the number from the database happens to be small enough, and you accidentally use an Int32, etc., you'll be fine. But the Int64 will definitely hold it.

And the error you get if you use something smaller and the full size is needed? A stack overflow! Yay!

Budge answered 21/1, 2010 at 22:44 Comment(3)
Sure it's not integer overflow?Doyon
Good reference for more data types: msdn.microsoft.com/en-us/library/system.data.sqldbtype.aspxWalton
in C#, long is not capitalized.Neufer
C
81

Int64 maps directly to BigInt.

Source

Crompton answered 21/1, 2010 at 22:44 Comment(2)
is long other name for Int64 /Susannsusanna
Yes. The long keyword is an alias for System.Int64.Nahshun
R
14

I just had a script that returned the primary key of an insert and used a

SELECT @@identity

on my bigint primary key, and I get a cast error using long - that was why I started this search. The correct answer, at least in my case, is that the type returned by that select is NUMERIC which equates to a decimal type. Using a long will cause a cast exception.

This is one reason to check your answers in more than one Google search (or even on Stack Overflow!).

To quote a database administrator who helped me out:

... BigInt is not the same as INT64 no matter how much they look alike. Part of the reason is that SQL will frequently convert Int/BigInt to Numeric as part of the normal processing. So when it goes to OLE or .NET the required conversion is NUMERIC to INT.

We don't often notice since the printed value looks the same.

Readiness answered 20/9, 2010 at 19:10 Comment(4)
Thanks for the numeric tip.. just helped me out big time!Ceylon
You got a cast error because SCOPE_IDENTITY and @@IDENTITY return NUMERIC(38, 0), not because your BigInt PK does not fit into a C# Int64. In other words, BigInt is the same as Int64, but BigInt values returned through SCOPE_IDENTITY or @@IDENTITY may be up-converted to NUMERIC(38, 0).Whimper
Thanks Dan. FYI, still happening with SQL Azure (RTM) - 12.0.2000.8 & .NET v 4.5Dicky
Surprised to see no comment to Dan's correction about the type for SQL Server identity values to just use the following on the DB side: SELECT CAST(@@IDENTITY as BIGINT) as IdentityINT64Cestus
P
9

Use a long datatype.

Pachalic answered 21/1, 2010 at 22:44 Comment(0)
K
9

You can use long type or Int64

Karlis answered 8/7, 2015 at 17:59 Comment(0)
M
4

I think the equivalent is Int64

Miculek answered 21/1, 2010 at 22:44 Comment(0)
D
2

int in sql maps directly to int32 also know as a primitive type i.e int in C# whereas

bigint in Sql Server maps directly to int64 also know as a primitive type i.e long in C#

An explicit conversion if biginteger to integer has been defined here

Dougie answered 14/6, 2016 at 7:18 Comment(0)
G
-1

I managed to convert the (bigint) value returned from the DB using Convert.ToInt64.

ie

var Id = Convert.ToInt64(identityAsTable.Rows[0].Field<object>(0));
Grandpapa answered 1/9, 2021 at 15:28 Comment(0)
S
-2

For most of the cases it is long(int64) in c#

Squadron answered 4/2, 2020 at 10:1 Comment(1)
This seems to be just a repeat of the existing answers.Cecil
A
-3

if you are using bigint in your database table, you can use Long in C#

Amass answered 18/5, 2020 at 14:26 Comment(1)
This seems to be just a repeat of the existing answers.Cecil
R
-3

int(64) or long Datatype is equivalent to BigInt.

Roncesvalles answered 22/2, 2021 at 9:11 Comment(1)
This seems to be just a repeat of the existing answers.Cecil
K
-6

I was handling a bigint datatype to be shown in a DataGridView and made it like this

something = (int)(Int64)data_reader[0];
Kymric answered 11/8, 2014 at 14:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.