What is the equivalent of bigint in C#?

C#.NetSql ServerBigint

C# Problem Overview


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

C# Solutions


Solution 1 - C#

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!

Solution 2 - C#

Int64 maps directly to BigInt.

Source

Solution 3 - C#

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.

Solution 4 - C#

Use a long datatype.

Solution 5 - C#

You can use long type or Int64

Solution 6 - C#

I think the equivalent is Int64

Solution 7 - C#

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

Solution 8 - C#

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

Solution 9 - C#

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

Solution 10 - C#

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

Solution 11 - C#

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

Solution 12 - C#

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

something = (int)(Int64)data_reader[0];

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAsadView Question on Stackoverflow
Solution 1 - C#Patrick KarcherView Answer on Stackoverflow
Solution 2 - C#ChaosPandionView Answer on Stackoverflow
Solution 3 - C#Robb SadlerView Answer on Stackoverflow
Solution 4 - C#Otávio DécioView Answer on Stackoverflow
Solution 5 - C#user5095242View Answer on Stackoverflow
Solution 6 - C#John BokerView Answer on Stackoverflow
Solution 7 - C#TechnacronView Answer on Stackoverflow
Solution 8 - C#Rajeev SirohiView Answer on Stackoverflow
Solution 9 - C#AbbasibhView Answer on Stackoverflow
Solution 10 - C#Tanvir MahtabView Answer on Stackoverflow
Solution 11 - C#Jeff MorettiView Answer on Stackoverflow
Solution 12 - C#RúbenView Answer on Stackoverflow