Mastering the Connection: A Complete Guide to Mapping SQL Server and C# Data Types
Let's dive into the definitive mapping table, highlighting the best practices for handling everything from simple integers to complex time stamps and binary data.
SQL Server to C# Data Type Mapping
| SQL Server Data Type | C# Data Type | Notes |
|---|---|---|
| int | int | 32-bit signed integer (System.Int32) |
| smallint | short | 16-bit signed integer (System.Int16) |
| tinyint | byte | 8-bit unsigned integer (System.Byte) |
| bigint | long | 64-bit signed integer (System.Int64) |
| bit | bool | Boolean (true / false) |
| decimal(p,s) / numeric(p,s) | decimal | High precision (often used for money, currency, etc.) |
| money, smallmoney | decimal | Monetary data type |
| float | double | 64-bit floating-point (System.Double) |
| real | float | 32-bit floating-point (System.Single) |
| char, nchar, varchar, nvarchar, text, ntext | string | All variable and fixed-length character types map to C# string |
| date, datetime, smalldatetime, datetime2 | DateTime | Date and time without timezone info |
| datetimeoffset | DateTimeOffset | Includes timezone offset information |
| time | TimeSpan | Represents a time of day or time interval |
| uniqueidentifier | Guid | Globally Unique Identifier |
| binary, varbinary, image | byte[] | For file storage or any binary data |