SQL Server Type |
C # Types |
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 conversion to C # data type
Public StaticType Sqltype2csharptype (SqlDbType sqltype) {Switch(sqltype) { CaseSqldbtype.bigint:return typeof(Int64); Casesqldbtype.binary:return typeof(Object); CaseSqldbtype.bit:return typeof(Boolean); CaseSqlDbType.Char:return typeof(String); CaseSqldbtype.datetime:return typeof(DateTime); CaseSqldbtype.decimal:return typeof(Decimal); Casesqldbtype.float:return typeof(Double); CaseSqldbtype.image:return typeof(Object); CaseSqlDbType.Int:return typeof(Int32); CaseSqldbtype.money:return typeof(Decimal); CaseSqlDbType.NChar:return typeof(String); CaseSqldbtype.ntext:return typeof(String); CaseSqlDbType.NVarChar:return typeof(String); CaseSqldbtype.real:return typeof(single); CaseSqldbtype.smalldatetime:return typeof(DateTime); CaseSqldbtype.smallint:return typeof(Int16); CaseSqldbtype.smallmoney:return typeof(Decimal); CaseSqldbtype.text:return typeof(String); CaseSqldbtype.timestamp:return typeof(Object); CaseSqldbtype.tinyint:return typeof(Byte); CaseSqlDbType.Udt://Custom Data Types return typeof(Object); CaseSqldbtype.uniqueidentifier:return typeof(Object); Casesqldbtype.varbinary:return typeof(Object); CaseSqlDbType.VarChar:return typeof(String); Casesqldbtype.variant:return typeof(Object); CaseSqldbtype.xml:return typeof(Object); default: return NULL; }}
//SQL Server data type (for example: varchar)//convert to SqlDbType type Public StaticSqlDbType Sqltypestring2sqltype (stringsqltypestring) {SqlDbType DbType= Sqldbtype.variant;//The default 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; } returnDbType;}
//data types in SQL Server, converted to type types in C # Public StaticType Sqltypestring2csharptype (stringsqltypestring) {SqlDbType Dbtpe=Sqltypestring2sqltype (sqltypestring); returnSqltype2csharptype (DBTPE);}//Converts a data type in SQL Server into a string of type in C # Public Static stringSqltypestring2csharptypestring (stringsqltypestring) {Type type=Sqltypestring2csharptype (sqltypestring); returntype. Name;}
SQL Server type corresponds to C # type