What data type to use for representing currency in C# and Sql Server 2005/2008?

ASP.NET, SQL Server 2 Comments »

This post will be short and sweet.  What data type should you use to represent currencies in C#? – Decimal

If you use double, you will end up with rounding errors on more precise numbers, and if you are writing applications that support multiple currencies, namely currencies that use very large numbers, such as the Indonesia Rupiah ($1 = 9,345.8 Indonesian Rupiahs) you will also get doubles represented in scientific notation when they reach very large values (think trillions).

In C# use Decimal

Now for SQL Server 2005/2008 I believe the best practice would be to use the money datatype if you are dealing exclusively with U.S. dollars, otherwise you should use decimal to get more precise values.

Simple SQL Cursor Example

SQL Server 10 Comments »

If you’re reading this blog, chances are, you know how to write/use a SQL cursor. But this is more for my benefit, and maybe even yours. Just a quick cursor example, so that I can easily reference the syntax if I ever need to quickly look it up, since I always seem to forget it.  Enjoy.

DECLARE @CONTACTID INT 

--will need to declare at least one variable
--in this case, to store values when iterating through cursor
DECLARE SIMPLE_CURSOR CURSOR FOR SELECT ID FROM CONTACT OPEN SIMPLE_CURSOR FETCH NEXT FROM SIMPLE_CURSOR --Start the cursor INTO @CONTACTID WHILE @@FETCH_STATUS = 0 --while there is a loaded record, keep processing BEGIN --do whatever you need to do print ('This is where the magic happens! Do whatever you need to do (update/insert/delete/stored proc/etc.') FETCH NEXT FROM SIMPLE_CURSOR INTO @CONTACTID --fetch next record END CLOSE SIMPLE_CURSOR --close and deallocate DEALLOCATE SIMPLE_CURSOR

Last updated by at .

WP Theme & Icons by N.Design Studio
Entries RSS Comments RSS Log in