C#中与SQL Server数据类型对应的数据类型

744

以下是 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)


3
我认为这可能是你正在寻找的内容:映射CLR参数数据 - Andrew Hare
5个回答

1325

这是针对 SQL Server 2005 的。有更新版本的表适用于 SQL Server 2008, SQL Server 2008 R2, SQL Server 2012SQL Server 2014

SQL Server 数据类型及其 .NET Framework 等效项

下表列出了 Microsoft SQL Server 数据类型,在 SQL Server 的公共语言运行时 (CLR) 中的等效项,以及它们在 Microsoft .NET Framework 中的本机 CLR 等效项。

SQL Server 数据类型 CLR 数据类型(SQL Server) CLR 数据类型(.NET Framework)
varbinary SqlBytes、SqlBinary Byte[]
binary SqlBytes、SqlBinary Byte[]
varbinary(1)、binary(1) SqlBytes、SqlBinary byte、Byte[]
image
varchar
char
nvarchar(1)、nchar(1) SqlChars、SqlString Char、String、Char[]
nvarchar SqlChars、SqlString String、Char[]
nchar SqlChars、SqlString String、Char[]
text
ntext
uniqueidentifier SqlGuid Guid
rowversion 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 Object
用户定义类型(UDT) 用户定义类型
table
cursor
timestamp
xml SqlXml

1
在 .Net Framework 中,应该使用哪种 CLR 数据类型(SQL Server)来表示 short? - Yogesh Patel
8
@yogeshpatel,short(https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/short)在此列表中等同于System.Int16。因此,在SQL Server中,这将是smallint。 - Örjan Jämte
1
重要提示:SQL 数据类型 "float" 默认为 "float(54)",其转换为 C#的类型为 "double"。 然而,SQL 数据类型 "float(24)" 转换为 C# 的类型为 "float"。 因此,如果您不需要那么多位数的精度并且想要提高性能/内存,则请在 SQL 中使用 float(24) 类型,并在 C# 中使用 "float" 类型。 - Jason

95

SQL Server和.Net数据类型映射

SQL Server和.Net数据类型映射


19

如果有人正在寻找将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;
}

编辑:修复了拼写错误


2
你的方法ConvertCSharpFormatToSqlServer将始终只返回第一个找到的实例,因为CSharp类型不是唯一的,例如"byte[]"将始终返回"binary",即使它映射到其他5个Sql Server类型。 - David
1
@David 虽然你说的在技术上并没有错,但只有在使用 ConvertSqlServerFormatToCSharp 方法时才有意义。这只是一个例子,你可以根据自己的需求进行修改。 - AndreFeijo

8

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,
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接