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