How to cast a SQL bigint to C# equivalent

16,090

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"

Share:
16,090
Rex_C
Author by

Rex_C

Fledgling software developer.

Updated on June 04, 2022

Comments

  • Rex_C
    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
      Tim about 9 years
      id = Convert.ToInt64(reader["id"]); maybe?
    • Matthew
      Matthew about 9 years
      try reader["id"].GetType() to see what the actual type you need to cast to.
    • Branko Dimitrijevic
      Branko Dimitrijevic about 9 years
      SQL Server's bigint is a 64-bit integer, which is equivalent to C#'s long. Can we see your SQL query and table/database definition? You are probably getting a wrong field.
    • Branko Dimitrijevic
      Branko Dimitrijevic about 9 years
      BTW, what happens when you try to cast to long? Do you get run-time or compile-time error?
    • Rex_C
      Rex_C about 9 years
      When 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
      Branko Dimitrijevic about 9 years
      OK, so this was a compile-time error, and it now works (both compile-time and run-time)?
  • Clever Neologism
    Clever Neologism about 9 years
    You could also do var id = 0L;
  • Pleun
    Pleun about 9 years
    True, 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
    Andrew Whitaker about 9 years
    In this case using var has nothing to do with anonymous types.
  • Andrew Whitaker
    Andrew Whitaker about 9 years
    You can use var with anonymous types (var x = new { .... }), but here id is an int from the moment it's declared. There's nothing anonymous about it.
  • Pleun
    Pleun about 9 years
    yes, you are right, I corrected to implicitly. Still I would not use var in these cases...
  • Andrew Whitaker
    Andrew Whitaker about 9 years
    @Pleun: I wouldn't either--very hard to tell what type id is.