dotConnect for MySQL Documentation
Devart.Data.MySql Namespace / MySqlDataReader Class
Members Example

On this page
    MySqlDataReader Class
    On this page
    Reads a forward-only stream of rows from MySQL.
    Syntax
    Remarks
    To create a MySqlDataReader, you must call the ExecuteReader method of the MySqlCommand object, rather than directly using a constructor.

    System.Data.Common.DbDataReader.IsClosed and RecordsAffected are the only properties that you can call after the MySqlDataReader is closed. In some cases, you must call Close before you can call RecordsAffected.

    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. For example, you cannot retrieve output parameters until you call Close.

    Example
    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)  {
            MySqlConnection mySqlConnection = new MySqlConnection(myConnString);
            MySqlCommand mySqlCommand = (MySqlCommand)mySqlConnection.CreateCommand();
            mySqlCommand.CommandText = "SELECT DeptNo, DName, Loc FROM Test.Dept";
            mySqlConnection.Open();
            MySqlDataReader mySqlReader = mySqlCommand.ExecuteReader();
            try {
                    // Always call Read before accessing data.
                    while (mySqlReader.Read()) {
                    Console.WriteLine(mySqlReader.GetInt32(0).ToString() + " " + 
                    mySqlReader.GetString(1) + " " + mySqlReader.GetString(2));
            }
            }
            finally {
                    // always call Close when done reading.
                    mySqlReader.Close();
    
                    // Close the connection when done with it.
                    mySqlConnection.Close();
            }
    }
    Public Sub ReadMyData(ByVal myConnString As String)
            Dim mySqlConnection As New MySqlConnection(myConnString)
            Dim mySqlCommand As MySqlCommand = mySqlConnection.CreateCommand()
                    mySqlCommand.CommandText = "SELECT DeptNo, DName, Loc FROM Test.Dept"
            mySqlConnection.Open()
            Dim mySqlReader As MySqlDataReader = mySqlCommand.ExecuteReader()
            Try
                    ' Always call Read before accessing data.
                    While mySqlReader.Read()
                            Console.WriteLine(String.Concat(mySqlReader.GetInt32(0).ToString(), " ", _
                            mySqlReader.GetString(1), " ", mySqlReader.GetString(2)))
                    End While
            Finally
                    ' always call Close when done reading.
                    mySqlReader.Close()
    
                            ' Close the connection when done with it.
                            mySqlConnection.Close()
            End Try
    End Sub
    Inheritance Hierarchy

    System.Object
       System.MarshalByRefObject
          System.Data.Common.DbDataReader
             Devart.Common.DbDataReaderBase
                Devart.Data.MySql.MySqlDataReader

    See Also