Question

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?

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top