4 // Uses the PostgresLibrary to retrieve a recordset.
5 // This is not meant to be used in Production, but as a
6 // learning aid in coding class System.Data.SqlClient.SqlDataReader.
9 // Daniel Morgan <danmorg@sc.rr.com>
11 // (C) 2002 Daniel Morgan
16 using System.Runtime.InteropServices;
17 using System.Diagnostics;
19 namespace LearnToCreateSqlDataReader
21 sealed public class PostgresLibrary {
23 public enum ConnStatusType {
28 CONNECTION_AWAITING_RESPONSE,
33 public enum PostgresPollingStatusType {
34 PGRES_POLLING_FAILED = 0,
35 PGRES_POLLING_READING,
36 PGRES_POLLING_WRITING,
41 public enum ExecStatusType {
42 PGRES_EMPTY_QUERY = 0,
53 public static extern string PQerrorMessage (IntPtr conn);
54 // char *PQerrorMessage(const PGconn *conn);
57 public static extern IntPtr PQconnectdb(String conninfo);
58 // PGconn *PQconnectdb(const char *conninfo)
61 public static extern void PQfinish(IntPtr conn);
62 // void PQfinish(PGconn *conn)
65 public static extern IntPtr PQexec(IntPtr conn,
67 // PGresult *PQexec(PGconn *conn, const char *query);
70 public static extern int PQntuples (IntPtr res);
71 // int PQntuples(const PGresult *res);
74 public static extern int PQnfields (IntPtr res);
75 // int PQnfields(const PGresult *res);
78 public static extern ConnStatusType PQstatus (IntPtr conn);
79 // ConnStatusType PQstatus(const PGconn *conn);
81 public static extern ExecStatusType PQresultStatus (IntPtr res);
82 // ExecStatusType PQresultStatus(const PGresult *res);
85 public static extern string PQresStatus (ExecStatusType status);
86 // char *PQresStatus(ExecStatusType status);
89 public static extern string PQresultErrorMessage (IntPtr res);
90 // char *PQresultErrorMessage(const PGresult *res);
93 public static extern int PQbinaryTuples (IntPtr res);
94 // int PQbinaryTuples(const PGresult *res);
97 public static extern string PQfname (IntPtr res,
99 // char *PQfname(const PGresult *res,
103 public static extern int PQfnumber (IntPtr res,
105 // int PQfnumber(const PGresult *res,
106 // const char *field_name);
110 public static extern int PQfmod (IntPtr res, int field_num);
111 // int PQfmod(const PGresult *res, int field_num);
114 public static extern int PQftype (IntPtr res,
116 // Oid PQftype(const PGresult *res,
120 public static extern int PQfsize (IntPtr res,
122 // int PQfsize(const PGresult *res,
126 public static extern string PQcmdStatus (IntPtr res);
127 // char *PQcmdStatus(PGresult *res);
130 public static extern string PQoidStatus (IntPtr res);
131 // char *PQoidStatus(const PGresult *res);
134 public static extern int PQoidValue (IntPtr res);
135 // Oid PQoidValue(const PGresult *res);
138 public static extern string PQcmdTuples (IntPtr res);
139 // char *PQcmdTuples(PGresult *res);
142 public static extern string PQgetvalue (IntPtr res,
143 int tup_num, int field_num);
144 // char *PQgetvalue(const PGresult *res,
145 // int tup_num, int field_num);
148 public static extern int PQgetlength (IntPtr res,
149 int tup_num, int field_num);
150 // int PQgetlength(const PGresult *res,
151 // int tup_num, int field_num);
154 public static extern int PQgetisnull (IntPtr res,
155 int tup_num, int field_num);
156 // int PQgetisnull(const PGresult *res,
157 // int tup_num, int field_num);
160 public static extern void PQclear (IntPtr res);
161 // void PQclear(PGresult *res);
166 public class ReadPostgresData
174 PostgresLibrary.ConnStatusType connStatus;
179 sConnInfo = "host=localhost dbname=test user=danmorg password=viewsonic";
182 "select tid, tdesc " +
185 pgConn = PostgresLibrary.PQconnectdb (sConnInfo);
187 connStatus = PostgresLibrary.PQstatus (pgConn);
190 ConnStatusType.CONNECTION_OK) {
192 Console.WriteLine("CONNECTION_OK");
194 pgResult = PostgresLibrary.PQexec(pgConn, sQuery);
196 PostgresLibrary.ExecStatusType execStatus;
198 execStatus = PostgresLibrary.
199 PQresultStatus (pgResult);
203 ExecStatusType.PGRES_TUPLES_OK)
205 Console.WriteLine("PGRES_TUPLES_OK");
207 int nRows = PostgresLibrary.
209 Console.WriteLine("Rows: " + nRows);
211 int nFields = PostgresLibrary.
213 Console.WriteLine("Columns: " + nFields);
218 // get meta data fromm result set (schema)
219 // for each column (field)
220 for(int fieldIndex = 0;
221 fieldIndex < nFields;
225 fieldName = PostgresLibrary.
226 PQfname(pgResult, fieldIndex);
228 Console.WriteLine("Field " +
233 // get PostgreSQL data type (OID)
234 oid = PostgresLibrary.
235 PQftype(pgResult, fieldIndex);
238 // get defined size of column
239 definedSize = PostgresLibrary.
240 PQfsize(pgResult, fieldIndex);
243 // for each row and column, get the data value
254 value = PostgresLibrary.
259 Console.WriteLine("Row: " + row +
265 columnIsNull = PostgresLibrary.
266 PQgetisnull(pgResult,
270 actualLength = PostgresLibrary.
271 PQgetlength(pgResult,
277 PostgresLibrary.PQclear (pgResult);
280 // display connection error
281 errorMessage = PostgresLibrary.
282 PQresStatus(execStatus);
284 errorMessage += " " + PostgresLibrary.
285 PQresultErrorMessage(pgResult);
287 Console.WriteLine(errorMessage);
290 // close database conneciton
291 PostgresLibrary.PQfinish(pgConn);
295 errorMessage = PostgresLibrary.
296 PQerrorMessage (pgConn);
297 errorMessage += ": Could not connect to database.";
298 Console.WriteLine(errorMessage);
305 static void Main(string[] args)