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 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 |
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
Saving Any File to in the Database, Just Convert It to a Byte Array
How to Load an Assembly to Appdomain with All References Recursively
Get the Generated SQL Statement from a SQLcommand Object
How to Write a JSON File in C#
Printing All Contents of Array in C#
In a "Using" Block Is a SQLconnection Closed on Return or Exception
Distinct by Property of Class with Linq
Is There a Way of Setting Culture for a Whole Application? All Current Threads and New Threads
C# - Faster Alternatives to Setpixel and Getpixel for Bitmaps for Windows Forms App
How to Let an Asmx File Output JSON
How to Access Named Capturing Groups in a .Net Regex
How to Set an SQL Server Connection String
How to Bind a Tabcontrol to a Collection of Viewmodels
The Cast to Value Type 'Int32' Failed Because the Materialized Value Is Null
Hide Console Window from Process.Start C#
Detailed Explanation of Variable Capture in Closures
Making a Property Deserialize But Not Serialize with JSON.Net