Professional Documents
Culture Documents
The SqlTypes library is part of the base class library of the Microsoft .NET Framework. It is designed to provide
data types with the same semantics and precision as those found in the SQL Server database. This topic describes
the new semantics to .NET Framework programmers, and introduces the types implemented in the
System.Data.SqlTypes namespace that is included in the System.Data library.
This following table lists the topics in this section.
Nullability and Three-Value Logic Comparisons
Discusses how NULL values are handled with common language runtime (CLR) integration data types.
Collation and CLR Integration Data Types
Describes how collations are handled with CLR integration.
Handling Large Object (LOB) Parameters in the CLR
Describes how to pass LOB types between SQL Server and the CLR.
Mapping CLR Parameter Data
Shows data type mappings between SQL Server, CLR integration, and the .NET Framework.
Collation and CLR Integration Data Types
3/24/2017 1 min to read Edit Online
In the .NET Framework, the CompareInfo object handles collations. The .NET Framework string application
programming interfaces (APIs) use the CompareInfo property associated with the CultureInfo object of the
current thread to perform string comparisons. The default setting of the CultureInfo object is based on the
Microsoft Windows locale setting for the computer on which Microsoft SQL Server is running. This determines the
default comparison semantics, if no explicit CultureInfo is specified, for comparisons of System.String values.
SQL Server does not explicitly change the CompareInfo property to the database or server collation. If required,
users must set the appropriate CompareInfo property in their routines.
Parameter Collation
When you create a common language runtime (CLR) routine, and a parameter of a CLR method bound to the
routine is of type SQLString, SQL Server creates an instance of the parameter with the default collation of the
database containing the calling routine. If a parameter is not a SqlType (for example, String rather than
SQLString), the collation information from the database is not associated with the parameter.
See Also
SQL Server Data Types in the .NET Framework
Handling Large Object (LOB) Parameters in the CLR
3/24/2017 1 min to read Edit Online
Use SqlBytes and SqlChars to pass large object (LOB) binary type (varbinary(max)) and LOB character type
(nvarchar(max)) parameters, respectively. These types allow streaming the LOB values from the database to the
common language runtime (CLR) routine, instead of copying the entire value into managed space. SqlBinary and
SqlString should be used only for small binary and character string values.
See Also
SQL Server Data Types in the .NET Framework
Mapping CLR Parameter Data
3/24/2017 2 min to read Edit Online
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 Type (in System.Data.SqlTypes or CLR data type (.NET Framework)
Microsoft.SqlServer.Types)
SqlGeography is defined in
Microsoft.SqlServer.Types.dll, which is
installed with SQL Server and can be
downloaded from the SQL Server 2017
feature pack.
SqlGeometry is defined in
Microsoft.SqlServer.Types.dll, which is
installed with SQL Server and can be
downloaded from the SQL Server 2017
feature pack.
hierarchyid SqlHierarchyId None
SqlHierarchyId is defined in
Microsoft.SqlServer.Types.dll, which is
installed with SQL Server and can be
downloaded from the SQL Server 2017
feature pack.
[Microsoft.SqlServer.Server.SqlProcedure]
public static void PriceSum(out SqlInt32 value)
{ }
\<Microsoft.SqlServer.Server.SqlProcedure> _
Public Shared Sub PriceSum( \<Out()> ByRef value As SqlInt32)
End Sub
After the assembly is built and created in the database, the stored procedure is created in SQL Server with the
following Transact-SQL, which specifies a SQL Server data type of int as an OUTPUT parameter:
When the CLR stored procedure is called, the SqlInt32 data type is automatically converted to an int data type, and
returned to the calling program.
Not all CLR data types can be automatically converted to their equivalent SQL Server data types through an out
parameter, however. The following table lists these exceptions.
Decimal smallmoney
SqlMoney smallmoney
Decimal money
DateTime smalldatetime
SQLDateTime smalldatetime
Change History
UPDATED CONTENT
See Also
SQL Server Data Types in the .NET Framework
Nullability and Three-Value Logic Comparisons
3/24/2017 1 min to read Edit Online
If you are familiar with the SQL Server data types, you will find similar semantics and precision in the
System.Data.SqlTypes namespace in the .NET Framework. There are some differences, however, and this topic
covers the most important of these differences.
NULL Values
A primary difference between native common language runtime (CLR) data types and SQL Server data types is that
the former do not allow for NULL values, while the latter provide full NULL semantics.
Comparisons are affected by NULL values. When comparing two values x and y, if either x or y is NULL, then some
logical comparisons evaluate to an UNKNOWN value rather than true or false.
Precision
Decimal data types in the .NET Framework CLR have different maximum values than those of the numeric and
decimal data types in SQL Server. In addition, in the .NET Framework CLR decimal data types assume the maximum
precision. In the CLR for SQL Server, however, SqlDecimal provides the same maximum precision and scale, and
the same semantics as the decimal data type in SQL Server.
Overflow Detection
In the .NET Framework CLR, the addition of two very large numbers may not throw an exception. Instead, if no
check operator has been used, the returned result may "wrap around" as a negative integer. In
System.Data.SqlTypes, exceptions are thrown for all overflow and underflow errors, and divide-by-zero errors.
See Also
SQL Server Data Types in the .NET Framework