276

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

Pang
  • 9,564
  • 146
  • 81
  • 122
Asad
  • 21,468
  • 17
  • 69
  • 94

12 Answers12

444

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!

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Patrick Karcher
  • 22,995
  • 5
  • 52
  • 66
79

Int64 maps directly to BigInt.

Source

sshow
  • 8,820
  • 4
  • 51
  • 82
ChaosPandion
  • 77,506
  • 18
  • 119
  • 157
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.

Pang
  • 9,564
  • 146
  • 81
  • 122
Robb Sadler
  • 157
  • 1
  • 2
  • Thanks for the numeric tip.. just helped me out big time! – jpshook Oct 26 '11 at 20:41
  • 10
    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). – Dan Hermann Jan 05 '14 at 13:21
  • Thanks Dan. FYI, still happening with SQL Azure (RTM) - 12.0.2000.8 & .NET v 4.5 – Gunnar Sep 11 '15 at 17:31
  • 1
    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 IdentityINT64 – dmarietta Dec 15 '22 at 18:34
9

Use a long datatype.

Otávio Décio
  • 73,752
  • 17
  • 161
  • 228
8

You can use long type or Int64

4

I think the equivalent is Int64

John Boker
  • 82,559
  • 17
  • 97
  • 130
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

Er Mayank
  • 1,017
  • 2
  • 16
  • 39
Asim Khan
  • 572
  • 6
  • 34
-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));
Jeff Moretti
  • 613
  • 5
  • 6
-2

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

Rajeev Sirohi
  • 75
  • 1
  • 3
-3

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

Abbasibh
  • 15
  • 1
-3

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

-5

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

something = (int)(Int64)data_reader[0];
p.campbell
  • 98,673
  • 67
  • 256
  • 322
Rúben
  • 1