次の方法で共有


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
[バイナリ] Byte[] VarBinary GetSqlBinary Binary GetBytes
ビット ボーリアン Bit GetSqlBoolean Boolean GetBoolean
焦がす

Char[]
Char GetSqlString AnsiStringFixedLength=

String
GetString

GetChars
日付 1

(SQL Server 2008 以降)
日付と時間 Date 1 GetSqlDateTime Date 1 GetDateTime
datetime 日付と時間 DateTime GetSqlDateTime DateTime GetDateTime
datetime2

(SQL Server 2008 以降)
日付と時間 DateTime2 無し DateTime2 GetDateTime
datetimeoffset (デイトタイムオフセット)

(SQL Server 2008 以降)
DateTimeOffset (日付と時刻のオフセット) DateTimeOffset なし DateTimeOffset GetDateTimeOffset
小数点 Decimal (10 進数型) Decimal GetSqlDecimal Decimal GetDecimal
FILESTREAM 属性 (varbinary(max)) Byte[] VarBinary GetSqlBytes Binary GetBytes
フロート ダブル Float GetSqlDouble Double GetDouble
画像 Byte[] Binary GetSqlBinary Binary GetBytes
INT Int32 Int GetSqlInt32 Int32 GetInt32
お金 Decimal (10 進数型) Money GetSqlMoney Decimal GetDecimal
nchar

Char[]
NChar GetSqlString StringFixedLength GetString

GetChars
ntext

Char[]
NText GetSqlString String GetString

GetChars
数値 Decimal (10 進数型) Decimal GetSqlDecimal Decimal GetDecimal
nvarchar(エヌヴァーチャー)

Char[]
NVarChar GetSqlString String GetString

GetChars
実数 シングル Real GetSqlSingle Single GetFloat
rowversion Byte[] Timestamp GetSqlBinary Binary GetBytes
smalldatetime(スモールデイトタイム) 日付と時間 DateTime GetSqlDateTime DateTime GetDateTime
スモールイント Int16 SmallInt GetSqlInt16 Int16 GetInt16
少額のお金 Decimal (10 進数型) SmallMoney GetSqlMoney Decimal GetDecimal
SQLバリアント オブジェクト 2 Variant GetSqlValue 2 Object GetValue 2
テキスト

Char[]
Text GetSqlString String GetString

GetChars
時間

(SQL Server 2008 以降)
タイムスパン Time なし Time GetTimeSpan
タイムスタンプ Byte[] Timestamp GetSqlBinary Binary GetBytes
tinyint バイト TinyInt GetSqlByte Byte GetByte
UNIQUEIDENTIFIER Guid UniqueIdentifier GetSqlGuid Guid GetGuid
varbinary Byte[] VarBinary GetSqlBinary Binary GetBytes
varchar

Char[]
VarChar GetSqlString $ GetString

GetChars
xml Xml Xml GetSqlXml Xml なし

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

SQL Server ドキュメント

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

関連項目