Sqlserver data type (for example, varchar) to SqlDbType struct (stringsqlTypeString) {SqlDbTypedbTypeSqlDbType. Variant; default value: Objectswitch (sqlTypeString) {caseint: dbTypeSqlDbType. I
// SQL server data type (for example, varchar) // convert to SqlDbType public static SqlDbType SqlTypeString2SqlType (string sqlTypeString) {SqlDbType dbType = SqlDbType. variant; // The default value is Object switch (sqlTypeString) {case "int": dbType = SqlDbType. I
// SQL server data type (for example, varchar)
// Convert to SqlDbType
Public static SqlDbType SqlTypeString2SqlType (string sqlTypeString)
{
SqlDbType dbType = SqlDbType. Variant; // The default value is Object.
Switch (sqlTypeString)
{
Case "int ":
DbType = SqlDbType. Int;
Break;
Case "varchar ":
DbType = SqlDbType. VarChar;
Break;
Case "bit ":
DbType = SqlDbType. Bit;
Break;
Case "datetime ":
DbType = SqlDbType. DateTime;
Break;
Case "decimal ":
DbType = SqlDbType. Decimal;
Break;
Case "float ":
DbType = SqlDbType. Float;
Break;
Case "image ":
DbType = SqlDbType. Image;
Break;
Case "money ":
DbType = SqlDbType. Money;
Break;
Case "ntext ":
DbType = SqlDbType. NText;
Break;
Case "nvarchar ":
DbType = SqlDbType. NVarChar;
Break;
Case "smalldatetime ":
DbType = SqlDbType. SmallDateTime;
Break;
Case "smallint ":
DbType = SqlDbType. SmallInt;
Break;
Case "text ":
DbType = SqlDbType. Text;
Break;
Case "bigint ":
DbType = SqlDbType. BigInt;
Break;
Case "binary ":
DbType = SqlDbType. Binary;
Break;
Case "char ":
DbType = SqlDbType. Char;
Break;
Case "nchar ":
DbType = SqlDbType. NChar;
Break;
Case "numeric ":
DbType = SqlDbType. Decimal;
Break;
Case "real ":
DbType = SqlDbType. Real;
Break;
Case "smallmoney ":
DbType = SqlDbType. SmallMoney;
Break;
Case "SQL _variant ":
DbType = SqlDbType. Variant;
Break;
Case "timestamp ":
DbType = SqlDbType. Timestamp;
Break;
Case "tinyint ":
DbType = SqlDbType. TinyInt;
Break;
Case "uniqueidentifier ":
DbType = SqlDbType. UniqueIdentifier;
Break;
Case "varbinary ":
DbType = SqlDbType. VarBinary;
Break;
Case "xml ":
DbType = SqlDbType. Xml;
Break;
}
Return dbType;
}
// Convert SqlDbType to C # Data Type
Public static Type SqlType2CsharpType (SqlDbType sqlType)
{
Switch (sqlType)
{
Case SqlDbType. BigInt:
Return typeof (Int64 );
Case SqlDbType. Binary:
Return typeof (Object );
Case SqlDbType. Bit:
Return typeof (Boolean );
Case SqlDbType. Char:
Return typeof (String );
Case SqlDbType. DateTime:
Return typeof (DateTime );
Case SqlDbType. Decimal:
Return typeof (Decimal );
Case SqlDbType. Float:
Return typeof (Double );
Case SqlDbType. Image:
Return typeof (Object );
Case SqlDbType. Int:
Return typeof (Int32 );
Case SqlDbType. Money:
Return typeof (Decimal );
Case SqlDbType. NChar:
Return typeof (String );
Case SqlDbType. NText:
Return typeof (String );
Case SqlDbType. NVarChar:
Return typeof (String );
Case SqlDbType. Real:
Return typeof (Single );
Case SqlDbType. SmallDateTime:
Return typeof (DateTime );
Case SqlDbType. SmallInt:
Return typeof (Int16 );
Case SqlDbType. SmallMoney:
Return typeof (Decimal );
Case SqlDbType. Text:
Return typeof (String );
Case SqlDbType. Timestamp:
Return typeof (Object );
Case SqlDbType. TinyInt:
Return typeof (Byte );
Case SqlDbType. Udt: // custom data type
Return typeof (Object );
Case SqlDbType. UniqueIdentifier:
Return typeof (Object );
Case SqlDbType. VarBinary:
Return typeof (Object );
Case SqlDbType. VarChar:
Return typeof (String );
Case SqlDbType. Variant:
Return typeof (Object );
Case SqlDbType. Xml:
Return typeof (Object );
Default:
Return null;
}
}
Code:
// Convert the Data Type in SQL server to the Data Type in C #
Public static Type SqlTypeString2CsharpType (string sqlTypeString)
{
SqlDbType dbTpe = SqlTypeString2SqlType (sqlTypeString );
Return SqlType2CsharpType (dbTpe );
}
// Convert the Data Type in SQL server to a string of the type in C #
Public static string SqlTypeString2CsharpTypeString (string sqlTypeString)
{
Type type = SqlTypeString2CsharpType (sqlTypeString );
Return type. Name;
}