Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Anybody got a C# function that maps the SQL datatype of a column to its CLR equivalent?

Tags:

c#

types

I'm sitting down to write a massive switch() statement to turn SQL datatypes into CLR datatypes in order to generate classes from MSSQL stored procedures. I'm using this chart as a reference. Before I get too far into what will probably take all day and be a huge pain to fully test, I'd like to call out to the SO community to see if anyone else has already written or found something in C# to accomplish this seemingly common and assuredly tedious task.

like image 613
Chris McCall Avatar asked Jun 29 '09 13:06

Chris McCall


People also ask

Are C good grades?

C+, C, C- indicates satisfactory performance. D+, D, D- indicates less than satisfactory performance. F indicates unsatisfactory performance (no credit: always include last date of attendance).

Is it OK to have AC on your report card?

If you receive one C during your high school years, it may ultimately affect your chances of getting into a top school. However, it won't automatically exclude you from one. Instead, it will make earning an acceptance a little harder for you, as you'll have to compensate in other areas.

Is it okay to be AC student?

Most students (and most parents) don't realize that in college, a C is a great grade. When the student who pulled a 4.0 in high school ends up with a 2.5 GPA in their first semester in college, their shock is real.

Do you pass if you get AC?

A passing grade is considered to be a C or above.


1 Answers

This is the one we use. You may want to tweak it (e.g. nullable/non-nullable types etc.) but it should save you most of the typing.

public static Type GetClrType(SqlDbType sqlType) {     switch (sqlType)     {         case SqlDbType.BigInt:             return typeof(long?);          case SqlDbType.Binary:         case SqlDbType.Image:         case SqlDbType.Timestamp:         case SqlDbType.VarBinary:             return typeof(byte[]);          case SqlDbType.Bit:             return typeof(bool?);          case SqlDbType.Char:         case SqlDbType.NChar:         case SqlDbType.NText:         case SqlDbType.NVarChar:         case SqlDbType.Text:         case SqlDbType.VarChar:         case SqlDbType.Xml:             return typeof(string);          case SqlDbType.DateTime:         case SqlDbType.SmallDateTime:         case SqlDbType.Date:         case SqlDbType.Time:         case SqlDbType.DateTime2:             return typeof(DateTime?);          case SqlDbType.Decimal:         case SqlDbType.Money:         case SqlDbType.SmallMoney:             return typeof(decimal?);          case SqlDbType.Float:             return typeof(double?);          case SqlDbType.Int:             return typeof(int?);          case SqlDbType.Real:             return typeof(float?);          case SqlDbType.UniqueIdentifier:             return typeof(Guid?);          case SqlDbType.SmallInt:             return typeof(short?);          case SqlDbType.TinyInt:             return typeof(byte?);          case SqlDbType.Variant:         case SqlDbType.Udt:             return typeof(object);          case SqlDbType.Structured:             return typeof(DataTable);          case SqlDbType.DateTimeOffset:             return typeof(DateTimeOffset?);          default:             throw new ArgumentOutOfRangeException("sqlType");     } } 
like image 184
Greg Beech Avatar answered Sep 18 '22 17:09

Greg Beech