C# Equivalent of SQL Server DataTypes
for the following SQL Server数据类型的数据类型的对应,是会在C #茶好吗?P></
精确数值解P></
1 2 3 4 5 6 7 8 9 | bigint numeric bit smallint decimal smallmoney int tinyint money |
数值近似P></
1 2 | float real |
日期和时间P></
1 2 3 4 5 6 | date datetimeoffset datetime2 smalldatetime datetime time |
character stringsP></
1 2 3 | char varchar text |
Unicode character stringsP></
1 2 3 | nchar nvarchar ntext |
二进制字符串P></
1 2 3 | binary varbinary image |
其他数据类型P></
1 2 3 4 5 6 7 | cursor timestamp hierarchyid uniqueidentifier sql_variant xml table |
(来源:的MSDN)P></
这是针对SQL Server 2005的。表的更新版本包括SQL Server 2008、SQL Server 2008 R2、SQL Server 2012和SQL Server 2014。
SQL Server数据类型及其.NET框架等效项下表列出了Microsoft SQL Server数据类型、System.Data.SqlTypes命名空间中SQL Server的公共语言运行时(CLR)中的等效数据类型以及Microsoft.NET框架中的本机CLR等效数据类型。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | SQL Server data type CLR data type (SQL Server) CLR data type (.NET Framework) varbinary SqlBytes, SqlBinary Byte[] binary SqlBytes, SqlBinary Byte[] varbinary(1), binary(1) SqlBytes, SqlBinary byte, Byte[] image None None varchar None None char None None nvarchar(1), nchar(1) SqlChars, SqlString Char, String, Char[] nvarchar SqlChars, SqlString String, Char[] nchar SqlChars, SqlString String, Char[] text None None ntext None None uniqueidentifier SqlGuid Guid rowversion None Byte[] bit SqlBoolean Boolean tinyint SqlByte Byte smallint SqlInt16 Int16 int SqlInt32 Int32 bigint SqlInt64 Int64 smallmoney SqlMoney Decimal money SqlMoney Decimal numeric SqlDecimal Decimal decimal SqlDecimal Decimal real SqlSingle Single float SqlDouble Double smalldatetime SqlDateTime DateTime datetime SqlDateTime DateTime sql_variant None Object User-defined type(UDT) None user-defined type table None None cursor None None timestamp None None xml SqlXml None |
SQL Server和.NET框架基于不同类型的系统。例如,.NET框架十进制结构的最大小数位数为28,而SQL Server十进制和数字数据类型的最大小数位数为38。点击这里有一个链接!细节
https://msdn.microsoft.com/en-us/library/cc716729(v=vs.110).aspx
SQL Server和.NET数据类型映射
如果有人在寻找从/到C和SQL Server格式的转换方法,下面是一个简单的实现:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | private readonly string[] SqlServerTypes = {"bigint","binary","bit", "char","date", "datetime","datetime2","datetimeoffset","decimal","filestream","float", "geography", "geometry", "hierarchyid", "image", "int","money", "nchar", "ntext", "numeric","nvarchar","real", "rowversion","smalldatetime","smallint","smallmoney","sql_variant","text", "time", "timestamp","tinyint","uniqueidentifier","varbinary","varchar","xml" }; private readonly string[] CSharpTypes = {"long", "byte[]","bool","char","DateTime","DateTime","DateTime", "DateTimeOffset","decimal","byte[]", "double","Microsoft.SqlServer.Types.SqlGeography","Microsoft.SqlServer.Types.SqlGeometry","Microsoft.SqlServer.Types.SqlHierarchyId","byte[]","int","decimal","string","string","decimal","string", "Single","byte[]", "DateTime", "short", "decimal", "object", "string","TimeSpan","byte[]", "byte", "Guid", "bite[]", "string", "string" }; public string ConvertSqlServerFormatToCSharp(string typeName) { var index = Array.IndexOf(SqlServerTypes, typeName); return index > -1 ? CSharpTypes[index] :"object"; } public string ConvertCSharpFormatToSqlServer(string typeName) { var index = Array.IndexOf(CSharpTypes, typeName); return index > -1 ? SqlServerTypes[index] : null; } |