DekGenius.com
Team LiB
Previous Section Next Section

The DataReader Object

The DataReader object is used to read data that has been retrieved from a database. While the Command and DataAdapter objects are used to issue database commands, the DataReader object is used to read retrieved data values. Once the values are inside this object, several other tasks can be achieved, such as:

The DataReader object should be the main interface between client objects and MTOs (Middle Tier Objects) because this object is equipped with some highly optimized functionality for data reading. The following section is a complete reference to the members of the DataReader object.

DataReader Properties

Depth

Type: Integer

Attribute: Read-only

Default: 0

Description: This property obtains a valid count of the number of nesting rows for the current row.

FieldCount

Type: Integer

Attribute: Read-only

Default: –1

Description: This property obtains the number of columns present in the current row that is being read.

IsClosed

Type: Boolean

Attribute: Read-only

Default: Null

Description: This property indicates whether the DataReader is closed or opened. It is used mostly to test the availability of data inside the object before issuing instructions to read the data.

Item(<column_name as string> or <column_ordinal as integer>)

Type: The native type of the item

Attribute: Read-only

Default: Null

Description: This property returns the value of the column specified by column_name or column_ordinal of the current row.

RecordsAffected

Type: Integer

Attribute: Read-only

Default: 0

Description: This property indicates how many records were affected by the execution of an SQL statement or changed on the client. The property returns 0 if there were no affected records or the SQL statement failed. –1 is returned for all SELECT statements.

DataReader Methods

Close()

Returns

Void

Parameters

None

Description

This method closes the DataReader object and frees up client resources.

CreateObjRef()

Returns

System.Runtime.Remoting.ObjRef: The returned object contains all the required information to generate a proxy that can communicate with another object residing remotely.

Parameters

System.Type: A valid type for the object to be created

Description

Call this method when you need to reference a valid object that contains all the required information to generate a proxy for the DataReader that will allow it to communicate with another object residing remotely. This method is used if you want the DataReader to communicate with remote objects.

Equals()

Returns

Boolean

Parameters

obj: System.Object; any object inherited from the System.Object class

Description

Determines whether obj is equal to the DataReader object

Usage

This method will always return False if you pass an object that is not of the same type as the DataReader object. It is useful when you need to test whether two DataReader objects are holding exactly the same values from the same source.

GetBoolean()

Returns

Boolean: The value of the specified column at the current row as a Boolean value

Parameters

i: Integer; an index of the column. The DataReader contains a zero-based column array.

Description

If the column’s type is not Boolean, the method does not attempt a conversion and throws an InvalidCastException exception. Be careful to test for a null value using the IsDBNull() method before attempting to read using this method.

GetByte()

Returns

Byte: The value of the specified column at the current row as a byte value

Parameters

i: Integer; an index of the column. The DataReader contains a zero-based column array.

Description

If the column’s type is not byte, the method does not attempt a conversion and throws an InvalidCastException exception. Be careful to test for a null value using the IsDBNull() method before attempting to read using this method.

GetBytes()

Returns

Integer: The count of Byte values read into the buffer parameter

Parameters

i: Integer; an index of the column. The DataReader contains a zero-based column array.

dataIndex: Long; the index within the column from which the method will start reading Byte values into the buffer

buffer(): Byte; an array of type Byte that will hold the stream of byte values to be read

bufferIndex: Integer; the ordinal inside the buffer into which the first Byte value will be read

length: Integer; the maximum number of byte values to copy into the buffer

Description

This method is used to obtain a stream of byte values from a column in the DataReader.

GetChar()

Returns

Char: The value of the specified column at the current row as a Char (single character) value.

Parameters

i: Integer; an index of the column. The DataReader contains a zero-based column array.

Description

If the column’s type is not Char, the method does not attempt a conversion and throws an InvalidCastException exception. Be careful to test for a null value using the IsDBNull() method before attempting to read using this method.

GetChars()

Returns

Integer: The count of Char values read into the buffer parameter

Parameters

i: Integer; an index of the column. The DataReader contains a zero-based column array.

dataIndex: Long; the index within the column from which the method will start reading Byte values into the buffer

buffer(): Char; an array of type Byte that will hold the stream of Byte values to be read

bufferIndex: Integer; the ordinal inside the buffer into which the first Byte value will be read

length: Integer; the maximum number of Byte values to copy into the buffer

Description

This method is used to obtain a stream of Char values from a column in the DataReader.

GetDataTypeName()

Returns

String: The name of the type of column specified by column ordinal parameter i

Parameters

i: Integer; an index of the column. The DataReader contains a zero-based column array.

Description

This method obtains the name of the type of column specified by the i parameter. It throws an IndexOutOfRangeException exception if the value specified by i was less than zero or greater than the FieldCount property of the DataReader object.

GetDateTime()

Returns

System.DateTime: The value of the specified column at the current row as a DateTime object

Parameters

i: Integer; an index of the column. The DataReader contains a zero-based column array.

Description

If the column’s type is not DateTime, the method does not attempt a conversion and throws an InvalidCastException exception. Be careful to test for a null value using the IsDBNull() method before attempting to read using this method.

If the i parameter is greater than the FieldCount property or less than zero, the method throws an IndexOutOfRangeException exception.

GetDecimal()

Returns

System.Decimal: The value of the specified column at the current row as a Decimal object

Parameters

i: Integer; an index of the column. The DataReader contains a zero-based column array.

Description

If the column’s type is not Decimal, the method does not attempt a conversion and throws an InvalidCastException exception. Be careful to test for a null value using the IsDBNull() method before attempting to read using this method.

If the i parameter is greater than the FieldCount property or less than zero, the method throws an IndexOutOfRangeException exception.

GetDouble()

Returns

System.Double: The value of the specified column at the current row as a System.Double object

Parameters

i: Integer; an index of the column. The DataReader contains a zero-based column array.

Description

If the column’s type is not Double, the method does not attempt a conversion and throws an InvalidCastException exception. Be careful to test for a null value using the IsDBNull() method before attempting to read using this method.

If the i parameter is greater than the FieldCount property or less than zero, the method throws an IndexOutOfRangeException exception.

GetFieldType()

Returns

System.Type: The valid type of the specified column as a System.Type object

Parameters

i: Integer; an index of the column. The DataReader contains a zero-based column array.

Description

This method is used to obtain a valid System.Type value that matches the data type of the field in the database.

GetFloat()

Returns

System.Single: The value of the specified column at the current row as a System.Single object

Parameters

i: Integer; an index of the column. The DataReader contains a zero-based column array.

Description

If the column’s type is not Single, the method does not attempt a conversion and throws an InvalidCastException exception. Be careful to test for a null value using the IsDBNull() method before attempting to read using this method.

If the i parameter is greater than the FieldCount property or less than zero, the method throws an IndexOutOfRangeException exception.

GetGuid()

Returns

System.Guid: The value of the specified column at the current row as a System.Guid object representing a globally unique identifier value

Parameters

i: Integer; an index of the column. The DataReader contains a zero-based column array.

Description

If the column’s type is not Guid, the method does not attempt a conversion and throws an InvalidCastException exception. Be careful to test for a null value using the IsDBNull() method before attempting to read using this method.

If the i parameter is greater than the FieldCount property or less than zero, the method throws an IndexOutOfRangeException exception.

GetHashCode()

Returns

Integer: The integer value indicating the hash code of the current row

Parameters

None

Description

Use this method to obtain the unique hash code for the DataReader.

GetInt16()

Returns

System.Int16: The value of the specified column at the current row as a System.Int16 object (a 16-bit signed integer value)

Parameters

i: Integer; an index of the column. The DataReader contains a zero-based column array.

Description

If the column’s type is not Int16, the method does not attempt a conversion and throws an InvalidCastException exception. Be careful to test for a null value using the IsDBNull() method before attempting to read using this method.

If the i parameter is greater than the FieldCount property or less than zero, the method throws an IndexOutOfRangeException exception.

GetInt32()

Returns

System.Int32: The value of the specified column at the current row as a System.Int32 object (a 32-bit signed integer value)

Parameters

i: Integer; an index of the column. The DataReader contains a zero-based column array.

Description

If the column’s type is not Int32, the method does not attempt a conversion and throws an InvalidCastException exception. Be careful to test for a null value using the IsDBNull() method before attempting to read using this method.

If the i parameter is greater than the FieldCount property or less than zero, the method throws an IndexOutOfRangeException exception.

GetInt64()

Returns

SystemInt64: The value of the specified column at the current row as a System.Int64 object (a 64-bit signed integer value)

Parameters

i: Integer; an index of the column. The DataReader contains a zero-based column array.

Description

If the column’s type is not Int64, the method does not attempt a conversion and throws an InvalidCastException exception. Be careful to test for a null value using the IsDBNull() method before attempting to read using this method.

If the i parameter is greater than the FieldCount property or less than zero, the method throws an IndexOutOfRangeException exception.

GetName()

Returns

String: The value of the name of the specified column at the current row as a String object

Parameters

i: Integer; an index of the column. The DataReader contains a zero-based column array.

Description

The GetName() method returns the name of the DataSet component and is useful when you want to refer to a DataSet by its name rather than using a reference variable.

GetOrdinal()

Returns

Integer: The ordinal of the column as an Integer value

Parameters

Name: String; the name of the column

Description

If the name specified is not a valid column name, the method throws an IndexOutOfRangeException exception.

GetSchemaTable()

Returns

System.Data.DataTable: A DataTable object containing the schema information for the columns of the DataReader

Parameters

None

Description

If the DataReader is closed, the method throws an InvalidOperationException exception.

GetString()

Returns

String: The value of the specified column at the current row as a String object

Parameters

i: Integer; an index of the column. The DataReader contains a zero-based column array.

Description

If the column’s type is not Int64, the method does not attempt a conversion and throws an InvalidCastException exception. Be careful to test for a null value using the IsDBNull() method before attempting to read using this method.

If the i parameter is greater than the FieldCount property or less than zero, the method throws an IndexOutOfRangeException exception.

GetTimeSpan()

Returns

System.TimeSpan: The value of the specified column at the current row as a TimeSpan object

Parameters

i: Integer; an index of the column. The DataReader contains a zero-based column array.

Description

If the column’s type is not TimeSpan, the method does not attempt a conversion and throws an InvalidCastException exception. Be careful to test for a null value using the IsDBNull() method before attempting to read using this method.

If the i parameter is greater than the FieldCount property or less than zero, the method throws an IndexOutOfRangeException exception.

GetValue()

Returns

System.Object: The value of the specified column at the current row as a System.Object object

Parameters

i: Integer; an index of the column. The DataReader contains a zero-based column array.

Description

If the i parameter is greater than the FieldCount property or less than zero, the method throws an IndexOutOfRangeException exception.

GetValues()

Returns

Integer: The count of Char values read into the buffer parameter

Parameters

values(): Object; an array of type Object that will hold the stream of Object values to be read

Description

This method is used to obtain a stream of Object values from a column in the DataReader. Be careful to test for a null value using the IsDBNull() method before attempting to read using this method.

IsDBNull()

Returns

Boolean: A Boolean value indicating whether the value at the specified column in the current row is equal to a System.DBNull object. It’s True if the value is null and False otherwise.

Parameters

i: Integer; an index of the column. The DataReader contains a zero-based column array.

Description

Use this method to check whether the value that you are about to read is null or not.

NextResult()

Returns

Boolean: A Boolean value indicating whether there are any result sets from the current position.

Parameters

None

Description

Use this method when dealing with a batch of SQL statements that returned multiple sets of results. If this method returns True, there is a result set, and the method moves the DataReader object to its position.

Read()

Returns

Boolean: A Boolean value indicating whether there are any rows in the DataReader. If this value is True, the DataReader has more rows and the position is shifted to the next row.

Parameters

None

Description

Use this method to test whether there are any rows inside the DataReader object, and move to the next row. The default location of the DataReader is prior to the first row, so you will have to call this method before you attempt any read operation on the DataReader.

Note 

.NET data providers such as the SQL Server .NET Data Provider provide several optimized methods that are equipped with functionality to read data of the types specific to that particular provider.

Team LiB
Previous Section Next Section