C# Equivalent of SQL Server Datatypes

C# Equivalent of SQL Server DataTypes

This is for SQL Server 2005. There are updated versions of the table for SQL Server 2008, SQL Server 2008 R2, SQL Server 2012 and SQL Server 2014.

SQL Server Data Types and Their .NET Framework Equivalents

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 typeCLR data type (SQL Server)CLR data type (.NET Framework)
varbinarySqlBytes, SqlBinaryByte[]
binarySqlBytes, SqlBinaryByte[]
varbinary(1), binary(1)SqlBytes, SqlBinarybyte, Byte[]
imageNoneNone
varcharNoneNone
charNoneNone
nvarchar(1), nchar(1)SqlChars, SqlStringChar, String, Char[]
nvarcharSqlChars, SqlStringString, Char[]
ncharSqlChars, SqlStringString, Char[]
textNoneNone
ntextNoneNone
uniqueidentifierSqlGuidGuid
rowversionNoneByte[]
bitSqlBooleanBoolean
tinyintSqlByteByte
smallintSqlInt16Int16
intSqlInt32Int32
bigintSqlInt64Int64
smallmoneySqlMoneyDecimal
moneySqlMoneyDecimal
numericSqlDecimalDecimal
decimalSqlDecimalDecimal
realSqlSingleSingle
floatSqlDoubleDouble
smalldatetimeSqlDateTimeDateTime
datetimeSqlDateTimeDateTime
sql_variantNoneObject
User-defined type(UDT)Noneuser-defined type
tableNoneNone
cursorNoneNone
timestampNoneNone
xmlSqlXmlNone

What is the equivalent SQL Server type for the C# long type?

The mapping table is clear - BIGINT is the equivalent of Int64 (which is long in C#).

What are equivalent C# data types for SQL Server's date, time and datetimeoffset?

Here are the equivalent CLR data types for date, time and datetimeoffset SQL Server data types:

date - DateTime, Nullable<DateTime>

time - TimeSpan, Nullable<TimeSpan>

datetimeoffset - DateTimeOffset, Nullable<DateTimeOffset>

Note that you can find a listing of all SQL Server data types and their CLR equivalents here, Mapping CLR Parameter Data

What is the equivalent of SQL Server 2005 IMAGE Datatype in C#

it won't be supported in future

No, not in future. It is not supported now. Extended support for SQL Server 2005 ended 3 years ago!

You can see SQL Server data type mappings in the official documentation. For image you should use DbType.Binary.

SQL XML datatype equivalent in C# .net

I would avoid using a string directly - and especially in you case where you just need to store the data. Either go with SqlXml or something else that gives a reader. For example like this:

var param = cmd.Parameters.Add("@xml", SqlDbType.Xml);
var doc = XDocument.Load(xmlFilePath);
param.Value = doc.CreateReader();
cmd.ExecuteNonQuery();

(Connection and Command object left out).

The important advice here is just not to use a string object yourself, but as you have discovered already ymmv...

What is the equivalent datatype of SQL Server's Numeric in C#

There isn't a direct equivalent, in that there are no built-in .NET types which allow you to specify the precision/scale explicitly as far as I'm aware. There's no fixed-point type like NUMERIC.

decimal and double are the common floating point types in .NET, with decimal implementing decimal floating point (like NUMERIC in T-SQL) and double implementing binary floating point behaviour (like FLOAT and REAL in T-SQL). (There's float as well, which is a smaller binary floating point type.)

You should choose between decimal and double based on what values you're going to represent - I typically think of "man-made", artificial values (particularly money) as being appropriate for decimal, and continuous, natural values (such as physical dimensions) as being appropriate for double.



Related Topics



Leave a reply



Submit