What am I supposed to use when handling a value in C#, which is bigint for an SQL Server database?
This question is related to
c#
.net
sql-server
bigint
Int64
maps directly to BigInt
.
Use a long datatype.
You can use long
type or Int64
For most of the cases it is long(int64) in c#
int(64) or long Datatype is equivalent to BigInt.
I was handling a bigint datatype to be shown in a DataGridView and made it like this
something = (int)(Int64)data_reader[0];
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
if you are using bigint in your database table, you can use Long in 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.
I think the equivalent is Int64
Source: Stackoverflow.com