MySqlDataReader Class

MySQL Connector/Net

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: 6.9.9
Syntax
public sealed class MySqlDataReader : DbDataReader, 
	IDisposable, IDataReader, IDataRecord
Public NotInheritable Class MySqlDataReader
	Inherits DbDataReader
	Implements IDisposable, IDataReader, IDataRecord
public ref class MySqlDataReader sealed : public DbDataReader, 
	IDisposable, IDataReader, IDataRecord
[<SealedAttribute>]
type MySqlDataReader =  
    class
        inherit DbDataReader
        interface IDisposable
        interface IDataReader
        interface IDataRecord
    end

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 (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
Protected methodDispose(Boolean) (Inherited from DbDataReader.)
Public methodEquals
Determines whether the specified object is equal to the current object.
(Inherited from Object.)
Protected methodFinalize (Overrides ObjectFinalize.)
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 (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 (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 (Inherited from DbDataReader.)
Public methodGetFieldValueAsyncT(Int32) (Inherited from DbDataReader.)
Public methodGetFieldValueAsyncT(Int32, CancellationToken) (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)
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)
Public methodGetMySqlDecimal(String)
Public methodGetMySqlGeometry(Int32)
Public methodGetMySqlGeometry(String)
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 (Inherited from DbDataReader.)
Public methodGetProviderSpecificValue (Inherited from DbDataReader.)
Public methodGetProviderSpecificValues (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 (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 (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) (Inherited from DbDataReader.)
Public methodIsDBNullAsync(Int32, CancellationToken) (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 (Inherited from DbDataReader.)
Public methodNextResultAsync(CancellationToken) (Inherited from DbDataReader.)
Public methodRead
Advances the MySqlDataReader to the next record.
(Overrides DbDataReaderRead.)
Public methodReadAsync (Inherited from DbDataReader.)
Public methodReadAsync(CancellationToken) (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();
}
Public Sub ReadMyData(myConnString As String)
Dim mySelectQuery As String = "SELECT OrderID, CustomerID FROM Orders"
Dim myConnection As New MySqlConnection(myConnString)
Dim myCommand As New MySqlCommand(mySelectQuery, myConnection)
myConnection.Open()
Dim myReader As MySqlDataReader
myReader = myCommand.ExecuteReader()
' Always call Read before accessing data.
While myReader.Read()
Console.WriteLine((myReader.GetInt32(0) & ", " & myReader.GetString(1)))
End While
' always call Close when done reading.
myReader.Close()
' Close the connection when done with it.
myConnection.Close()
End Sub 'ReadMyData

No code example is currently available or this language may not be supported.

No code example is currently available or this language may not be supported.

See Also