TestSqlDataReader.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. //
  2. // Test/SqlDataReader.cs - to test Mono.Data.PostgreSqlClient/PgSqlDataReader.cs
  3. //
  4. // Test to do read a simple forward read only record set.
  5. // Using PgSqlCommand.ExecuteReader() to return a PgSqlDataReader
  6. // which can be used to Read a row
  7. // and Get a String or Int32.
  8. //
  9. // Author:
  10. // Daniel Morgan <[email protected]>
  11. //
  12. // (C) 2002 Daniel Morgan
  13. //
  14. //
  15. // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
  16. //
  17. // Permission is hereby granted, free of charge, to any person obtaining
  18. // a copy of this software and associated documentation files (the
  19. // "Software"), to deal in the Software without restriction, including
  20. // without limitation the rights to use, copy, modify, merge, publish,
  21. // distribute, sublicense, and/or sell copies of the Software, and to
  22. // permit persons to whom the Software is furnished to do so, subject to
  23. // the following conditions:
  24. //
  25. // The above copyright notice and this permission notice shall be
  26. // included in all copies or substantial portions of the Software.
  27. //
  28. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  29. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  30. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  31. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  32. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  33. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  34. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  35. //
  36. using System;
  37. using System.Data;
  38. using Mono.Data.PostgreSqlClient;
  39. namespace Test.Mono.Data.PostgreSqlClient {
  40. class TestPgSqlDataReader {
  41. static void Test(PgSqlConnection con, string sql,
  42. CommandType cmdType, CommandBehavior behavior,
  43. string testDesc)
  44. {
  45. PgSqlCommand cmd = null;
  46. PgSqlDataReader rdr = null;
  47. int c;
  48. int results = 0;
  49. Console.WriteLine("Test: " + testDesc);
  50. Console.WriteLine("[BEGIN SQL]");
  51. Console.WriteLine(sql);
  52. Console.WriteLine("[END SQL]");
  53. cmd = new PgSqlCommand(sql, con);
  54. cmd.CommandType = cmdType;
  55. Console.WriteLine("ExecuteReader...");
  56. rdr = cmd.ExecuteReader(behavior);
  57. if(rdr == null) {
  58. Console.WriteLine("IDataReader has a Null Reference.");
  59. }
  60. else {
  61. do {
  62. // get the DataTable that holds
  63. // the schema
  64. DataTable dt = rdr.GetSchemaTable();
  65. if(rdr.RecordsAffected != -1) {
  66. // Results for
  67. // SQL INSERT, UPDATE, DELETE Commands
  68. // have RecordsAffected >= 0
  69. Console.WriteLine("Result is from a SQL Command (INSERT,UPDATE,DELETE). Records Affected: " + rdr.RecordsAffected);
  70. }
  71. else if (dt == null)
  72. Console.WriteLine("Result is from a SQL Command not (INSERT,UPDATE,DELETE). Records Affected: " + rdr.RecordsAffected);
  73. else {
  74. // Results for
  75. // SQL not INSERT, UPDATE, nor DELETE
  76. // have RecordsAffected = -1
  77. Console.WriteLine("Result is from a SQL SELECT Query. Records Affected: " + rdr.RecordsAffected);
  78. // Results for a SQL Command (CREATE TABLE, SET, etc)
  79. // will have a null reference returned from GetSchemaTable()
  80. //
  81. // Results for a SQL SELECT Query
  82. // will have a DataTable returned from GetSchemaTable()
  83. results++;
  84. Console.WriteLine("Result Set " + results + "...");
  85. // number of columns in the table
  86. Console.WriteLine(" Total Columns: " +
  87. dt.Columns.Count);
  88. // display the schema
  89. foreach (DataRow schemaRow in dt.Rows) {
  90. foreach (DataColumn schemaCol in dt.Columns)
  91. Console.WriteLine(schemaCol.ColumnName +
  92. " = " +
  93. schemaRow[schemaCol]);
  94. Console.WriteLine();
  95. }
  96. int nRows = 0;
  97. string output, metadataValue, dataValue;
  98. // Read and display the rows
  99. Console.WriteLine("Gonna do a Read() now...");
  100. while(rdr.Read()) {
  101. Console.WriteLine(" Row " + nRows + ": ");
  102. for(c = 0; c < rdr.FieldCount; c++) {
  103. // column meta data
  104. DataRow dr = dt.Rows[c];
  105. metadataValue =
  106. " Col " +
  107. c + ": " +
  108. dr["ColumnName"];
  109. // column data
  110. if(rdr.IsDBNull(c) == true)
  111. dataValue = " is NULL";
  112. else
  113. dataValue =
  114. ": " +
  115. rdr.GetValue(c);
  116. // display column meta data and data
  117. output = metadataValue + dataValue;
  118. Console.WriteLine(output);
  119. }
  120. nRows++;
  121. }
  122. Console.WriteLine(" Total Rows: " +
  123. nRows);
  124. }
  125. } while(rdr.NextResult());
  126. Console.WriteLine("Total Result sets: " + results);
  127. rdr.Close();
  128. }
  129. }
  130. [STAThread]
  131. static void Main(string[] args) {
  132. String connectionString = null;
  133. connectionString =
  134. "host=localhost;" +
  135. "dbname=test;" +
  136. "user=postgres";
  137. PgSqlConnection con;
  138. con = new PgSqlConnection(connectionString);
  139. con.Open();
  140. string sql;
  141. // Text - only has one query (single query behavior)
  142. sql = "select * from pg_tables";
  143. Test(con, sql, CommandType.Text,
  144. CommandBehavior.SingleResult, "Text1");
  145. // Text - only has one query (default behavior)
  146. sql = "select * from pg_tables";
  147. Test(con, sql, CommandType.Text,
  148. CommandBehavior.Default, "Text2");
  149. // Text - has three queries
  150. sql =
  151. "select * from pg_user;" +
  152. "select * from pg_tables;" +
  153. "select * from pg_database";
  154. Test(con, sql, CommandType.Text,
  155. CommandBehavior.Default, "Text3Queries");
  156. // Table Direct
  157. sql = "pg_tables";
  158. Test(con, sql, CommandType.TableDirect,
  159. CommandBehavior.Default, "TableDirect1");
  160. // Stored Procedure
  161. sql = "version";
  162. Test(con, sql, CommandType.StoredProcedure,
  163. CommandBehavior.Default, "SP1");
  164. // Text - test a SQL Command (default behavior)
  165. // Note: this not a SQL Query
  166. sql = "SET DATESTYLE TO 'ISO'";
  167. Test(con, sql, CommandType.Text,
  168. CommandBehavior.Default, "TextCmd1");
  169. con.Close();
  170. }
  171. }
  172. }