locked
SQL Server vs. C# Data Types RRS feed

  • Question

  • Is there a cross reference available between the SQL Server data types and C# data types? SQL Server appears to have many more 'primitive' data types than C#.

    Thanks


    Tuesday, August 26, 2008 5:06 AM

Answers

  • Hi, Paolo,

           I hope the following information will be helpful. Please click here to get the original doc.

     

    SQL Server 2008 Books Online
    Mapping CLR Parameter Data

    The following table lists Microsoft SQL Server data types, their equivalents in the common language runtime (CLR) for SQL Server in the System.Data.SqlTypes namespace, and their native CLR equivalents in the Microsoft .NET Framework.

    SQL Server data type

    CLR data type (SQL Server)

    CLR data type (.NET Framework)

    bigint

    SqlInt64

    Int64, Nullable<Int64>

    binary

    SqlBytes, SqlBinary

    Byte[]

    bit

    SqlBoolean

    Boolean, Nullable<Boolean>

    char

    None

    None

    cursor

    None

    None

    date

    SqlDateTime

    DateTime, Nullable<DateTime>

    datetime

    SqlDateTime

    DateTime, Nullable<DateTime>

    datetime2

    SqlDateTime

    DateTime, Nullable<DateTime>

    DATETIMEOFFSET

    None

    DateTimeOffset, Nullable<DateTimeOffset>

    decimal

    SqlDecimal

    Decimal, Nullable<Decimal>

    float

    SqlDouble

    Double, Nullable<Double>

    image

    None

    None

    int

    SqlInt32

    Int32, Nullable<Int32>

    money

    SqlMoney

    Decimal, Nullable<Decimal>

    nchar

    SqlChars, SqlString

    String, Char[]

    ntext

    None

    None

    numeric

    SqlDecimal

    Decimal, Nullable<Decimal>

    nvarchar

    SqlChars, SqlString

    SQLChars is a better match for data transfer and access, and SQLString is a better match for performing String operations.

    String, Char[]

    nvarchar(1), nchar(1)

    SqlChars, SqlString

    Char, String, Char[], Nullable<char>

    real

    SqlSingle

    Single, Nullable<Single>

    rowversion

    None

    Byte[]

    smallint

    SqlInt16

    Int16, Nullable<Int16>

    smallmoney

    SqlMoney

    Decimal, Nullable<Decimal>

    sql_variant

    None

    Object

    table

    None

    None

    text

    None

    None

    time

    TimeSpan

    TimeSpan, Nullable<TimeSpan>

    timestamp

    None

    None

    tinyint

    SqlByte

    Byte, Nullable<Byte>

    uniqueidentifier

    SqlGuid

    Guid, Nullable<Guid>

    User-defined type(UDT)

    None

    The same class that is bound to the user-defined type in the same assembly or a dependent assembly.

    varbinary

    SqlBytes, SqlBinary

    Byte[]

    varbinary(1), binary(1)

    SqlBytes, SqlBinary

    byte, Byte[], Nullable<byte>

    varchar

    None

    None

    xml

    SqlXml

    None

    Tuesday, August 26, 2008 5:53 AM

All replies

  • Hi, Paolo,

           I hope the following information will be helpful. Please click here to get the original doc.

     

    SQL Server 2008 Books Online
    Mapping CLR Parameter Data

    The following table lists Microsoft SQL Server data types, their equivalents in the common language runtime (CLR) for SQL Server in the System.Data.SqlTypes namespace, and their native CLR equivalents in the Microsoft .NET Framework.

    SQL Server data type

    CLR data type (SQL Server)

    CLR data type (.NET Framework)

    bigint

    SqlInt64

    Int64, Nullable<Int64>

    binary

    SqlBytes, SqlBinary

    Byte[]

    bit

    SqlBoolean

    Boolean, Nullable<Boolean>

    char

    None

    None

    cursor

    None

    None

    date

    SqlDateTime

    DateTime, Nullable<DateTime>

    datetime

    SqlDateTime

    DateTime, Nullable<DateTime>

    datetime2

    SqlDateTime

    DateTime, Nullable<DateTime>

    DATETIMEOFFSET

    None

    DateTimeOffset, Nullable<DateTimeOffset>

    decimal

    SqlDecimal

    Decimal, Nullable<Decimal>

    float

    SqlDouble

    Double, Nullable<Double>

    image

    None

    None

    int

    SqlInt32

    Int32, Nullable<Int32>

    money

    SqlMoney

    Decimal, Nullable<Decimal>

    nchar

    SqlChars, SqlString

    String, Char[]

    ntext

    None

    None

    numeric

    SqlDecimal

    Decimal, Nullable<Decimal>

    nvarchar

    SqlChars, SqlString

    SQLChars is a better match for data transfer and access, and SQLString is a better match for performing String operations.

    String, Char[]

    nvarchar(1), nchar(1)

    SqlChars, SqlString

    Char, String, Char[], Nullable<char>

    real

    SqlSingle

    Single, Nullable<Single>

    rowversion

    None

    Byte[]

    smallint

    SqlInt16

    Int16, Nullable<Int16>

    smallmoney

    SqlMoney

    Decimal, Nullable<Decimal>

    sql_variant

    None

    Object

    table

    None

    None

    text

    None

    None

    time

    TimeSpan

    TimeSpan, Nullable<TimeSpan>

    timestamp

    None

    None

    tinyint

    SqlByte

    Byte, Nullable<Byte>

    uniqueidentifier

    SqlGuid

    Guid, Nullable<Guid>

    User-defined type(UDT)

    None

    The same class that is bound to the user-defined type in the same assembly or a dependent assembly.

    varbinary

    SqlBytes, SqlBinary

    Byte[]

    varbinary(1), binary(1)

    SqlBytes, SqlBinary

    byte, Byte[], Nullable<byte>

    varchar

    None

    None

    xml

    SqlXml

    None

    Tuesday, August 26, 2008 5:53 AM
  • Gary: just what I was looking for. Many thanks.
    Tuesday, August 26, 2008 6:17 AM
  • All -- This given mapping matrix is nice. However, it could be better. For example, when actually writing C# code, instead of "Int64" one should use "long", by convention and best-practice. The same goes for "float" and "int" and etc. That is what Microsoft has recommended in the past, and I guess they probably still recommend it. The matrix ought to reflect such conventions. Regardless, it is a decent matrix. HTH. Thanks. -- Mark Kamoski
    • Proposed as answer by mkamoski2 Thursday, January 26, 2012 9:27 PM
    Thursday, January 26, 2012 9:27 PM
  • Since we don't have a mapping between varchar and nvarchar, should we use nvarchar if we want to use EF?

    Also, for smallint should I use Int16?

    Thanks.


    For every expert, there is an equal and opposite expert. - Becker's Law


    My blog

    Tuesday, September 4, 2012 8:26 PM