C# Equivalent of SQL Server DataTypes C# Equivalent of SQL Server DataTypes sql-server sql-server

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                          Nonevarchar                       None                          Nonechar                          None                          Nonenvarchar(1), nchar(1)         SqlChars, SqlString           Char, String, Char[]     nvarchar                      SqlChars, SqlString           String, Char[] nchar                         SqlChars, SqlString           String, Char[] text                          None                          Nonentext                         None                          Noneuniqueidentifier              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                          Nonetimestamp                     None                          None xml                           SqlXml                        None


SQL Server and .Net Data Type mapping

SQL Server and .Net Data Type mapping


In case anybody is looking for methods to convert from/to C# and SQL Server formats, here goes a simple implementation:

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;}

Edit: fixed typo