TestOleDb.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. //
  2. // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining
  5. // a copy of this software and associated documentation files (the
  6. // "Software"), to deal in the Software without restriction, including
  7. // without limitation the rights to use, copy, modify, merge, publish,
  8. // distribute, sublicense, and/or sell copies of the Software, and to
  9. // permit persons to whom the Software is furnished to do so, subject to
  10. // the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be
  13. // included in all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  16. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  17. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  18. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  19. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  20. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  21. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  22. //
  23. using System;
  24. using System.Data.OleDb;
  25. namespace System.Data.OleDb.Test
  26. {
  27. public class TestOleDb
  28. {
  29. private OleDbConnection m_cnc;
  30. private TestOleDb ()
  31. {
  32. OleDbCommand cmd;
  33. m_cnc = new OleDbConnection ("Provider=PostgreSQL;Addr=127.0.0.1;Database=rodrigo");
  34. m_cnc.Open ();
  35. Console.WriteLine ("Connected to:");
  36. Console.WriteLine (" Data Source: " + m_cnc.DataSource);
  37. Console.WriteLine (" Database: " + m_cnc.Database);
  38. Console.WriteLine (" Connection string: " + m_cnc.ConnectionString);
  39. Console.WriteLine (" Provider: " + m_cnc.Provider);
  40. Console.WriteLine (" Server version:" + m_cnc.ServerVersion);
  41. /* create temporary table */
  42. Console.WriteLine ("Creating temporary table...");
  43. cmd = new OleDbCommand ("CREATE TABLE mono_test_table ( " +
  44. " name varchar(25), email varchar(50), date_entered timestamp)",
  45. m_cnc);
  46. cmd.ExecuteNonQuery ();
  47. InsertRow ("Mike Smith", "[email protected]");
  48. InsertRow ("Julie Andrews", "[email protected]");
  49. InsertRow ("Michael Jordan", "[email protected]");
  50. }
  51. void InsertRow (string name, string email)
  52. {
  53. OleDbCommand cmd;
  54. cmd = new OleDbCommand ("INSERT INTO mono_test_table (name, email, date_entered) VALUES ('" +
  55. name + "', '" + email +"', date 'now')", m_cnc);
  56. Console.WriteLine ("Executing command '" + cmd.CommandText + "'");
  57. cmd.ExecuteNonQuery ();
  58. }
  59. void DisplayRow (OleDbDataReader reader)
  60. {
  61. for (int i = 0; i < reader.FieldCount; i++) {
  62. Console.WriteLine (" " + reader.GetDataTypeName (i) + ": " +
  63. reader.GetValue (i).ToString ());
  64. }
  65. }
  66. void TestDataReader ()
  67. {
  68. int i = 0;
  69. string sql = "SELECT * FROM mono_test_table";
  70. Console.WriteLine ("Executing SELECT command...");
  71. OleDbCommand cmd = new OleDbCommand (sql, m_cnc);
  72. OleDbDataReader reader = cmd.ExecuteReader ();
  73. Console.WriteLine (" Recordset description:");
  74. for (i = 0; i < reader.FieldCount; i++) {
  75. Console.WriteLine (" Field " + i + ": " +
  76. reader.GetName (i) + " (" +
  77. reader.GetDataTypeName (i) + ")");
  78. }
  79. Console.WriteLine ("Reading data...");
  80. i = 0;
  81. while (reader.Read ()) {
  82. Console.WriteLine ("Row " + i + ":");
  83. DisplayRow (reader);
  84. i++;
  85. }
  86. reader.Close ();
  87. }
  88. void TestTransaction ()
  89. {
  90. Console.WriteLine ("Starting transaction...");
  91. OleDbTransaction xaction = m_cnc.BeginTransaction ();
  92. Console.WriteLine ("Aborting transaction...");
  93. xaction.Rollback ();
  94. }
  95. void Close ()
  96. {
  97. OleDbCommand cmd = new OleDbCommand ("DROP TABLE mono_test_table", m_cnc);
  98. cmd.ExecuteNonQuery ();
  99. m_cnc.Close ();
  100. }
  101. static void Main (string[] args)
  102. {
  103. try {
  104. TestOleDb test = new TestOleDb ();
  105. test.TestDataReader ();
  106. test.TestTransaction ();
  107. test.Close ();
  108. } catch (Exception e) {
  109. Console.WriteLine ("An error has occured: {0}", e.ToString ());
  110. }
  111. }
  112. }
  113. }