Database Reference
In-Depth Information
// ExpMonth
txtTypeAccess.AppendText(rdr.GetByte(2).ToString());
txtTypeAccess.AppendText("\t\t");
// ExpYear
txtTypeAccess.AppendText(rdr.GetInt16(3).ToString());
txtTypeAccess.AppendText("\n");
}
Looking at Table 14-1, you can see that you can access nvarchar, tinyint, and smallint data types in
SQL Server with the GetString , GetByte , and Getlntl6 accessor methods, respectively.
This technique is fast and completely type safe. By this, we mean that if implicit conversions from
native data types to .NET types fail, an exception is thrown for invalid casts. For instance, if you try using
the GetString method on a bit data type instead of using the GetBoolean method, a “Specified cast is not
valid” exception will be thrown.
Getting Data About Data
So far, all you've done is retrieve data from a data source. Once you have a populated data reader in your
hands, you can do a lot more. There are a number of useful methods for retrieving schema information
or retrieving information directly related to a result set. Table 14-4 describes some of the metadata
methods and properties of a data reader.
Table 14-4. Data Reader Metadata Properties and Methods
Method or Property Name
Description
Depth
A property that gets the depth of nesting for the current row
FieldCount
A property that holds the number of columns in the current row
GetDataTypeName
A method that accepts an index and returns a string containing the name of
the column data type
GetFieldType
A method that accepts an index and returns the .NET Framework type of the
object
GetName
A method that accepts an index and returns the name of the specified
column
GetOrdinal
A method that accepts a column name and returns the column index
GetSchemaTable
A method that returns column metadata
HasRows
A property that indicates whether the data reader has any rows
RecordsAffected
A property that gets the number of rows changed, inserted, or deleted
 
Search WWH ::




Custom Search