SQL Server typ C# bitbool tinyintbyte smallintshort Intint bigintlong realfloat Floatdouble moneydecimal datetimeDateTime Struna varcharstring ncharstring nvarcharstring textstring ntextstring imagebyte[] Binarybyte[] uniqueidentifierGuid/ SqlDbType转换为C#数据类型 public static Type SqlType2CsharpType(SqlDbType sqlType)
{ switch (sqlType)
{ případ SqlDbType.BigInt: return typeof(Int64); case SqlDbType.Binary: return typeof(Object); případ SqlDbType.Bit: return typeof(Boolean); case SqlDbType.Char: return typeof(String); case SqlDbType.DateTime: return typeof(DateTime); případ SqlDbType.Decimal: return typeof(Decimal); case SqlDbType.Float: return typeof(Double); case SqlDbType.Image: return typeof(Object); Případ SqlDbType.Int: return typeof(Int32); případ SqlDbType.Money: return typeof(Decimal); případ SqlDbType.NChar: return typeof(String); případ 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); případ SqlDbType.SmallMoney: return typeof(Decimal); case SqlDbType.Text: return typeof(String); případ SqlDbType.Timestamp: return typeof(Object); případ SqlDbType.TinyInt: return typeof(Byte); case SqlDbType.Udt:// custom data type return typeof(Object); případ SqlDbType.UniqueIdentifier: return typeof(Object); case SqlDbType.VarBinary: return typeof(Object); case SqlDbType.VarChar: return typeof(String); případ SqlDbType.Variant: return typeof(Object); případ SqlDbType.Xml: return typeof(Object); Výchozí: return null;
}
}Kopírovat obsah do schránkyKód:Datové typy SQL Serveru (např. varchar) Převod na typ SqlDbType public static SqlDbType SqlTypeString2SqlType (string sqlTypeString)
{ SqlDbType dbType = SqlDbType.Variant; Výchozí je Object
switch (sqlTypeString)
{ Případ "int": dbType = SqlDbType.Int; pauza; Případ "Varchar": dbType = SqlDbType.VarChar; pauza; Případ "bit": dbType = SqlDbType.Bit; pauza; Případ "DateTime": dbType = SqlDbType.DateTime; pauza; Případ "desetinný": dbType = SqlDbType.Decimal; pauza; Případ "float": dbType = SqlDbType.Float; pauza; Případ "Image": dbType = SqlDbType.Image; pauza; Případ "Money": dbType = SqlDbType.Money; pauza; Případ "Ntext": dbType = SqlDbType.NText; pauza; Případ "Nvarchar": dbType = SqlDbType.NVarChar; pauza; Případ "SmalldateTime": dbType = SqlDbType.SmallDateTime; pauza; Případ "smallint": dbType = SqlDbType.SmallInt; pauza; Případ "text": dbType = SqlDbType.Text; pauza; Případ "bigint": dbType = SqlDbType.BigInt; pauza; Případ "binární": dbType = SqlDbType.Binary; pauza; Případ "Char": dbType = SqlDbType.Char; pauza; Případ "NCHAR": dbType = SqlDbType.NChar; pauza; Případ "numerické": dbType = SqlDbType.Decimal; pauza; Případ "skutečný": dbType = SqlDbType.Real; pauza; Případ "smallmoney": dbType = SqlDbType.SmallMoney; pauza; Případ "sql_variant": dbType = SqlDbType.Variant; pauza; Případ "Časové razítko": dbType = SqlDbType.Timestamp; pauza; Případ "Tinyint": dbType = SqlDbType.TinyInt; pauza; Případ "uniqueIdentifier": dbType = SqlDbType.UniqueIdentifier; pauza; Případ "varbinární": dbType = SqlDbType.VarBinary; pauza; Případ "XML": dbType = SqlDbType.Xml; pauza;
} vrátit dbType;
} Kopírovat obsah do schránkyKód:Datové typy SQL Serveru, převedené na typy v C# public static Type SqlTypeString2CsharpType(string sqlTypeString)
{ SqlDbType dbTpe = SqlTypeString2SqlType(sqlTypeString);
return SqlType2CsharpType(dbTpe);
}
Převod datových typů v SQL Serveru na řetězce typů v C# public static string SqlTypeString2CsharpTypeString(string sqlTypeString)
{ Typ typu = SqlTypeString2CsharpType(sqlTypeString);
Typ vrácení. Jméno;
}
|