Wednesday, March 7, 2012

CLR (C#) to SQL Server 2005 System.Decimal mapping

Can someone tell me how are you supposed to map the CLR data type System.Decimal to a SQL server 2005 data type decimal? Maybe decimal(28,0) or what?

I don't think that decimal(28,0) is a good match. While decimal(28,0) seems to be similar, I think that you will be disappointed when you accomplish something like this:


SELECT HorrorsWhatHappened = cast( 123456.898794857 AS decimal(28,0))


HorrorsWhatHappened
123457


You 'should' know the precision/scale, and then map to the same precision/scale.

References:

http://msdn2.microsoft.com/en-us/library/4e5xt97a(VS.80).aspx

Also, from: http://geekswithblogs.net/lorint/Default.aspx

Read down to: Finding the scale in decimal values in .NET

...

So now if you have a pile of decimal numbers and you want to create a new table in SQL to hold all of them, you can find the one with the highest scale, and define that column appropriately when you go to use CREATE TABLE or ALTER TABLE. ...

No comments:

Post a Comment