Mapping dei tipi di dati dei provider di dati .NET Framework sui tipi di dati .NET Framework
L'oggetto DataSet di ADO.NET è indipendente da qualsiasi specifica origine dati. I dati in un DataSet vengono recuperati da un'origine dati e le modifiche vengono applicate nell'origine dati utilizzando un DataAdapter. Questo significa che quando un DataAdapter riempie un DataTable in un DataSet con valori provenienti da un'origine dati, i tipi di dati che si ottengono nelle colonne del DataTable sono tipi .NET Framework anziché tipi specifici del provider di dati .NET Framework utilizzato per la connessione all'origine dati.
In modo analogo, quando un DataReader restituisce un valore da un'origine dati, il valore ottenuto viene memorizzato in una variabile locale con un tipo .NET Framework.
Per le operazioni Fill del DataAdapter e i metodi Get del DataReader, il tipo .NET Framework viene dedotto dal tipo restituito dal provider di dati .NET Framework. Si consiglia di utilizzare i metodi delle funzioni di accesso tipizzate di DataReader quando si conosce il tipo specifico del valore che viene restituito. I metodi delle funzioni di accesso tipizzate garantiscono prestazioni migliori restituendo un valore come un tipo .NET Framework specifico ed eliminando quindi la necessità di un'ulteriore conversione del tipo. SqlDataReader espone i metodi delle funzioni di accesso tipizzate specifici per SQL Server se un tipo .NET Framework non soddisfa le esigenze dell'applicazione. I metodi delle funzioni di accesso tipizzate specifici per SQL Server restituiscono oggetti di System.Data.SqlType.
Nelle tabelle riportate di seguito sono elencati i tipi .NET Framework dedotti dai tipi di dati di Microsoft SQL Server, OLE DB e ODBC. Sono inoltre elencati i metodi delle funzioni di accesso tipizzate per DataReader.
Nota I valori null per qualsiasi tipo di dati dei provider di dati .NET Framework sono rappresentati da DBNull.Value.
Provider di dati .NET Framework per SQL Server
Tipo di SQL Server | Tipo di .NET Framework | Funzione di accesso tipizzata .NET Framework | Funzione di accesso tipizzata SqlType |
---|---|---|---|
bigint | Int64 | GetInt64() | GetSqlInt64() |
binary | Byte[] | GetBytes() | GetSqlBinary() |
bit | Boolean | GetBoolean() | GetSqlBit() |
char | String
Char[] |
GetString()
GetChars() |
GetSqlString() |
datetime | DateTime | GetDateTime() | GetSqlDateTime() |
decimal | Decimal | GetDecimal() | GetSqlDecimal() |
float | Double | GetDouble() | GetSqlDouble() |
image | Byte[] | GetBytes() | GetSqlBinary() |
int | Int32 | GetInt32() | GetSqlInt32() |
money | Decimal | GetDecimal() | GetSqlMoney() |
nchar | String
Char[] |
GetString()
GetChars() |
GetSqlString() |
ntext | String
Char[] |
GetString()
GetChars() |
GetSqlString() |
numeric | Decimal | GetDecimal() | GetSqlDecimal() |
nvarchar | String
Char[] |
GetString()
GetChars() |
GetSqlString() |
real | Single | GetFloat() | GetSqlSingle() |
smalldatetime | DateTime | GetDateTime() | GetSqlDateTime() |
smallint | Int16 | GetInt16() | GetSqlInt16() |
smallmoney | Decimal | GetDecimal() | GetSqlDecimal() |
sql_variant | Object * | GetValue() * | GetSqlValue() * |
text | String
Char[] |
GetString()
GetChars() |
GetSqlString() |
timestamp | Byte[] | GetBytes() | GetSqlBinary() |
tinyint | Byte | GetByte() | GetSqlByte() |
uniqueidentifier | Guid | GetGuid() | GetSqlGuid() |
varbinary | Byte[] | GetBytes() | GetSqlBinary() |
varchar | String
Char[] |
GetString()
GetChars() |
GetSqlString() |
* È consigliabile utilizzare una specifica funzione di accesso tipizzata se è noto il tipo sottostante di sql_variant.
Provider di dati .NET Framework per OLE DB
Tipo ADO | Tipo OLE DB | Tipo di .NET Framework | Funzione di accesso tipizzata .NET Framework |
---|---|---|---|
adBigInt | DBTYPE_I8 | Int64 | GetInt64() |
adBinary | DBTYPE_BYTES | Byte[] | GetBytes() |
adBoolean | DBTYPE_BOOL | Boolean | GetBoolean() |
adBSTR | DBTYPE_BSTR | String | GetString() |
adChapter | DBTYPE_HCHAPTER | Supportato mediante DataReader. Vedere Recupero di dati mediante DataReader. | GetValue() |
adChar | DBTYPE_STR | String | GetString() |
adCurrency | DBTYPE_CY | Decimal | GetDecimal() |
adDate | DBTYPE_DATE | DateTime | GetDateTime() |
adDBDate | DBTYPE_DBDATE | DateTime | GetDateTime() |
adDBTime | DBTYPE_DBTIME | DateTime | GetDateTime() |
adDBTimeStamp | DBTYPE_DBTIMESTAMP | DateTime | GetDateTime() |
adDecimal | DBTYPE_DECIMAL | Decimal | GetDecimal() |
adDouble | DBTYPE_R8 | Double | GetDouble() |
adError | DBTYPE_ERROR | ExternalException | GetValue() |
adFileTime | DBTYPE_FILETIME | DateTime | GetDateTime() |
adGUID | DBTYPE_GUID | Guid | GetGuid() |
adIDispatch | DBTYPE_IDISPATCH * | Object | GetValue() |
adInteger | DBTYPE_I4 | Int32 | GetInt32() |
adIUnknown | DBTYPE_IUNKNOWN * | Object | GetValue() |
adNumeric | DBTYPE_NUMERIC | Decimal | GetDecimal() |
adPropVariant | DBTYPE_PROPVARIANT | Object | GetValue() |
adSingle | DBTYPE_R4 | Single | GetFloat() |
adSmallInt | DBTYPE_I2 | Int16 | GetInt16() |
adTinyInt | DBTYPE_I1 | Byte | GetByte() |
adUnsignedBigInt | DBTYPE_UI8 | UInt64 | GetValue() |
adUnsignedInt | DBTYPE_UI4 | UInt32 | GetValue() |
adUnsignedSmallInt | DBTYPE_UI2 | UInt16 | GetValue() |
adUnsignedTinyInt | DBTYPE_UI1 | Byte | GetByte() |
adVariant | DBTYPE_VARIANT | Object | GetValue() |
adWChar | DBTYPE_WSTR | String | GetString() |
adUserDefined | DBTYPE_UDT | non supportato | |
adVarNumeric | DBTYPE_VARNUMERIC | non supportato |
* Per i tipi OLE DB DBTYPE_IUNKNOWN e DBTYPE_IDISPATCH, il riferimento all'oggetto è una rappresentazione con marshalling del puntatore.
Provider di dati .NET Framework per ODBC
Tipo ODBC | Tipo di .NET Framework | Funzione di accesso tipizzata .NET Framework |
---|---|---|
SQL_BIGINT | Int64 | GetInt64() |
SQL_BINARY | Byte[] | GetBytes() |
SQL_BIT | Boolean | GetBoolean() |
SQL_CHAR | String
Char[] |
GetString()
GetChars() |
SQL_DECIMAL | Decimal | GetDecimal() |
SQL_DOUBLE | Double | GetDouble() |
SQL_GUID | Guid | GetGuid() |
SQL_INTEGER | Int32 | GetInt32() |
SQL_LONG_VARCHAR | String
Char[] |
GetString()
GetChars() |
SQL_LONGVARBINARY | Byte[] | GetBytes() |
SQL_NUMERIC | Decimal | GetDecimal() |
SQL_REAL | Single | GetFloat() |
SQL_SMALLINT | Int16 | GetInt16() |
SQL_TINYINT | Byte | GetByte() |
SQL_TYPE_TIMES | DateTime | GetDateTime() |
SQL_TYPE_TIMESTAMP | DateTime | GetDateTime() |
SQL_VARBINARY | Byte[] | GetBytes() |
SQL_WCHAR | String
Char[] |
GetString()
GetChars() |
SQL_WLONGVARCHAR | String
Char[] |
GetString()
GetChars() |
SQL_WVARCHAR | String
Char[] |
GetString()
GetChars() |
Provider di dati .NET Framework per Oracle
Tipo Oracle | Tipo di .NET Framework | Funzione di accesso tipizzata .NET Framework | Funzioni di accesso tipizzate OracleType |
---|---|---|---|
BFILE | Byte[] | GetBytes() | GetOracleBFile() |
BLOB | Byte[] | GetBytes() | GetOracleLob() |
CHAR | String
Char[] |
GetString()
GetChars() |
GetOracleString() |
CLOB | String
Char[] |
GetString()
GetChars() |
GetOracleLob() |
DATE | DateTime | GetDateTime() | GetOracleDateTime() |
FLOAT | Decimal | GetDecimal() | GetOracleNumber() ** |
INTEGER | Decimal | GetDecimal() | GetOracleNumber() ** |
INTERVAL YEAR TO MONTH * | Int32 | GetInt32() | GetOracleMonthSpan() |
INTERVAL DAY TO SECOND * | TimeSpan | GetTimeSpan() | GetOracleTimeSpan() |
LONG | String
Char[] |
GetString()
GetChars() |
GetOracleString() |
LONG RAW | Byte[] | GetBytes() | GetOracleBinary() |
NCHAR | String
Char[] |
GetString()
GetChars() |
GetOracleString() |
NCLOB | String
Char[] |
GetString()
GetChars() |
GetOracleLob() |
NUMBER | Decimal | GetDecimal() | GetOracleNumber() ** |
NVARCHAR2 | String
Char[] |
GetString()
GetChars() |
GetOracleString() |
RAW | Byte[] | GetBytes() | GetOracleBinary() |
REF CURSOR | |||
ROWID | String
Char[] |
GetString()
GetChars() |
GetOracleString() |
TIMESTAMP * | DateTime | GetDateTime() | GetOracleDateTime() |
TIMESTAMP WITH LOCAL TIME ZONE * | DateTime | GetDateTime() | GetOracleDateTime() |
TIMESTAMP WITH TIME ZONE * | DateTime | GetDateTime() | GetOracleDateTime() |
UNSIGNED INTEGER | Decimal | GetDecimal() | GetOracleNumber() ** |
VARCHAR2 | String
Char[] |
GetString()
GetChars() |
GetOracleString() |
* Il tipo Oracle specificato è disponibile solo quando si utilizzano i software Oracle 9i sia client che server.
** Un NUMBER Oracle può avere un massimo di 38 cifre significative. Il tipo decimal di .NET Framework è limitato a 28 cifre. Se si legge un NUMBER Oracle di più di 28 cifre in un tipo decimal .NET Framework si verificherà un OverflowException. Se si legge un valore NUMBER Oracle da un OracleDataReader, sarà consigliabile chiamare la funzione di accesso tipizzata GetOracleNumber così da restituire valori NUMBER Oracle come OracleNumber. Se si riempie un DataSet, sarà possibile utilizzare l'evento FillError per determinare se si è verificata una OverflowException e prendere le opportune misure. Per informazioni sull'evento FillError, vedere Utilizzo di eventi DataAdapter.