Question

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?

Was it helpful?

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#

OTHER TIPS

I know it's not the same, but would this do:

select convert(decimal(38, 0), 12345678901234567890123456789012345678)

The maximum for an unsigned long seems to be 18,446,744,073,709,551,615 which is significantly less than this decimal can store. There might be issues converting, but I'm sure that a couple of wrapper functions in your application would sort it pretty quickly.

For me Neil N's solution didn't work. Visual Studio kept complaining that I can't assign a long value to a ulong variable, no matter the unchecked.

What did work in the end was simply

ulong fileSize = (ulong)(long)reader["fileSize"];

(withoutunchecked).

In the SQLite DB the value is stored as long, so very high values become negative and sorting them on SQL-side fails. But after getting the ulong values Linq can do its magic

items = items.OrderByDescending(x => x.FileSize).ToList();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top