czwartek, 19 listopada 2015

Mapping between sql and c# types

Numeric Mapping


SQL Server Type
Default CLR Type mapping used by O/R Designer and SQLMetal
BIT
TINYINT
INT
BIGINT
SMALLMONEY
MONEY
DECIMAL
NUMERIC
REAL/FLOAT(24)
FLOAT/FLOAT(53)


CLR Type
Default SQL Server Type used by DataContext.CreateDatabase
BIT
TINYINT
SMALLINT
INT
BIGINT
SMALLINT
INT
BIGINT
DECIMAL(20)
DECIMAL(29,4)
REAL
FLOAT

WARNING
The SQL Server MONEY and SMALLMONEY types, which are also paired with the CLR System.Decimal type by default, have a much smaller precision, which can result in overflow or data loss exceptions when saving data to the database.  

The default precision of SQL Server DECIMAL type (18 decimal digits to the left and right of the decimal point) is much smaller than the precision of the CLR T:System.Decimal type that it is paired with by default.This can result in precision loss when you save data to the database.


------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Text and XML Mapping
 

SQL Server Type
Default CLR Type mapping used by O/R Designer and SQLMetal
CHAR
NCHAR
VARCHAR
NVARCHAR
TEXT
NTEXT
XML


CLR Type
Default SQL Server Type used by DataContext.CreateDatabase
NCHAR(1)
NVARCHAR(4000)
NVARCHAR(4000)
Custom type implementing Parse() and ToString()
NVARCHAR(MAX)

------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Date and Time Mapping


SQL Server Type
Default CLR Type mapping used by O/R Designer and SQLMetal
SMALLDATETIME
DATETIME
DATETIME2
DATETIMEOFFSET
DATE
TIME


CLR Type
Default SQL Server Type used by DataContext.CreateDatabase
DATETIME
DATETIMEOFFSET
TIME


WARNING
The range and precision of the CLR System.DateTime type is greater than the range and precision of the SQL Server DATETIME type, which is the default type mapping for the DataContext.CreateDatabase method.To help avoid exceptions related to dates outside the range of DATETIME, use DATETIME2, which is available starting with Microsoft SQL Server 2008.DATETIME2 can match the range and precision of the CLR System.DateTime.

SQL Server dates have no concept of TimeZone, a feature that is richly supported in the CLR.TimeZone values are saved as is to the database without TimeZone conversion, regardless of the original DateTimeKind information.

------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Binary Mapping


SQL Server Type
Default CLR Type mapping used by O/R Designer and SQLMetal
BINARY(50)
VARBINARY(50)
VARBINARY(MAX)
VARBINARY(MAX) with the FILESTREAM attribute
IMAGE
TIMESTAMP


CLR Type
Default SQL Server Type used by DataContext.CreateDatabase
VARBINARY(MAX)
VARBINARY(MAX)
VARBINARY(MAX)

------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

 Miscellaneous Mapping


SQL Server Type
Default CLR Type mapping used by O/R Designer and SQLMetal
UNIQUEIDENTIFIER
SQL_VARIANT


CLR Type
Default SQL Server Type used by DataContext.CreateDatabase
UNIQUEIDENTIFIER
SQL_VARIANT

 https://msdn.microsoft.com/pl-pl/library/bb386947%28v=vs.110%29.aspx

SQL Server and the .NET Framework are based on different type systems.

To maintain data integrity when reading and writing data, the SqlDataReader exposes SQL Server–specific typed accessor methods that return objects of System.Data.SqlTypes as well as accessor methods that return .NET Framework types.

Both SQL Server types and .NET Framework types are also represented by enumerations in the DbType and SqlDbType classes, which you can use when specifying SqlParameter data types.

The following table shows the inferred .NET Framework type, the DbType and SqlDbType enumerations, and the accessor methods for the SqlDataReader.


SQL Server Database Engine type
.NET Framework type
SqlDbType enumeration
SqlDataReader SqlTypes typed accessor
DbType enumeration
SqlDataReader DbType typed accessor
bigint
Int64
binary
Byte[]
bit
Boolean
Bit 
char
String
Char[]
Char 
date
(SQL Server 2008 and later)
DateTime
Date 
Date 
datetime
DateTime
datetime2
(SQL Server 2008 and later)
DateTime
None
datetimeoffset
(SQL Server 2008 and later)
DateTimeOffset
none
decimal
Decimal
Decimal  
FILESTREAM attribute (varbinary(max))
Byte[]
float
Double
image
Byte[]
int
Int32
Int 
money
Decimal
nchar
String
Char[]
ntext
String
Char[]
numeric
Decimal
Decimal  
nvarchar
String
Char[]
real
Single
Real 
rowversion
Byte[]
smalldatetime
DateTime
smallint
Int16
smallmoney
Decimal
sql_variant
Object *
text
String
Char[]
Text 
time
(SQL Server 2008 and later)
TimeSpan
Time 
none
Time 
timestamp
Byte[]
tinyint
Byte
Byte 
uniqueidentifier
Guid
Guid 
varbinary
Byte[]
varchar
String
Char[]
xml
Xml
Xml 
Xml 
none

https://msdn.microsoft.com/en-us/library/cc716729%28v=vs.110%29.aspx

------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

SqlTypes

Each data type in SqlTypes has its equivalent data type in SQL Server, with the same underlying data representation. Many of them also have equivalent data types in the CLR. However, SqlDateTime, SqlDecimal, and SqlString have different underlying data structures with their corresponding .NET Framework data types.

The following table maps the members of the SqlTypes namespace to Microsoft SQL Server data types and to the members of the SqlDbType enumeration.


.NET Framework SqlTypes
Native SQL Server
.NET Framework SqlDbType
binary, image, timestamp, varbinary
Binary, Image, TimeStamp, VarBinary
bit
Bit
tinyint
TinyInt
binary, image, timestamp, varbinary
Binary, Image, TimeStamp, VarBinary
char, nchar, text, ntext, nvarchar, varchar
Char, NChar, Text, Ntext, NVarChar, VarChar
datetime, smalldatetime
DateTime, SmallDateTime
numeric, decimal
Decimal
float
Float
varbinary
VarBinary
uniqueidentifier
UniqueIdentifier
smallint
SmallInt
int
Int
bigint
BigInt
money, smallmoney
Money, SmallMoney
real
Real
char, nchar, text, ntext, nvarchar, varchar
Char, NChar, Text, Ntext, NVarChar, VarChar
xml
Xml


https://msdn.microsoft.com/en-us/library/system.data.sqltypes%28v=vs.110%29.aspx

https://gist.github.com/Romoku/5444566
https://msdn.microsoft.com/en-us/library/cc716729%28v=vs.110%29.aspx
https://msdn.microsoft.com/en-us/library/bb386947%28v=vs.110%29.aspx
https://msdn.microsoft.com/en-us/library/ms131092.aspx
https://social.msdn.microsoft.com/Forums/sqlserver/en-US/d496c385-e0d9-4b98-81a4-1181270ea030/sql-server-vs-c-data-types?forum=sqlexpress
http://stackoverflow.com/questions/1058322/anybody-got-a-c-sharp-function-that-maps-the-sql-datatype-of-a-column-to-its-clr
http://stackoverflow.com/questions/425389/c-sharp-equivalent-of-sql-server-datatypes
https://msdn.microsoft.com/en-us/library/cc716729%28v=vs.110%29.aspx
https://msdn.microsoft.com/pl-pl/library/system.data.sqltypes%28v=vs.110%29.aspx

Brak komentarzy:

Prześlij komentarz