C#等效於SQL Server數據類型

對於如下SQL Server數據類型,C#中對應的數據類型是什麼? sql

精確數值 ide

bigint
numeric
bit
smallint
decimal
smallmoney
int
tinyint
money

近似數值 ui

float
real

日期和時間 spa

date
datetimeoffset
datetime2
smalldatetime
datetime
time

字符串 code

char
varchar
text

Unicode字符串 orm

nchar
nvarchar
ntext

二進制字符串 xml

binary
varbinary
image

其餘數據類型 ci

cursor
timestamp
hierarchyid
uniqueidentifier
sql_variant
xml
table

(來源: MSDN字符串


#1樓

SQL Server和.NET Framework基於不一樣的類型系統。 例如,.NET Framework十進制結構的最大比例爲28,而SQL Server十進制和數字數據類型的最大比例爲38。單擊此處是連接 ! 詳細 get

https://msdn.microsoft.com/zh-CN/library/cc716729(v=vs.110).aspx


#2樓

SQL Server和.Net數據類型映射

SQL Server和.Net數據類型映射


#3樓

萬一有人在尋找將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;
}

編輯:固定錯別字


#4樓

這是針對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
相關文章
相關標籤/搜索