How to cast a SQL bigint to C# equivalent
Solution 1
As the following table
illustrates, SQL Server's bigint
maps to .NET's Int64
structure:
long id = 0;
//Omitted code
if (reader.Read())
{
id = (long) reader["id"];
}
If this is throwing an InvalidCastException
that you totally don't have a bigint
in your dtaabase and you probably wanna be checking the actual type in the debugger using reader["id"].GetType()
.
By the way notice how I replace your var id = 0;
with long id = 0;
which is actually declaring a variable of type Int64
instead of Int32
. If you wanna use C# variable inference make sure you are doing it the proper way: var id = 0L;
By the way if you don't wanna be casting you may try this alternative:
long id = 0;
//Omitted code
if (reader.Read())
{
id = reader.GetInt64(reader.GetOrdinal("id"));
}
Solution 2
Perhaps the problem is your initial declaration with var
. This is an implicitly typed variable and assigned at the first use. And it is assumed to be an int since you assign it 0.
Replace the declaration with Int64 or long id = 0
and you should be fine
Solution 3
Use Int64
.
Source: "System.Data.SqlTypes Namespace"
Comments
-
Rex_C over 1 year
I'm trying to cast a bigint from SQL Server into its equivalent in c#, but Visual Studio keeps forcing me to try to cast it to int, which throws a 'specified cast not valid' error. I've tried to cast it to long, Int64, int, etc. What would be the proper way of doing that? My code looks like this:
var id = 0; //Omitted code if (reader.Read()) { id = (int) reader["id"]; } //More code
-
Tim about 9 years
id = Convert.ToInt64(reader["id"]);
maybe? -
Matthew about 9 yearstry
reader["id"].GetType()
to see what the actual type you need to cast to. -
Branko Dimitrijevic about 9 yearsSQL Server's
bigint
is a 64-bit integer, which is equivalent to C#'slong
. Can we see your SQL query and table/database definition? You are probably getting a wrong field. -
Branko Dimitrijevic about 9 yearsBTW, what happens when you try to cast to
long
? Do you get run-time or compile-time error? -
Rex_C about 9 yearsWhen I try to cast it to long, resharper yelled at me to cast to int. Explicitly setting the id type to Int64 worked.
-
Branko Dimitrijevic about 9 yearsOK, so this was a compile-time error, and it now works (both compile-time and run-time)?
-
-
Clever Neologism about 9 yearsYou could also do var id = 0L;
-
Pleun about 9 yearsTrue, but I prefer to restrict var to really anonymous types, like projections in Linq. Not if you know the intended datatype. It's kind of lazy coding and can get you into trouble :)
-
Andrew Whitaker about 9 yearsIn this case using
var
has nothing to do with anonymous types. -
Andrew Whitaker about 9 yearsYou can use
var
with anonymous types (var x = new { .... }
), but hereid
is anint
from the moment it's declared. There's nothing anonymous about it. -
Pleun about 9 yearsyes, you are right, I corrected to implicitly. Still I would not use var in these cases...
-
Andrew Whitaker about 9 years@Pleun: I wouldn't either--very hard to tell what type
id
is.