Reads a forward-only stream of rows from PostgreSQL.
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
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