Represents a SQL statement or stored procedure to execute against PostgreSQL.
The following example uses the
ExecuteReader method of
PgSqlCommand, along with
PgSqlDataReader and
PgSqlConnection, to select rows from a table.
|
|---|
public void ReadMyData(string myConnString)
{
string mySelectQuery = "SELECT DeptNo, DName FROM Test.Dept";
PgSqlConnection pgSqlConnection = new PgSqlConnection(myConnString);
PgSqlCommand pgSqlCommand = new PgSqlCommand(mySelectQuery,pgSqlConnection);
pgSqlConnection.Open();
PgSqlDataReader pgSqlReader = pgSqlCommand.ExecuteReader();
try
{
while (pgSqlReader.Read())
{
Console.WriteLine(pgSqlReader.GetInt32(0).ToString() + ", " + pgSqlReader.GetString(1));
}
}
finally
{
// always call Close when done reading.
pgSqlReader.Close();
// always call Close when done reading.
pgSqlConnection.Close();
}
} |
|
|---|
Public Sub ReadMyData(myConnString As String)
Dim mySelectQuery As String = "SELECT DeptNo, DName FROM Test.Dept"
Dim pgSqlConnection As New PgSqlConnection(myConnString)
Dim pgSqlCommand As New PgSqlCommand(mySelectQuery, pgSqlConnection)
pgSqlConnection.Open()
Dim pgSqlReader As PgSqlDataReader = pgSqlCommand.ExecuteReader()
Try
While pgSqlReader.Read()
Console.WriteLine(pgSqlReader.GetInt32(0).ToString() + ", " _
+ pgSqlReader.GetString(1))
End While
Finally
' always call Close when done reading.
pgSqlReader.Close()
' always call Close when done with connection.
pgSqlConnection.Close()
End Try
End Sub |