[C#] C #과 동등한 SQL Server DataTypes

다음 SQL Server 데이터 형식의 경우 C #의 해당 데이터 형식은 무엇입니까?

정확한 숫자

bigint
numeric
bit
smallint
decimal
smallmoney
int
tinyint
money

대략적인 숫자

float
real

날짜와 시간

date
datetimeoffset
datetime2
smalldatetime
datetime
time

문자열

char
varchar
text

유니 코드 문자 스트링

nchar
nvarchar
ntext

이진 문자열

binary
varbinary
image

다른 자료형

cursor
timestamp
hierarchyid
uniqueidentifier
sql_variant
xml
table

(출처 : MSDN )



답변

이것은 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


답변

SQL Server 및 .Net 데이터 형식 매핑

SQL Server 및 .Net 데이터 형식 매핑


답변

SQL Server와 .NET Framework는 다른 유형 시스템을 기반으로합니다. 예를 들어 .NET Framework Decimal 구조의 최대 크기는 28이고 SQL Server 10 진수 및 숫자 데이터 형식의 최대 크기는 38입니다. 여기를 클릭하십시오 링크 ! 자세한 내용

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


답변

누구나 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;
}

편집 : 고정 오타


답변