Saturday, February 25, 2012

Money, SQL Server and C#

Argggg...

Please help.

I need to store money in SQL Server.
I am using C# and a stored procedure to insert.

How do you accomplish this? Specifically what datatype should the money variable be in C# during the insert? It's initial value is a string as it is entered from a text field.

I have been trying for over an hour now to simply insert money into SQL Server using C# and have it stored as a money type!

Thanks

Try using decimal. hth

|||

I did get it working using decimal.. thanks!

Decimal rate = Convert.ToDecimal(txtRate.Text);

Stored procedure looks like this.

SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GO

ALTER PROCEDURE sp_UpdateHorsepowerRate

@.HorsePower int,
@.Rate money,
@.UserId uniqueidentifier,
@.EmployerId int

AS

INSERT INTO EmployerHorsePower (HorsePower, Rate, UserId, EmployerId) VALUES (@.HorsePower, @.Rate, @.UserId, @.EmployerId)

GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO

Rob

|||

I would recommend that you not begin your stored procedures with "sp" (refer to:http://www.sql-server-performance.com/rd_optimizing_sp_recompiles.asp).

Also, when setting up the SqlParameter be sure that you consider the precision and scale. For themoney datatype, it would be precision=19, scale = 4.

No comments:

Post a Comment