SQL Server Type |
C # type |
Bit |
Bool |
Tinyint |
Byte |
Smallint |
Short |
Int |
Int |
Bigint |
Long |
Real |
Float |
Float |
Double |
Money |
Decimal |
Datetime |
Datetime |
Char |
String |
Varchar |
String |
Nchar |
String |
Nvarchar |
String |
Text |
String |
Ntext |
String |
Image |
Byte [] |
Binary |
Byte [] |
Uniqueidentifier |
Guid |
/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;
}
}
Copy content to clipboard
Code :
// 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;
}
Copy content to clipboard
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;
}