The SqlDataReader Class

The SqlDataReader Class

You use an object of the SqlDataReader class to read rows retrieved from a SQL Server database, an object of the OleDbDataReader class to read rows from any database that supports OLE DB, such as Oracle or Access, and an object of the OdbcDataReader class to read rows from any data-base that supports ODBC. Table 9.1 shows some of the SqlDataReader properties.

Table 9.1: SqlDataReader PROPERTIES

PROPERTY

TYPE

DESCRIPTION

Depth

int

Gets a value indicating the depth of nesting for the current row.

FieldCount

int

Gets the number of columns in the current row.

IsClosed

bool

Gets a bool value indicating whether the data reader is closed.

RecordsAffected

int

Gets the number of rows added, modified, or removed by execution of the SQL statement.

Note 

Although the SqlDataReader class is specific to SQL Server, many of the properties and methods in this class are the same as those for the OleDbDataReader and OdbcDataReader classes.

Table 9.2 shows some of the public SqlDataReader methods.

Table 9.2: SqlDataReader METHODS

METHOD

RETURN TYPE

DESCRIPTION

GetBoolean()

bool

Returns the value of the specified column as a bool.

GetByte()

byte

Returns the value of the specified column as a byte.

GetBytes()

long

Reads a stream of byte values from the specified column into a byte array. The long value returned is the number of byte values read from the column.

GetChar()

char

Returns the value of the specified column as a char.

GetChars()

long

Reads a stream of char values from the specified column into a char array. The long value returned is the number of char values read from the column.

GetDataTypeName()

string

Returns the name of the source data type for the specified column.

GetDateTime()

DateTime

Returns the value of the specified column as a DateTime.

GetDecimal()

decimal

Returns the value of the specified column as a decimal.

GetDouble()

double

Returns the value of the specified column as a double.

GetFieldType()

Type

Returns the Type of the specified column.

GetFloat()

float

Returns the value of the specified column as a float.

GetGuid()

Guid

Returns the value of the specified column as a globally unique identifier (GUID).

GetInt16()

short

Returns the value of the specified column as a short.

GetInt32()

int

Returns the value of the specified column as an int.

GetInt64()

long

Returns the value of the specified column as a long.

GetName()

string

Returns the name of the specified column.

GetOrdinal()

int

Returns the numeric position, or ordinal, of the specified column (first column has an ordinal of 0).

GetSchemaTable()

DataTable

Returns a DataTable that contains details of the columns stored in the data reader.

GetSqlBinary()

SqlBinary

Returns the value of the specified column as a SqlBinary object. The SqlBinary class is declared in the System.Data.SqlTypes namespace.

All the GetSql* methods are specific to the SqlDataReader class.

GetSqlBoolean()

SqlBoolean

Returns the value of the specified column as a SqlBoolean object.

GetSqlByte()

SqlByte

Returns the value of the specified column as a SqlByte object.

GetSqlDateTime()

SqlDateTime

Returns the value of the specified column as a SqlDateTime object.

GetSqlDecimal()

SqlDecimal

Returns the value of the specified column as a SqlDecimal object.

GetSqlDouble()

SqlDouble

Returns the value of the specified column as a SqlDouble object.

GetSqlGuid()

SqlGuid

Returns the value of the specified column as a SqlGuid object.

GetSqlInt16()

SqlInt16

Returns the value of the specified column as a SqlInt16 object.

GetSqlInt32()

SqlInt32

Returns the value of the specified column as a SqlInt32 object.

GetSqlInt64()

SqlInt64

Returns the value of the specified column as a SqlInt64 object.

GetSqlMoney()

SqlMoney

Returns the value of the specified column as a SqlMoney object.

GetSqlSingle()

SqlSingle

Returns the value of the specified column as a SqlSingle object.

GetSqlString()

SqlString

Returns the value of the specified column as a SqlString object.

GetSqlValue()

object

Returns the value of the specified column as an object.

GetSqlValues()

int

Copies the value of all the columns in the current row into a specified object array. The int returned by this method is the number of elements in the array.

GetString()

string

Returns the value of the specified column as a string.

GetValue()

object

Returns the value of the specified column as an object.

GetValues()

int

Copies the value of all the columns in the current row into a specified object array. The int returned by this method is the number of elements in the array.

IsDBNull()

bool

Returns a bool that indicates whether the specified column contains a null value.

NextResult()

bool

Moves the data reader to the next row in the result set. The bool returned by this method indicates whether there are more rows in the result set.

Read()

bool

Moves the data reader to the next row in the result set and reads the row. The bool returned by this method indicates whether there are more rows in the result set.

Note 

The DataTable column details include the name (stored in the ColumnName column of the returned DataTable), ordinal (stored in ColumnOrdinal), maximum length of the value that may be stored in the column (stored in ColumnSize), precision and scale of a numeric column (stored in NumericPrecision and NumericScale), among others. Precision is the total number of digits that make up a number, and scale is the total number of digits to the right of the decimal point. You saw how to read a schema using a program in the previous chapter.

Tip 

The System.Data.SqlTypes namespace provides classes for native data types used within SQL Server. These classes provide a safer and faster alternative to other data types returned by the other Get* methods. Using the classes in this namespace helps prevent type conversion errors caused by loss of precision. Because other data types are converted to and from SqlTypes behind the scenes, explicitly creating and using objects within this namespace results in faster code as well. You'll learn more about the SqlTypes namespace later in the "Using the GetSql* Methods to Read Column Values" section.




Mastering C# Database Programming
Mastering the SAP Business Information Warehouse: Leveraging the Business Intelligence Capabilities of SAP NetWeaver
ISBN: 0764596373
EAN: 2147483647
Year: 2003
Pages: 181

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net