Wednesday, September 09, 2009

Mapping Sql Data Type to System Type

//C#
private Type DeriveCLSType(SqlDataType _SqlDataType)
{
Type typ = GetType();
switch (_SqlDataType)
{
case SqlDataType.BigInt:
typ = Type.GetType("System.Int64");
break;

case SqlDataType.Bit:
typ = Type.GetType("System.Boolean");
break;

case SqlDataType.Char:
case SqlDataType.VarChar:
case SqlDataType.VarCharMax:
typ = Type.GetType("System.String");
break;

case SqlDataType.DateTime:
case SqlDataType.SmallDateTime:
typ = Type.GetType("System.DateTime");
break;

case SqlDataType.Decimal:
case SqlDataType.Money:
case SqlDataType.SmallMoney:
typ = Type.GetType("System.Decimal");
break;

case SqlDataType.Int:
typ = Type.GetType("System.Int32");
break;

case SqlDataType.NChar:
case SqlDataType.NText:
case SqlDataType.NVarChar:
case SqlDataType.NVarCharMax:
case SqlDataType.Text:
typ = Type.GetType("System.String");
break;

case SqlDataType.Real:
case SqlDataType.Numeric:
case SqlDataType.Float:
typ = Type.GetType("System.Double");
break;

case SqlDataType.Timestamp:
case SqlDataType.Binary:
typ = Type.GetType("System.Byte");
break;

case SqlDataType.TinyInt:
case SqlDataType.SmallInt:
typ = Type.GetType("System.Int16");
break;

case SqlDataType.UniqueIdentifier:
typ = Type.GetType("System.Guid");
break;

case SqlDataType.UserDefinedDataType:
case SqlDataType.UserDefinedType:
case SqlDataType.Variant:
case SqlDataType.Image:
typ = Type.GetType("System.Object");
break;

default:
typ = Type.GetType("System.String");
break;
}
return typ;
}

No comments: