2 // Test/SqlDataReader.cs - to test Mono.Data.PostgreSqlClient/PgSqlDataReader.cs
\r
4 // Test to do read a simple forward read only record set.
\r
5 // Using PgSqlCommand.ExecuteReader() to return a PgSqlDataReader
\r
6 // which can be used to Read a row
\r
7 // and Get a String or Int32.
\r
10 // Daniel Morgan <danmorg@sc.rr.com>
\r
12 // (C) 2002 Daniel Morgan
\r
17 using Mono.Data.PostgreSqlClient;
\r
19 namespace Test.Mono.Data.PostgreSqlClient {
\r
20 class TestPgSqlDataReader {
\r
22 static void Test(PgSqlConnection con, string sql,
\r
23 CommandType cmdType, CommandBehavior behavior,
\r
26 PgSqlCommand cmd = null;
\r
27 PgSqlDataReader rdr = null;
\r
32 Console.WriteLine("Test: " + testDesc);
\r
33 Console.WriteLine("[BEGIN SQL]");
\r
34 Console.WriteLine(sql);
\r
35 Console.WriteLine("[END SQL]");
\r
37 cmd = new PgSqlCommand(sql, con);
\r
38 cmd.CommandType = cmdType;
\r
40 Console.WriteLine("ExecuteReader...");
\r
41 rdr = cmd.ExecuteReader(behavior);
\r
45 Console.WriteLine("IDataReader has a Null Reference.");
50 // get the DataTable that holds
\r
52 DataTable dt = rdr.GetSchemaTable();
\r
54 if(rdr.RecordsAffected != -1) {
\r
56 // SQL INSERT, UPDATE, DELETE Commands
\r
57 // have RecordsAffected >= 0
\r
58 Console.WriteLine("Result is from a SQL Command (INSERT,UPDATE,DELETE). Records Affected: " + rdr.RecordsAffected);
\r
60 else if (dt == null)
\r
61 Console.WriteLine("Result is from a SQL Command not (INSERT,UPDATE,DELETE). Records Affected: " + rdr.RecordsAffected);
\r
64 // SQL not INSERT, UPDATE, nor DELETE
\r
65 // have RecordsAffected = -1
\r
66 Console.WriteLine("Result is from a SQL SELECT Query. Records Affected: " + rdr.RecordsAffected);
\r
68 // Results for a SQL Command (CREATE TABLE, SET, etc)
\r
69 // will have a null reference returned from GetSchemaTable()
\r
71 // Results for a SQL SELECT Query
\r
72 // will have a DataTable returned from GetSchemaTable()
\r
75 Console.WriteLine("Result Set " + results + "...");
\r
77 // number of columns in the table
\r
78 Console.WriteLine(" Total Columns: " +
\r
81 // display the schema
\r
82 foreach (DataRow schemaRow in dt.Rows) {
\r
83 foreach (DataColumn schemaCol in dt.Columns)
\r
84 Console.WriteLine(schemaCol.ColumnName +
\r
86 schemaRow[schemaCol]);
\r
87 Console.WriteLine();
\r
91 string output, metadataValue, dataValue;
\r
92 // Read and display the rows
\r
93 Console.WriteLine("Gonna do a Read() now...");
\r
95 Console.WriteLine(" Row " + nRows + ": ");
\r
97 for(c = 0; c < rdr.FieldCount; c++) {
\r
98 // column meta data
\r
99 DataRow dr = dt.Rows[c];
\r
106 if(rdr.IsDBNull(c) == true)
\r
107 dataValue = " is NULL";
\r
113 // display column meta data and data
\r
114 output = metadataValue + dataValue;
\r
115 Console.WriteLine(output);
\r
119 Console.WriteLine(" Total Rows: " +
\r
122 } while(rdr.NextResult());
\r
123 Console.WriteLine("Total Result sets: " + results);
\r
131 static void Main(string[] args) {
\r
132 String connectionString = null;
\r
138 PgSqlConnection con;
\r
139 con = new PgSqlConnection(connectionString);
\r
144 // Text - only has one query (single query behavior)
\r
145 sql = "select * from pg_tables";
\r
146 Test(con, sql, CommandType.Text,
\r
147 CommandBehavior.SingleResult, "Text1");
\r
149 // Text - only has one query (default behavior)
\r
150 sql = "select * from pg_tables";
\r
151 Test(con, sql, CommandType.Text,
\r
152 CommandBehavior.Default, "Text2");
\r
154 // Text - has three queries
\r
156 "select * from pg_user;" +
\r
157 "select * from pg_tables;" +
\r
158 "select * from pg_database";
\r
159 Test(con, sql, CommandType.Text,
\r
160 CommandBehavior.Default, "Text3Queries");
\r
164 Test(con, sql, CommandType.TableDirect,
\r
165 CommandBehavior.Default, "TableDirect1");
\r
167 // Stored Procedure
\r
169 Test(con, sql, CommandType.StoredProcedure,
\r
170 CommandBehavior.Default, "SP1");
\r
172 // Text - test a SQL Command (default behavior)
\r
173 // Note: this not a SQL Query
\r
174 sql = "SET DATESTYLE TO 'ISO'";
\r
175 Test(con, sql, CommandType.Text,
\r
176 CommandBehavior.Default, "TextCmd1");
\r