SQL Server 数据类型的 C# 等效项
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/425389/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C# Equivalent of SQL Server DataTypes
提问by George Stocker
For the following SQL Server datatypes, what would be the corresponding datatype in C#?
对于以下 SQL Server 数据类型,C# 中对应的数据类型是什么?
Exact Numerics
精确数字
bigint
numeric
bit
smallint
decimal
smallmoney
int
tinyint
money
Approximate Numerics
近似数字
float
real
Date and Time
日期和时间
date
datetimeoffset
datetime2
smalldatetime
datetime
time
Character Strings
字符串
char
varchar
text
Unicode Character Strings
Unicode 字符串
nchar
nvarchar
ntext
Binary Strings
二进制字符串
binary
varbinary
image
Other Data Types
其他数据类型
cursor
timestamp
hierarchyid
uniqueidentifier
sql_variant
xml
table
(source: MSDN)
(来源:MSDN)
采纳答案by ?rjan J?mte
This is for SQL Server 2005. There are updated versions of the table for SQL Server 2008, SQL Server 2008 R2, SQL Server 2012and SQL Server 2014.
这是针对SQL Server 2005 的。SQL Server 2008、SQL Server 2008 R2、SQL Server 2012和SQL Server 2014的表有更新版本。
SQL Server Data Types and Their .NET Framework Equivalents
SQL Server 数据类型及其 .NET Framework 等效项
The following table lists Microsoft SQL Server data types, their equivalents in the common language runtime (CLR) for SQL Server in the System.Data.SqlTypesnamespace, and their native CLR equivalents in the Microsoft .NET Framework.
下表列出了 Microsoft SQL Server 数据类型、它们在System.Data.SqlTypes命名空间中SQL Server 的公共语言运行时 (CLR) 中的等效项,以及它们在 Microsoft .NET Framework 中的本机 CLR 等效项。
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
回答by Salman
SQL Server and the .NET Framework are based on different type systems. For example, the .NET Framework Decimal structure has a maximum scale of 28, whereas the SQL Server decimal and numeric data types have a maximum scale of 38. Click Here's a link! for detail
SQL Server 和 .NET Framework 基于不同的类型系统。例如,.NET Framework Decimal 结构的最大小数位数为 28,而 SQL Server 小数和数字数据类型的最大小数位数为 38。单击此处的链接!详情
https://msdn.microsoft.com/en-us/library/cc716729(v=vs.110).aspx
https://msdn.microsoft.com/en-us/library/cc716729(v=vs.110).aspx
回答by AndreFeijo
In case anybody is looking for methods to convert from/to C# and SQL Server formats, here goes a simple implementation:
如果有人正在寻找从/向 C# 和 SQL Server 格式转换的方法,这里有一个简单的实现:
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", "byte[]", "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;
}
Edit: fixed typo
编辑:修正错别字