Search code examples
c#.netsqlbigintulong

Can T-SQL store ulong's?


I want to store a C#.NET ulong into a T-SQL database. I don't see any provisions for doing this, as the SQL bigint has the same Min/Max values as a normal long.

Is there any way I can do this? Or is catching an OverflowException my only hope?


Solution

  • This should answer your question:

    http://social.msdn.microsoft.com/Forums/en-US/adodotnetdataproviders/thread/ff08c190-a981-4896-9542-3f64b95a84a2/

    You would use BigInt, you just have to be careful in how you convert the signed type back into an unsigned type in C#

    // This has not been tested
    unchecked
    {
        myUlong = myDataReader.GetInt64(...);
    }
    

    ...

    The other possibility is to use VarBinary with a length of 8, then convert the bytes to a ulong in C#