Mapowania typów danych programu SQL Server

Dotyczy: .NET Framework .NET Standard

Pobieranie ADO.NET

SQL Server i .NET Framework opierają się na różnych systemach typów. Na przykład struktura .NET Framework Decimal ma maksymalną skalę 28, natomiast typy danych dziesiętnych i liczbowych programu SQL Server mają maksymalną skalę 38. Aby zachować integralność danych podczas odczytywania i zapisywania danych, SqlDataReader uwidacznia metody dostępu specyficzne dla programu SQL Server, które zwracają obiekty System.Data.SqlTypes oraz metody dostępu zwracające typy programu .NET Framework. Typy SQL Server i .NET Framework są również reprezentowane przez wyliczenia w klasach DbType i SqlDbType, których można użyć podczas określania typów danych SqlParameter.

W poniższej tabeli przedstawiono wywnioskowany typ .NET Framework, wyliczenia DbType i SqlDbType oraz metody dostępu dla elementu SqlDataReader.

Typ silnika bazy danych SQL Server Typ programu .NET Framework SqlDbType, wyliczenie SqlDataReader SqlTypes, typizowane metody dostępu wyliczenie DbType SqlDataReader DbType, typizowane akcesorium
bigint Int64 BigInt GetSqlInt64 Int64 GetInt64
binarny Byte[] VarBinary GetSqlBinary Binary GetBytes
bit logiczny Bit GetSqlBoolean Boolean GetBoolean
char Sznurek

Char[]
Char GetSqlString AnsiStringFixedLength.

String
GetString

GetChars
data 1

(SQL Server 2008 i nowsze)
DateTime Date 1 GetSqlDateTime Date 1 GetDateTime
data/godzina DateTime DateTime GetSqlDateTime DateTime GetDateTime
typ data_czas2

(SQL Server 2008 i nowsze)
DateTime DateTime2 Żaden DateTime2 GetDateTime
datetimeoffset

(SQL Server 2008 i nowsze)
DateTimeOffset DateTimeOffset none DateTimeOffset GetDateTimeOffset
liczba dziesiętna Decimal Decimal GetSqlDecimal Decimal GetDecimal
ATRYBUT FILESTREAM (varbinary(max)) Byte[] VarBinary GetSqlBytes Binary GetBytes
float Double Float GetSqlDouble Double GetDouble
obraz Byte[] Binary GetSqlBinary Binary GetBytes
int Int32 Int GetSqlInt32 Int32 GetInt32
pieniądze Decimal Money GetSqlMoney Decimal GetDecimal
nchar Sznurek

Char[]
NChar GetSqlString StringFixedLength GetString

GetChars
ntekst Sznurek

Char[]
NText GetSqlString String GetString

GetChars
numeryczny Decimal Decimal GetSqlDecimal Decimal GetDecimal
nvarchar Sznurek

Char[]
NVarChar GetSqlString String GetString

GetChars
real 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 SmallMoney GetSqlMoney Decimal GetDecimal
sql_variant Obiekt 2 Variant GetSqlValue 2 Object GetValue 2
SMS Sznurek

Char[]
Text GetSqlString String GetString

GetChars
time

(SQL Server 2008 i nowsze)
TimeSpan Time none Time GetDateTime
sygnatura czasowa Byte[] Timestamp GetSqlBinary Binary GetBytes
tinyint Byte TinyInt GetSqlByte Byte GetByte
uniqueidentifier Przewodnik UniqueIdentifier GetSqlGuid Guid GetGuid
varbinary Byte[] VarBinary GetSqlBinary Binary GetBytes
Varchar powiedział: Sznurek

Char[]
VarChar GetSqlString AnsiString, String GetString

GetChars
XML XML Xml GetSqlXml Xml none
JSON Sznurek Json GetSqlJson none none
wektor none Vector GetSqlVector none none

1 Nie można ustawić właściwości DbType na SqlParameter.
2. Użyj skonkretyzowanego typu metody dostępu, jeśli znasz podstawowy typ sql_variant.

Dokumentacja programu SQL Server

Aby uzyskać więcej informacji na temat typów danych programu SQL Server, zobacz Typy danych (Transact-SQL).