SQL 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 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://a 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:
SQL Server data type (for example: varchar)
Convert to SqlDbType type
public static SqlDbType Sqltypestring2sqltype (String sqltypestring)
{
SqlDbType DbType = sqldbtype.variant;//Default to 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;
}
Code:
// sql server中的数据类型,转换为C#中的类型类型
public static Type SqlTypeString2CsharpType(string sqlTypeString)
{
SqlDbType dbTpe = SqlTypeString2SqlType(sqlTypeString);
return SqlType2CsharpType(dbTpe);
}
// 将sql server中的数据类型,转化为C#中的类型的字符串
public static string SqlTypeString2CsharpTypeString(string sqlTypeString)
{
Type type = SqlTypeString2CsharpType(sqlTypeString);
return type.Name;
}
SQL server type corresponds to C # type [go]