TestSqlDataReader.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. using System;
  15. using System.Data;
  16. using Mono.Data.PostgreSqlClient;
  17. namespace Test.Mono.Data.PostgreSqlClient {
  18. class TestPgSqlDataReader {
  19. static void Test(PgSqlConnection con, string sql,
  20. CommandType cmdType, CommandBehavior behavior,
  21. string testDesc)
  22. {
  23. PgSqlCommand cmd = null;
  24. PgSqlDataReader rdr = null;
  25. int c;
  26. int results = 0;
  27. Console.WriteLine("Test: " + testDesc);
  28. Console.WriteLine("[BEGIN SQL]");
  29. Console.WriteLine(sql);
  30. Console.WriteLine("[END SQL]");
  31. cmd = new PgSqlCommand(sql, con);
  32. cmd.CommandType = cmdType;
  33. Console.WriteLine("ExecuteReader...");
  34. rdr = cmd.ExecuteReader(behavior);
  35. if(rdr == null) {
  36. Console.WriteLine("IDataReader has a Null Reference.");
  37. }
  38. else {
  39. do {
  40. // get the DataTable that holds
  41. // the schema
  42. DataTable dt = rdr.GetSchemaTable();
  43. if(rdr.RecordsAffected != -1) {
  44. // Results for
  45. // SQL INSERT, UPDATE, DELETE Commands
  46. // have RecordsAffected >= 0
  47. Console.WriteLine("Result is from a SQL Command (INSERT,UPDATE,DELETE). Records Affected: " + rdr.RecordsAffected);
  48. }
  49. else if (dt == null)
  50. Console.WriteLine("Result is from a SQL Command not (INSERT,UPDATE,DELETE). Records Affected: " + rdr.RecordsAffected);
  51. else {
  52. // Results for
  53. // SQL not INSERT, UPDATE, nor DELETE
  54. // have RecordsAffected = -1
  55. Console.WriteLine("Result is from a SQL SELECT Query. Records Affected: " + rdr.RecordsAffected);
  56. // Results for a SQL Command (CREATE TABLE, SET, etc)
  57. // will have a null reference returned from GetSchemaTable()
  58. //
  59. // Results for a SQL SELECT Query
  60. // will have a DataTable returned from GetSchemaTable()
  61. results++;
  62. Console.WriteLine("Result Set " + results + "...");
  63. // number of columns in the table
  64. Console.WriteLine(" Total Columns: " +
  65. dt.Columns.Count);
  66. // display the schema
  67. foreach (DataRow schemaRow in dt.Rows) {
  68. foreach (DataColumn schemaCol in dt.Columns)
  69. Console.WriteLine(schemaCol.ColumnName +
  70. " = " +
  71. schemaRow[schemaCol]);
  72. Console.WriteLine();
  73. }
  74. int nRows = 0;
  75. string output, metadataValue, dataValue;
  76. // Read and display the rows
  77. Console.WriteLine("Gonna do a Read() now...");
  78. while(rdr.Read()) {
  79. Console.WriteLine(" Row " + nRows + ": ");
  80. for(c = 0; c < rdr.FieldCount; c++) {
  81. // column meta data
  82. DataRow dr = dt.Rows[c];
  83. metadataValue =
  84. " Col " +
  85. c + ": " +
  86. dr["ColumnName"];
  87. // column data
  88. if(rdr.IsDBNull(c) == true)
  89. dataValue = " is NULL";
  90. else
  91. dataValue =
  92. ": " +
  93. rdr.GetValue(c);
  94. // display column meta data and data
  95. output = metadataValue + dataValue;
  96. Console.WriteLine(output);
  97. }
  98. nRows++;
  99. }
  100. Console.WriteLine(" Total Rows: " +
  101. nRows);
  102. }
  103. } while(rdr.NextResult());
  104. Console.WriteLine("Total Result sets: " + results);
  105. rdr.Close();
  106. }
  107. }
  108. [STAThread]
  109. static void Main(string[] args) {
  110. String connectionString = null;
  111. connectionString =
  112. "host=localhost;" +
  113. "dbname=test;" +
  114. "user=postgres";
  115. PgSqlConnection con;
  116. con = new PgSqlConnection(connectionString);
  117. con.Open();
  118. string sql;
  119. // Text - only has one query (single query behavior)
  120. sql = "select * from pg_tables";
  121. Test(con, sql, CommandType.Text,
  122. CommandBehavior.SingleResult, "Text1");
  123. // Text - only has one query (default behavior)
  124. sql = "select * from pg_tables";
  125. Test(con, sql, CommandType.Text,
  126. CommandBehavior.Default, "Text2");
  127. // Text - has three queries
  128. sql =
  129. "select * from pg_user;" +
  130. "select * from pg_tables;" +
  131. "select * from pg_database";
  132. Test(con, sql, CommandType.Text,
  133. CommandBehavior.Default, "Text3Queries");
  134. // Table Direct
  135. sql = "pg_tables";
  136. Test(con, sql, CommandType.TableDirect,
  137. CommandBehavior.Default, "TableDirect1");
  138. // Stored Procedure
  139. sql = "version";
  140. Test(con, sql, CommandType.StoredProcedure,
  141. CommandBehavior.Default, "SP1");
  142. // Text - test a SQL Command (default behavior)
  143. // Note: this not a SQL Query
  144. sql = "SET DATESTYLE TO 'ISO'";
  145. Test(con, sql, CommandType.Text,
  146. CommandBehavior.Default, "TextCmd1");
  147. con.Close();
  148. }
  149. }
  150. }