George Duckett
George Duckett

Reputation: 32428

Going from an SqlParameter to the SQL type

I have an SqlParameter object and I want to get the equivalent sql for the data type, e.g. varchar(50), int, decimal(1, 2)

I've started looking at building it dynamically by switching on the SqlDbType to determine if i need to use the Size property etc however this is very long-winded and error prone.

Is there a way of doing this built into the .net framework?

Upvotes: 0

Views: 100

Answers (1)

ta.speot.is
ta.speot.is

Reputation: 27214

SQL Server Type Mappings has the information you require.

I've started looking at building it dynamically by switching on the SqlDbType to determine if i need to use the Size property etc however this is very long-winded and error prone.

It's unlikely you can improve upon this, the page says this much:

SQL Server and the .NET Framework are based on different type systems. For example, the .NET Framework Decimal structure has a maximum scale of 28, whereas the SQL Server decimal and numeric data types have a maximum scale of 38

For the example of Decimal and decimal, you could always fix the scale at 28. That is, always take the highest precision required and do not try to vary it.

Upvotes: 1

Related Questions