C#等效于SQL Server数据类型


593

对于以下SQL Server数据类型,C#中对应的数据类型是什么?

精确数值

bigint
numeric
bit
smallint
decimal
smallmoney
int
tinyint
money

近似数值

float
real

日期和时间

date
datetimeoffset
datetime2
smalldatetime
datetime
time

字串

char
varchar
text

Unicode字符串

nchar
nvarchar
ntext

二进制字符串

binary
varbinary
image

其他数据类型

cursor
timestamp
hierarchyid
uniqueidentifier
sql_variant
xml
table

(来源:MSDN


1
我认为这是您可能正在寻找的:映射CLR参数数据
Andrew Hare

Answers:


1090

这是针对SQL Server 2005的。该表有SQL Server 2008SQL Server 2008 R2SQL Server 2012SQL Server 2014的更新版本。

SQL Server数据类型及其.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

2
.NET中的int与此表中的Int32相同,因此在SQL Server中也将为int。
与OrjanJämte

short.Net框架应使用哪种CLR数据类型(SQL Server)?
Yogesh Patel '18

3
@yogeshpatel,shortdocs.microsoft.com/en-us/dotnet/csharp/language-reference/…)等于此清单中的System.Int16。因此,在SQL Server中将为smallint。
与OrjanJämte



6

万一有人在寻找将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;
}

编辑:固定错别字

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.