SQL Server データ型のマッピング

SQL Server と .NET Framework は異なる型システムを使用しています。 たとえば、.NET Framework の Decimal 構造体の最大小数点以下桁数は 28 ですが、SQL Server の decimal データ型と numeric データ型の最大小数点以下桁数は 38 です。 データを読み書きするときにデータの整合性を保つために、SqlDataReader では、.NET Framework の型を返すアクセサー メソッドと共に、System.Data.SqlTypes のオブジェクトを返す SQL Server 固有の型指定されたアクセサー メソッドを公開しています。 SQL Server の型と .NET Framework の型は、両方とも DbType および SqlDbType クラスの列挙によって表されます。これらは SqlParameter データ型を指定するときに使用できます。

推論される .NET Framework 型、DbType 列挙型と SqlDbType 列挙型、SqlDataReader のアクセサー メソッドを、次の表に示します。

SQL Server データベース エンジンの型 .NET Framework 型 SqlDbType 列挙 SqlDataReader SqlTypes の型指定されたアクセサー DbType 列挙 SqlDataReader DbType の型指定されたアクセサー
bigint Int64 BigInt GetSqlInt64 Int64 GetInt64
binary Byte[] VarBinary GetSqlBinary Binary GetBytes
bit Boolean Bit GetSqlBoolean Boolean GetBoolean
char String

Char[]
Char GetSqlString AnsiStringFixedLength

String
GetString

GetChars
date 1

(SQL Server 2008 以降)
DateTime Date1 GetSqlDateTime Date1 GetDateTime
DATETIME DateTime DateTime GetSqlDateTime DateTime GetDateTime
datetime2

(SQL Server 2008 以降)
DateTime DateTime2 None DateTime2 GetDateTime
datetimeoffset

(SQL Server 2008 以降)
DateTimeOffset DateTimeOffset none DateTimeOffset GetDateTimeOffset
decimal Decimal (10 進数型) Decimal GetSqlDecimal Decimal GetDecimal
FILESTREAM attribute (varbinary(max)) Byte[] VarBinary GetSqlBytes Binary GetBytes
float Double Float GetSqlDouble Double GetDouble
image Byte[] Binary GetSqlBinary Binary GetBytes
INT Int32 Int GetSqlInt32 Int32 GetInt32
money Decimal (10 進数型) Money GetSqlMoney Decimal GetDecimal
nchar String

Char[]
NChar GetSqlString StringFixedLength GetString

GetChars
ntext String

Char[]
NText GetSqlString String GetString

GetChars
数値 Decimal (10 進数型) Decimal GetSqlDecimal Decimal GetDecimal
nvarchar String

Char[]
NVarChar GetSqlString String GetString

GetChars
実数 Single Real GetSqlSingle Single GetFloat
rowversion Byte[] Timestamp GetSqlBinary Binary GetBytes
smalldatetime DateTime DateTime GetSqlDateTime DateTime GetDateTime
smallint Int16 SmallInt GetSqlInt16 Int16 GetInt16
smallmoney Decimal (10 進数型) SmallMoney GetSqlMoney Decimal GetDecimal
sql_variant Object 2 Variant GetSqlValue2 Object GetValue2
text String

Char[]
Text GetSqlString String GetString

GetChars
時間

(SQL Server 2008 以降)
TimeSpan Time none Time GetDateTime
タイムスタンプ Byte[] Timestamp GetSqlBinary Binary GetBytes
tinyint Byte TinyInt GetSqlByte Byte GetByte
UNIQUEIDENTIFIER Guid UniqueIdentifier GetSqlGuid Guid GetGuid
varbinary Byte[] VarBinary GetSqlBinary Binary GetBytes
varchar String

Char[]
VarChar GetSqlString AnsiStringString GetString

GetChars
xml Xml Xml GetSqlXml Xml none

1SqlParameterDbType プロパティを SqlDbType.Date に設定することはできません。
2sql_variant の基になる型がわかっている場合は、特定の型指定されたアクセサーを使用してください。

SQL Server ドキュメント

SQL Server のデータ型について詳しくは、「データ型 (Transact-SQL)」をご覧ください。

関連項目