TestSqlParameters.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. using System;
  14. using System.Collections;
  15. using System.Data;
  16. using Mono.Data.PostgreSqlClient;
  17. namespace TestSystemDataPgSqlClient {
  18. public class TestParameters {
  19. public static void Main() {
  20. Console.WriteLine("** Start Test...");
  21. String connectionString = null;
  22. connectionString =
  23. "host=localhost;" +
  24. "dbname=test;" +
  25. "user=postgres";
  26. PgSqlConnection con;
  27. Console.WriteLine("** Creating connection...");
  28. con = new PgSqlConnection(connectionString);
  29. Console.WriteLine("** opening connection...");
  30. con.Open();
  31. string tableName = "pg_type";
  32. string sql;
  33. sql = "SELECT * FROM PG_TABLES WHERE TABLENAME = :inTableName";
  34. Console.WriteLine("** Creating command...");
  35. PgSqlCommand cmd = new PgSqlCommand(sql, con);
  36. // add parameter for inTableName
  37. Console.WriteLine("** Create parameter...");
  38. PgSqlParameter parm = new PgSqlParameter("inTableName", DbType.String);
  39. Console.WriteLine("** set dbtype of parameter to string");
  40. parm.DbType = DbType.String;
  41. Console.WriteLine("** set direction of parameter to input");
  42. parm.Direction = ParameterDirection.Input;
  43. Console.WriteLine("** set value to the tableName string...");
  44. parm.Value = tableName;
  45. Console.WriteLine("** add parameter to parameters collection in the command...");
  46. cmd.Parameters.Add(parm);
  47. PgSqlDataReader rdr;
  48. Console.WriteLine("** ExecuteReader()...");
  49. rdr = cmd.ExecuteReader();
  50. Console.WriteLine("[][] And now we are going to our results [][]...");
  51. int c;
  52. int results = 0;
  53. do {
  54. results++;
  55. Console.WriteLine("Result Set " + results + "...");
  56. // get the DataTable that holds
  57. // the schema
  58. DataTable dt = rdr.GetSchemaTable();
  59. // number of columns in the table
  60. Console.WriteLine(" Total Columns: " +
  61. dt.Columns.Count);
  62. // display the schema
  63. foreach (DataRow schemaRow in dt.Rows) {
  64. foreach (DataColumn schemaCol in dt.Columns)
  65. Console.WriteLine(schemaCol.ColumnName +
  66. " = " +
  67. schemaRow[schemaCol]);
  68. Console.WriteLine();
  69. }
  70. string output, metadataValue, dataValue;
  71. int nRows = 0;
  72. // Read and display the rows
  73. while(rdr.Read()) {
  74. Console.WriteLine(" Row " + nRows + ": ");
  75. for(c = 0; c < rdr.FieldCount; c++) {
  76. // column meta data
  77. DataRow dr = dt.Rows[c];
  78. metadataValue =
  79. " Col " +
  80. c + ": " +
  81. dr["ColumnName"];
  82. // column data
  83. if(rdr.IsDBNull(c) == true)
  84. dataValue = " is NULL";
  85. else
  86. dataValue =
  87. ": " +
  88. rdr.GetValue(c);
  89. // display column meta data and data
  90. output = metadataValue + dataValue;
  91. Console.WriteLine(output);
  92. }
  93. nRows++;
  94. }
  95. Console.WriteLine(" Total Rows: " +
  96. nRows);
  97. } while(rdr.NextResult());
  98. Console.WriteLine("Total Result sets: " + results);
  99. con.Close();
  100. }
  101. }
  102. }