ChatGPT解决这个技术问题 Extra ChatGPT

SQL Server 数据类型的 C# 等效项

对于以下 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

我认为这就是您可能正在寻找的:Mapping CLR Parameter Data

H
H. Pauwelyn

这是给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 数据类型 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[] 位 SqlBoolean Boolean tinyint SqlByte Byte smallint SqlInt16 Int16 int SqlInt32 Int32 bigint SqlInt64 Int64 smallmoney SqlMoney 十进制钱 SqlMoney 十进制数字 SqlDecimal 十进制十进制 SqlDecimal 十进制实数 SqlSingle 单浮点 SqlDouble SqlDTime SqlDateTime DateTime无 Object 用户自定义类型(UDT) 无 用户自定义类型表 无 无 游标 无 无 时间戳 无 无 xml SqlXml 无


.Net 框架中的 short 应使用哪种 CLR 数据类型 (SQL Server)?
@yogeshpatel,short (docs.microsoft.com/en-us/dotnet/csharp/language-reference/…) 等于此清单中的 System.Int16。所以这将是 SQL Server 中的 smallint。
重要提示:SQL 数据类型“float”默认为“float(54)”,它转换为 C#“double”。但是,SQL 数据类型“float(24)”仅转换为 C#“float”。因此,如果您不需要很多位的精度并且想要提高性能/内存,请在 SQL 中使用 float(24) 并在 C# 中键入“float”。
M
Must.Tek

SQL Server 和 .Net 数据类型映射

https://i.stack.imgur.com/CBhE9.png


J
JanW

如果有人正在寻找从/转换为 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;
}

编辑:修正错字


您的方法 ConvertCSharpFormatToSqlServer 将始终返回找到的第一个实例,因为 CSharp 类型不是唯一的,即“byte[]”将始终返回“binary”,即使它映射到 5 个其他 Sql Server 类型。
@David 虽然您所说的在技术上并没有错,但是当您使用 ConvertSqlServerFormatToCSharp 方法时它是有道理的。这只是一个示例,您可以随时根据自己的需要对其进行修改。
S
Salman

SQL Server 和 .NET Framework 基于不同的类型系统。例如,.NET Framework Decimal 结构的最大小数位数为 28,而 SQL Server 十进制和数字数据类型的最大小数位数为 38。单击此处 a link!详情

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


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