Follow Techotopia on Twitter

On-line Guides
All Guides
eBook Store
iOS / Android
Linux for Beginners
Office Productivity
Linux Installation
Linux Security
Linux Utilities
Linux Virtualization
Linux Kernel
System/Network Admin
Programming
Scripting Languages
Development Tools
Web Development
GUI Toolkits/Desktop
Databases
Mail Systems
openSolaris
Eclipse Documentation
Techotopia.com
Virtuatopia.com
Answertopia.com

How To Guides
Virtualization
General System Admin
Linux Security
Linux Filesystems
Web Servers
Graphics & Desktop
PC Hardware
Windows
Problem Solutions
Privacy Policy

  




 

 

26.2.3.5. The MySqlDataReader Class

The MySqlDataReader class provides a means of reading a forward-only stream of rows from a MySQL database.

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 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.

26.2.3.5.1. Properties

The following properties are available:

  • Depth: Gets a value indicating the depth of nesting for the current row. This method is not supported currently and always returns 0.

  • FieldCount: Gets the number of columns in the current row.

  • HasRows: Gets a value indicating whether the MySqlDataReader contains one or more rows.

  • IsClosed: Gets a value indicating whether the data reader is closed.

  • Item: Gets the value of a column in its native format. In C#, this property is the indexer for the MySqlDataReader class.

  • RecordsAffected: Gets the number of rows changed, inserted, or deleted by execution of the SQL statement.

26.2.3.5.2. Methods

The following methods are available:

  • Close: Closes the MySqlDataReader object.

  • GetBoolean: Gets the value of the specified column as a Boolean.

  • GetByte: Gets the value of the specified column as a byte.

  • GetBytes: Reads a stream of bytes from the specified column offset into the buffer an array starting at the given buffer offset.

  • GetChar: Gets the value of the specified column as a single character.

  • GetChars: Reads a stream of characters from the specified column offset into the buffer as an array starting at the given buffer offset.

  • GetDataTypeName: Gets the name of the source data type.

  • GetDateTime: Gets the value of the specified column as a DateTime object.

  • GetDecimal: Gets the value of the specified column as a Decimal object.

  • GetDouble: Gets the value of the specified column as a double-precision floating point number.

  • GetFieldType: Gets the Type that is the data type of the object.

  • GetFloat: Gets the value of the specified column as a single-precision floating point number.

  • GetGuid: Gets the value of the specified column as a GUID.

  • GetInt16: Gets the value of the specified column as a 16-bit signed integer.

  • GetInt32: Gets the value of the specified column as a 32-bit signed integer.

  • GetInt64: Gets the value of the specified column as a 64-bit signed integer.

  • GetMySqlDateTime: Gets the value of the specified column as a MySqlDateTime object.

  • GetName: Gets the name of the specified column.

  • GetOrdinal: Gets the column ordinal, given the name of the column.

  • GetSchemaTable: Returns a DataTable that describes the column metadata of the MySqlDataReader.

  • GetString: Gets the value of the specified column as a String object.

  • GetTimeSpan: Gets the value of the specified column as a TimeSpan object.

  • GetUInt16: Gets the value of the specified column as a 16-bit unsigned integer.

  • GetUInt32: Gets the value of the specified column as a 32-bit unsigned integer.

  • GetUInt64: Gets the value of the specified column as a 64-bit unsigned integer.

  • GetValue: Gets the value of the specified column in its native format.

  • GetValues: Gets all attribute columns in the collection for the current row.

  • IsDBNull: Gets a value indicating whether the column contains non-existent or missing values.

  • NextResult: Advances the data reader to the next result, when reading the results of batch SQL statements.

  • Read: Advances the MySqlDataReader to the next record.

26.2.3.5.3. Usage

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

26.2.3.5.3.1. VB.NET

The following example shows how to use the MySqlDataReader class with VB.NET:

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       
      
26.2.3.5.3.2. C#

The following example shows how to use the MySqlDataReader class with C#:

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();
 }     
      

 
 
  Published under the terms of the GNU General Public License Design by Interspire