C # equivalente de SQL Server DataTypes

594

Para os seguintes tipos de dados do SQL Server, qual seria o tipo de dados correspondente em C #?

Numéricos Exatos

bigint
numeric
bit
smallint
decimal
smallmoney
int
tinyint
money

Numéricos Aproximados

float
real

Data e hora

date
datetimeoffset
datetime2
smalldatetime
datetime
time

Cadeias de caracteres

char
varchar
text

Cadeias de caracteres Unicode

nchar
nvarchar
ntext

Cordas binárias

binary
varbinary
image

Outros tipos de dados

cursor
timestamp
hierarchyid
uniqueidentifier
sql_variant
xml
table

(fonte: MSDN )

George Stocker
fonte
1
Eu acho que isso é o que você pode estar procurando: Mapeamento CLR Parâmetro Dados
Andrew Hare

Respostas:

1093

Isso é para o SQL Server 2005 . Existem versões atualizadas da tabela para SQL Server 2008 , SQL Server 2008 R2 , SQL Server 2012 e SQL Server 2014 .

Tipos de dados do SQL Server e seus equivalentes do .NET Framework

A tabela a seguir lista os tipos de dados do Microsoft SQL Server, seus equivalentes no Common Language Runtime (CLR) para SQL Server no espaço para nome System.Data.SqlTypes e seus equivalentes CLR nativos no Microsoft .NET Framework.

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
Örjan Jämte
fonte
2
int no .NET é o mesmo que Int32 nesta tabela, portanto, também seria um int no SQL Server.
Örjan
Qual tipo de dados CLR (SQL Server) deve ser usado shortna estrutura .Net?
Yogesh Patel
3
@yogeshpatel, short( docs.microsoft.com/en-us/dotnet/csharp/language-reference/… ) é igual a System.Int16 nesta listagem. Portanto, isso seria pequeno no SQL Server.
Örjan Jämte 16/04
34

Mapeamento de tipos de dados do SQL Server e .Net

Mapeamento de tipos de dados do SQL Server e .Net

Must.Tek
fonte
7

O SQL Server e o .NET Framework são baseados em sistemas de tipos diferentes. Por exemplo, a estrutura decimal do .NET Framework tem uma escala máxima de 28, enquanto os tipos de dados decimais e numéricos do SQL Server têm uma escala máxima de 38. Clique em Aqui está um link ! para detalhes

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

Salman
fonte
Você pode explicar por que recebo -1 para esta resposta?
Salman
8
Não fui eu quem votou negativamente na resposta, mas, idealmente, você deve responder à pergunta, não fornecer um link com ela.
Esteban Verbel
6

Caso alguém esteja procurando métodos para converter de / para formatos C # e SQL Server, aqui está uma implementação simples:

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

Editar: erro de digitação fixo

AndreFeijo
fonte