C#等效的SQL Server数据类型

Geo*_*ker 551 .net c# sql-server

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

精确的数字

bigint
numeric
bit
smallint
decimal
smallmoney
int
tinyint
money
Run Code Online (Sandbox Code Playgroud)

近似数字

float
real
Run Code Online (Sandbox Code Playgroud)

日期和时间

date
datetimeoffset
datetime2
smalldatetime
datetime
time
Run Code Online (Sandbox Code Playgroud)

字符串

char
varchar
text
Run Code Online (Sandbox Code Playgroud)

Unicode字符串

nchar
nvarchar
ntext
Run Code Online (Sandbox Code Playgroud)

二进制字符串

binary
varbinary
image
Run Code Online (Sandbox Code Playgroud)

其他数据类型

cursor
timestamp
hierarchyid
uniqueidentifier
sql_variant
xml
table
Run Code Online (Sandbox Code Playgroud)

(来源:MSDN)

Örj*_*mte 1024

这适用于SQL Server 2005.SQL Server 2008,SQL Server 2008 R2,SQL 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
Run Code Online (Sandbox Code Playgroud)

  • .NET中的int与此表中的Int32相同,因此它也是SQL Server中的int. (2认同)
  • @yogeshpatel,“ short”(https://docs.microsoft.com/zh-cn/dotnet/csharp/language-reference/keywords/short)等于此清单中的System.Int16。因此,在SQL Server中将为smallint。 (2认同)

Mus*_*Tek 18

SQL Server和.Net数据类型映射

SQL Server和.Net数据类型映射


And*_*ijo 13

如果有人正在寻找从/向 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;
}
Run Code Online (Sandbox Code Playgroud)

编辑:修正错别字

  • 您的方法 ConvertCSharpFormatToSqlServer 将始终仅返回找到的第一个实例,因为 CSharp 类型不是唯一的,即“byte[]”将始终返回“二进制”,即使它映射到 5 个其他 Sql Server 类型。 (2认同)

小智 7

SQL Server和.NET Framework基于不同类型的系统.例如,.NET Framework Decimal结构的最大比例为28,而SQL Server十进制和数字数据类型的最大比例为38.单击此处为链接!细节

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

  • 倾向于答案的不是我,但理想情况下你应该回答这个问题,而不是提供与之相关的链接. (7认同)

Yit*_*erg 7

public static string FromSqlType(string sqlTypeString)
{
    if (! Enum.TryParse(sqlTypeString, out Enums.SQLType typeCode))
    {
        throw new Exception("sql type not found");
    }
    switch (typeCode)
    {
        case Enums.SQLType.varbinary:
        case Enums.SQLType.binary:
        case Enums.SQLType.filestream:
        case Enums.SQLType.image:
        case Enums.SQLType.rowversion:
        case Enums.SQLType.timestamp://?
            return "byte[]";
        case Enums.SQLType.tinyint:
            return "byte";
        case Enums.SQLType.varchar:
        case Enums.SQLType.nvarchar:
        case Enums.SQLType.nchar:
        case Enums.SQLType.text:
        case Enums.SQLType.ntext:
        case Enums.SQLType.xml:
            return "string";
        case Enums.SQLType.@char:
            return "char";
        case Enums.SQLType.bigint:
            return "long";
        case Enums.SQLType.bit:
            return "bool";
        case Enums.SQLType.smalldatetime:
        case Enums.SQLType.datetime:
        case Enums.SQLType.date:
        case Enums.SQLType.datetime2:
            return "DateTime";
        case Enums.SQLType.datetimeoffset:
            return "DateTimeOffset";
        case Enums.SQLType.@decimal:
        case Enums.SQLType.money:
        case Enums.SQLType.numeric:
        case Enums.SQLType.smallmoney:
            return "decimal";
        case Enums.SQLType.@float:
            return "double";
        case Enums.SQLType.@int:
            return "int";
        case Enums.SQLType.real:
            return "Single";
        case Enums.SQLType.smallint:
            return "short";
        case Enums.SQLType.uniqueidentifier:
            return "Guid";
        case Enums.SQLType.sql_variant:
            return "object";
        case Enums.SQLType.time:
            return "TimeSpan";
        default:
            throw new Exception("none equal type");
    }
}

public enum SQLType
{
    varbinary,//(1)
    binary,//(1)
    image,
    varchar,
    @char,
    nvarchar,//(1)
    nchar,//(1)
    text,
    ntext,
    uniqueidentifier,
    rowversion,
    bit,
    tinyint,
    smallint,
    @int,
    bigint,
    smallmoney,
    money,
    numeric,
    @decimal,
    real,
    @float,
    smalldatetime,
    datetime,
    sql_variant,
    table,
    cursor,
    timestamp,
    xml,
    date,
    datetime2,
    datetimeoffset,
    filestream,
    time,
}
Run Code Online (Sandbox Code Playgroud)