TestSqlParameters.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. //
  2. // TestSqlParameters.cs - test parameters for the PostgreSQL .NET Data Provider in Mono
  3. // using PgSqlParameter and PgSqlParameterCollection
  4. //
  5. // Note: it currently only tests input parameters. Output is next on the list.
  6. // Then output/input and return parameters.
  7. //
  8. // Author:
  9. // Daniel Morgan <[email protected]>
  10. //
  11. // (c)copyright 2002 Daniel Morgan
  12. //
  13. //
  14. // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
  15. //
  16. // Permission is hereby granted, free of charge, to any person obtaining
  17. // a copy of this software and associated documentation files (the
  18. // "Software"), to deal in the Software without restriction, including
  19. // without limitation the rights to use, copy, modify, merge, publish,
  20. // distribute, sublicense, and/or sell copies of the Software, and to
  21. // permit persons to whom the Software is furnished to do so, subject to
  22. // the following conditions:
  23. //
  24. // The above copyright notice and this permission notice shall be
  25. // included in all copies or substantial portions of the Software.
  26. //
  27. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  28. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  29. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  30. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  31. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  32. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  33. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  34. //
  35. using System;
  36. using System.Collections;
  37. using System.Data;
  38. using Mono.Data.PostgreSqlClient;
  39. namespace TestSystemDataPgSqlClient {
  40. public class TestParameters {
  41. public static void Main() {
  42. Console.WriteLine("** Start Test...");
  43. String connectionString = null;
  44. connectionString =
  45. "host=localhost;" +
  46. "dbname=test;" +
  47. "user=postgres";
  48. PgSqlConnection con;
  49. Console.WriteLine("** Creating connection...");
  50. con = new PgSqlConnection(connectionString);
  51. Console.WriteLine("** opening connection...");
  52. con.Open();
  53. string tableName = "pg_type";
  54. string sql;
  55. sql = "SELECT * FROM PG_TABLES WHERE TABLENAME = :inTableName";
  56. Console.WriteLine("** Creating command...");
  57. PgSqlCommand cmd = new PgSqlCommand(sql, con);
  58. // add parameter for inTableName
  59. Console.WriteLine("** Create parameter...");
  60. PgSqlParameter parm = new PgSqlParameter("inTableName", DbType.String);
  61. Console.WriteLine("** set dbtype of parameter to string");
  62. parm.DbType = DbType.String;
  63. Console.WriteLine("** set direction of parameter to input");
  64. parm.Direction = ParameterDirection.Input;
  65. Console.WriteLine("** set value to the tableName string...");
  66. parm.Value = tableName;
  67. Console.WriteLine("** add parameter to parameters collection in the command...");
  68. cmd.Parameters.Add(parm);
  69. PgSqlDataReader rdr;
  70. Console.WriteLine("** ExecuteReader()...");
  71. rdr = cmd.ExecuteReader();
  72. Console.WriteLine("[][] And now we are going to our results [][]...");
  73. int c;
  74. int results = 0;
  75. do {
  76. results++;
  77. Console.WriteLine("Result Set " + results + "...");
  78. // get the DataTable that holds
  79. // the schema
  80. DataTable dt = rdr.GetSchemaTable();
  81. // number of columns in the table
  82. Console.WriteLine(" Total Columns: " +
  83. dt.Columns.Count);
  84. // display the schema
  85. foreach (DataRow schemaRow in dt.Rows) {
  86. foreach (DataColumn schemaCol in dt.Columns)
  87. Console.WriteLine(schemaCol.ColumnName +
  88. " = " +
  89. schemaRow[schemaCol]);
  90. Console.WriteLine();
  91. }
  92. string output, metadataValue, dataValue;
  93. int nRows = 0;
  94. // Read and display the rows
  95. while(rdr.Read()) {
  96. Console.WriteLine(" Row " + nRows + ": ");
  97. for(c = 0; c < rdr.FieldCount; c++) {
  98. // column meta data
  99. DataRow dr = dt.Rows[c];
  100. metadataValue =
  101. " Col " +
  102. c + ": " +
  103. dr["ColumnName"];
  104. // column data
  105. if(rdr.IsDBNull(c) == true)
  106. dataValue = " is NULL";
  107. else
  108. dataValue =
  109. ": " +
  110. rdr.GetValue(c);
  111. // display column meta data and data
  112. output = metadataValue + dataValue;
  113. Console.WriteLine(output);
  114. }
  115. nRows++;
  116. }
  117. Console.WriteLine(" Total Rows: " +
  118. nRows);
  119. } while(rdr.NextResult());
  120. Console.WriteLine("Total Result sets: " + results);
  121. con.Close();
  122. }
  123. }
  124. }