Click or drag to resize
MySqlDataReader Class
Provides a means of reading a forward-only stream of rows from a MySQL database. This class cannot be inherited.
Inheritance Hierarchy
SystemObject
  SystemMarshalByRefObject
    System.Data.CommonDbDataReader
      MySql.Data.MySqlClientMySqlDataReader

Namespace:  MySql.Data.MySqlClient
Assembly:  MySql.Data (in MySql.Data.dll) Version: 8.0.21
Syntax
public sealed class MySqlDataReader : DbDataReader, 
	IDataReader, IDisposable, IDataRecord

The MySqlDataReader type exposes the following members.

Properties
  NameDescription
Public propertyDepth
Gets a value indicating the depth of nesting for the current row. This method is not supported currently and always returns 0.
(Overrides DbDataReaderDepth.)
Public propertyFieldCount
Gets the number of columns in the current row.
(Overrides DbDataReaderFieldCount.)
Public propertyHasRows
Gets a value indicating whether the MySqlDataReader contains one or more rows.
(Overrides DbDataReaderHasRows.)
Public propertyIsClosed
Gets a value indicating whether the data reader is closed.
(Overrides DbDataReaderIsClosed.)
Public propertyItemInt32
Overloaded. Gets the value of a column in its native format. In C#, this property is the indexer for the MySqlDataReader class.
(Overrides DbDataReaderItemInt32.)
Public propertyItemString
Gets the value of a column in its native format. [C#] In C#, this property is the indexer for the MySqlDataReader class.
(Overrides DbDataReaderItemString.)
Public propertyRecordsAffected
Gets the number of rows changed, inserted, or deleted by execution of the SQL statement.
(Overrides DbDataReaderRecordsAffected.)
Public propertyVisibleFieldCount
Gets the number of fields in the DbDataReader that are not hidden.
(Inherited from DbDataReader.)
Top
Methods
  NameDescription
Public methodClose
Closes the MySqlDataReader object.
(Overrides DbDataReaderClose.)
Public methodCreateObjRef
Creates an object that contains all the relevant information required to generate a proxy used to communicate with a remote object.
(Inherited from MarshalByRefObject.)
Public methodDispose
Releases all resources used by the MySqlDataReader
Protected methodDispose(Boolean)
Releases the managed resources used by the DbDataReader and optionally releases the unmanaged resources.
(Inherited from DbDataReader.)
Public methodEquals
Determines whether the specified object is equal to the current object.
(Inherited from Object.)
Protected methodFinalize (Overrides ObjectFinalize.)
Public methodGetBodyDefinition
Public methodGetBoolean(Int32)
Gets the value of the specified column as a Boolean.
(Overrides DbDataReaderGetBoolean(Int32).)
Public methodGetBoolean(String)
Gets the value of the specified column as a Boolean.
Public methodGetByte(Int32)
Gets the value of the specified column as a byte.
(Overrides DbDataReaderGetByte(Int32).)
Public methodGetByte(String)
Gets the value of the specified column as a byte.
Public methodGetBytes
Reads a stream of bytes from the specified column offset into the buffer an array starting at the given buffer offset.
(Overrides DbDataReaderGetBytes(Int32, Int64, Byte, Int32, Int32).)
Public methodGetChar(Int32)
Gets the value of the specified column as a single character.
(Overrides DbDataReaderGetChar(Int32).)
Public methodGetChar(String)
Gets the value of the specified column as a single character.
Public methodGetChars
Reads a stream of characters from the specified column offset into the buffer as an array starting at the given buffer offset.
(Overrides DbDataReaderGetChars(Int32, Int64, Char, Int32, Int32).)
Public methodGetData
Returns a DbDataReader object for the requested column ordinal.
(Inherited from DbDataReader.)
Public methodGetDataTypeName
Gets the name of the source data type.
(Overrides DbDataReaderGetDataTypeName(Int32).)
Public methodGetDateTime(Int32)
Gets the value of the specified column as a DateTime object.
(Overrides DbDataReaderGetDateTime(Int32).)
Public methodGetDateTime(String)
Gets the value of the specified column as a DateTime object.
Protected methodGetDbDataReader
Returns a DbDataReader object for the requested column ordinal that can be overridden with a provider-specific implementation.
(Inherited from DbDataReader.)
Public methodGetDecimal(Int32)
Gets the value of the specified column as a Decimal object.
(Overrides DbDataReaderGetDecimal(Int32).)
Public methodGetDecimal(String)
Gets the value of the specified column as a Decimal object.
Public methodGetDouble(Int32)
Gets the value of the specified column as a double-precision floating point number.
(Overrides DbDataReaderGetDouble(Int32).)
Public methodGetDouble(String)
Gets the value of the specified column as a double-precision floating point number.
Public methodGetEnumerator
Returns an IEnumerator that iterates through the MySqlDataReader.
(Overrides DbDataReaderGetEnumerator.)
Public methodGetFieldType(Int32)
Gets the Type that is the data type of the object.
(Overrides DbDataReaderGetFieldType(Int32).)
Public methodGetFieldType(String)
Public methodGetFieldValueT (Overrides DbDataReaderGetFieldValueT(Int32).)
Public methodGetFieldValueAsyncT(Int32)
Asynchronously gets the value of the specified column as a type.
(Inherited from DbDataReader.)
Public methodGetFieldValueAsyncT(Int32, CancellationToken)
Asynchronously gets the value of the specified column as a type.
(Inherited from DbDataReader.)
Public methodGetFloat(Int32)
Gets the value of the specified column as a single-precision floating point number.
(Overrides DbDataReaderGetFloat(Int32).)
Public methodGetFloat(String)
Gets the value of the specified column as a single-precision floating point number.
Public methodGetGuid(Int32) (Overrides DbDataReaderGetGuid(Int32).)
Public methodGetGuid(String)
Gets the value of the specified column as a globally-unique identifier(GUID).
Public methodGetHashCode
Serves as the default hash function.
(Inherited from Object.)
Public methodGetInt16(Int32)
Gets the value of the specified column as a 16-bit signed integer.
(Overrides DbDataReaderGetInt16(Int32).)
Public methodGetInt16(String)
Gets the value of the specified column as a 16-bit signed integer.
Public methodGetInt32(Int32)
Gets the value of the specified column as a 32-bit signed integer.
(Overrides DbDataReaderGetInt32(Int32).)
Public methodGetInt32(String)
Gets the value of the specified column as a 32-bit signed integer.
Public methodGetInt64(Int32)
Gets the value of the specified column as a 64-bit signed integer.
(Overrides DbDataReaderGetInt64(Int32).)
Public methodGetInt64(String)
Gets the value of the specified column as a 64-bit signed integer.
Public methodGetLifetimeService
Retrieves the current lifetime service object that controls the lifetime policy for this instance.
(Inherited from MarshalByRefObject.)
Public methodGetMySqlDateTime(Int32)
Gets the value of the specified column as a MySqlDateTime object.
Public methodGetMySqlDateTime(String)
Gets the value of the specified column as a MySqlDateTime object.
Public methodGetMySqlDecimal(Int32)
Gets the value of the specified column as a MySqlDecimal.
Public methodGetMySqlDecimal(String)
Gets the value of the specified column as a MySqlDecimal.
Public methodGetMySqlGeometry(Int32)
Gets the value of the specified column as a MySqlGeometry.
Public methodGetMySqlGeometry(String)
Gets the value of the specified column as a MySqlGeometry.
Public methodGetName
Gets the name of the specified column.
(Overrides DbDataReaderGetName(Int32).)
Public methodGetOrdinal
Gets the column ordinal, given the name of the column.
(Overrides DbDataReaderGetOrdinal(String).)
Public methodGetProviderSpecificFieldType
Returns the provider-specific field type of the specified column.
(Inherited from DbDataReader.)
Public methodGetProviderSpecificValue
Gets the value of the specified column as an instance of Object.
(Inherited from DbDataReader.)
Public methodGetProviderSpecificValues
Gets all provider-specific attribute columns in the collection for the current row.
(Inherited from DbDataReader.)
Public methodGetSByte(Int32)
Gets the value of the specified column as a sbyte.
Public methodGetSByte(String)
Gets the value of the specified column as a sbyte.
Public methodGetSchemaTable
Returns a DataTable that describes the column metadata of the MySqlDataReader.
(Overrides DbDataReaderGetSchemaTable.)
Public methodGetStream
Retrieves data as a Stream.
(Inherited from DbDataReader.)
Public methodGetString(Int32)
Gets the value of the specified column as a String object.
(Overrides DbDataReaderGetString(Int32).)
Public methodGetString(String)
Gets the value of the specified column as a String object.
Public methodGetTextReader
Retrieves data as a TextReader.
(Inherited from DbDataReader.)
Public methodGetTimeSpan(Int32)
Gets the value of the specified column as a TimeSpan object.
Public methodGetTimeSpan(String)
Gets the value of the specified column as a TimeSpan object.
Public methodGetType
Gets the Type of the current instance.
(Inherited from Object.)
Public methodGetUInt16(Int32)
Gets the value of the specified column as a 16-bit unsigned integer.
Public methodGetUInt16(String)
Gets the value of the specified column as a 16-bit unsigned integer.
Public methodGetUInt32(Int32)
Gets the value of the specified column as a 32-bit unsigned integer.
Public methodGetUInt32(String)
Gets the value of the specified column as a 32-bit unsigned integer.
Public methodGetUInt64(Int32)
Gets the value of the specified column as a 64-bit unsigned integer.
Public methodGetUInt64(String)
Gets the value of the specified column as a 64-bit unsigned integer.
Public methodGetValue
Gets the value of the specified column in its native format.
(Overrides DbDataReaderGetValue(Int32).)
Public methodGetValues
Gets all attribute columns in the collection for the current row.
(Overrides DbDataReaderGetValues(Object).)
Public methodInitializeLifetimeService
Obtains a lifetime service object to control the lifetime policy for this instance.
(Inherited from MarshalByRefObject.)
Public methodIsDBNull
Gets a value indicating whether the column contains non-existent or missing values.
(Overrides DbDataReaderIsDBNull(Int32).)
Public methodIsDBNullAsync(Int32)
An asynchronous version of IsDBNull(Int32), which gets a value that indicates whether the column contains non-existent or missing values.
(Inherited from DbDataReader.)
Public methodIsDBNullAsync(Int32, CancellationToken)
An asynchronous version of IsDBNull(Int32), which gets a value that indicates whether the column contains non-existent or missing values. Optionally, sends a notification that operations should be cancelled.
(Inherited from DbDataReader.)
Protected methodMemberwiseClone
Creates a shallow copy of the current Object.
(Inherited from Object.)
Protected methodMemberwiseClone(Boolean)
Creates a shallow copy of the current MarshalByRefObject object.
(Inherited from MarshalByRefObject.)
Public methodNextResult
Advances the data reader to the next result, when reading the results of batch SQL statements.
(Overrides DbDataReaderNextResult.)
Public methodNextResultAsync
An asynchronous version of NextResult, which advances the reader to the next result when reading the results of a batch of statements.Invokes NextResultAsync(CancellationToken) with CancellationToken.None.
(Inherited from DbDataReader.)
Public methodNextResultAsync(CancellationToken)
This is the asynchronous version of NextResult. Providers should override with an appropriate implementation. The cancellationToken may optionally be ignored.The default implementation invokes the synchronous NextResult method and returns a completed task, blocking the calling thread. The default implementation will return a cancelled task if passed an already cancelled cancellationToken. Exceptions thrown by NextResult will be communicated via the returned Task Exception property.Other methods and properties of the DbDataReader object should not be invoked while the returned Task is not yet completed.
(Inherited from DbDataReader.)
Public methodRead
Advances the MySqlDataReader to the next record.
(Overrides DbDataReaderRead.)
Public methodReadAsync
An asynchronous version of Read, which advances the reader to the next record in a result set. This method invokes ReadAsync(CancellationToken) with CancellationToken.None.
(Inherited from DbDataReader.)
Public methodReadAsync(CancellationToken)
This is the asynchronous version of Read. Providers should override with an appropriate implementation. The cancellationToken may optionally be ignored.The default implementation invokes the synchronous Read method and returns a completed task, blocking the calling thread. The default implementation will return a cancelled task if passed an already cancelled cancellationToken. Exceptions thrown by Read will be communicated via the returned Task Exception property.Do not invoke other methods and properties of the DbDataReader object until the returned Task is complete.
(Inherited from DbDataReader.)
Public methodToString
Returns a string that represents the current object.
(Inherited from Object.)
Top
Remarks

To create a MySQLDataReader, you must call the ExecuteReader method of the MySqlCommand object, rather than directly using a constructor.

While the MySqlDataReader is in use, the associated MySqlConnection is busy serving the MySqlDataReader, and no other operations can be performed on the MySqlConnection other than closing it. This is the case until the Close method of the MySqlDataReader is called.

IsClosed and RecordsAffected are the only properties that you can call after the MySqlDataReader is closed. Though the RecordsAffected property may be accessed at any time while the MySqlDataReader exists, always call Close before returning the value of RecordsAffected to ensure an accurate return value.

For optimal performance, MySqlDataReader avoids creating unnecessary objects or making unnecessary copies of data. As a result, multiple calls to methods such as GetValue(Int32) return a reference to the same object. Use caution if you are modifying the underlying value of the objects returned by methods such as GetValue.

Examples
The following example creates a MySqlConnection, a MySqlCommand, and a MySqlDataReader. The example reads through the data, writing it out to the console. Finally, the example closes the MySqlDataReader, then the MySqlConnection.
public void ReadMyData(string myConnString) {
string mySelectQuery = "SELECT OrderID, CustomerID FROM Orders";
MySqlConnection myConnection = new MySqlConnection(myConnString);
MySqlCommand myCommand = new MySqlCommand(mySelectQuery,myConnection);
myConnection.Open();
MySqlDataReader myReader;
myReader = myCommand.ExecuteReader();
// Always call Read before accessing data.
while (myReader.Read()) {
Console.WriteLine(myReader.GetInt32(0) + ", " + myReader.GetString(1));
}
// always call Close when done reading.
myReader.Close();
// Close the connection when done with it.
myConnection.Close();
}
See Also