dotConnect for PostgreSQL Documentation
Devart.Data.PostgreSql Namespace / PgSqlDataReader Class
Members Example

In This Topic
    PgSqlDataReader Class
    In This Topic
    Reads a forward-only stream of rows from PostgreSQL.
    Syntax
    Remarks
    To create a PgSqlDataReader, you must call the ExecuteReader method of the PgSqlCommand object, rather than directly using a constructor.

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

    When protocol is ProtocolVersion.Ver20, the following restriction takes place. While the PgSqlDataReader is in use, the associated PgSqlConnection is busy serving the PgSqlDataReader, and no other operations can be performed on the PgSqlConnection other than closing it. This is the case until the Close method of the PgSqlDataReader is called. For example, you cannot retrieve output parameters until you call Close.

    Example
    The following example creates a PgSqlConnection, a PgSqlCommand, and a PgSqlDataReader. The example reads through the data, writing it out to the console. Finally, the example closes the PgSqlDataReader, then the PgSqlConnection.
    public void ReadMyData(string myConnString)  {
            PgSqlConnection pgConnection = new PgSqlConnection(myConnString);
            PgSqlCommand pgCommand = (PgSqlCommand)pgConnection.CreateCommand();
            pgCommand.CommandText = "SELECT DeptNo, DName, Loc FROM Test.Dept";
            pgConnection.Open();
            PgSqlDataReader pgReader = pgCommand.ExecuteReader();
            try {
                    // Always call Read before accessing data.
                    while (pgReader.Read()) {
                    Console.WriteLine(pgReader.GetInt32(0).ToString() + " " + 
                    pgReader.GetString(1) + " " + pgReader.GetString(2));
            }
            }
            finally {
                    // always call Close when done reading.
                    pgReader.Close();
    
                    // Close the connection when done with it.
                    pgConnection.Close();
            }
    }
    Public Sub ReadMyData(ByVal myConnString As String)
            Dim pgConnection As New PgSqlConnection(myConnString)
            Dim pgCommand As PgSqlCommand = pgConnection.CreateCommand()
                    pgCommand.CommandText = "SELECT DeptNo, DName, Loc FROM Test.Dept"
            pgConnection.Open()
            Dim pgReader As PgSqlDataReader = pgCommand.ExecuteReader()
            Try
                    ' Always call Read before accessing data.
                    While pgReader.Read()
                            Console.WriteLine(String.Concat(pgReader.GetInt32(0).ToString(), " ", _
                            pgReader.GetString(1), " ", pgReader.GetString(2)))
                    End While
            Finally
                    ' always call Close when done reading.
                    pgReader.Close()
    
                            ' Close the connection when done with it.
                            pgConnection.Close()
            End Try
    End Sub
    Inheritance Hierarchy

    System.Object
       System.MarshalByRefObject
          System.Data.Common.DbDataReader
             Devart.Common.DbDataReaderBase
                Devart.Data.PostgreSql.PgSqlDataReader

    Requirements

    Target Platforms: Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

    See Also