0

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?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
George Duckett
  • 31,770
  • 9
  • 95
  • 162
  • 3
    What are you doing that requires you to know this? 99% of the time you can let the driver deal with this. Show your code... – tomfanning Aug 09 '13 at 09:33
  • I've got an SqlCommand with a select statement (and parameters). I want to create an sql command that will create a stored procedure that has the same parameters and returns the same results. – George Duckett Aug 09 '13 at 09:43

1 Answers1

1

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.

ta.speot.is
  • 26,914
  • 8
  • 68
  • 96
  • I understand what you're saying (given a .net type I don't have enough info to get an sql type), but I've got an `SqlParameter`, which has information re. size/scale/precision so I should be able to get the exact sql declaration. I suppose what I should take from this answer is that I'll need to implement this myself. – George Duckett Aug 09 '13 at 10:01