TestOleDb.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. using System;
  2. using System.Data.OleDb;
  3. namespace System.Data.OleDb.Test
  4. {
  5. public class TestOleDb
  6. {
  7. private OleDbConnection m_cnc;
  8. private TestOleDb ()
  9. {
  10. OleDbCommand cmd;
  11. m_cnc = new OleDbConnection ("Provider=PostgreSQL;Addr=127.0.0.1;Database=rodrigo");
  12. m_cnc.Open ();
  13. Console.WriteLine ("Connected to:");
  14. Console.WriteLine (" Data Source: " + m_cnc.DataSource);
  15. Console.WriteLine (" Database: " + m_cnc.Database);
  16. Console.WriteLine (" Connection string: " + m_cnc.ConnectionString);
  17. Console.WriteLine (" Provider: " + m_cnc.Provider);
  18. Console.WriteLine (" Server version:" + m_cnc.ServerVersion);
  19. /* create temporary table */
  20. Console.WriteLine ("Creating temporary table...");
  21. cmd = new OleDbCommand ("CREATE TABLE mono_test_table ( " +
  22. " name varchar(25), email varchar(50), date_entered timestamp)",
  23. m_cnc);
  24. cmd.ExecuteNonQuery ();
  25. InsertRow ("Mike Smith", "[email protected]");
  26. InsertRow ("Julie Andrews", "[email protected]");
  27. InsertRow ("Michael Jordan", "[email protected]");
  28. }
  29. void InsertRow (string name, string email)
  30. {
  31. OleDbCommand cmd;
  32. cmd = new OleDbCommand ("INSERT INTO mono_test_table (name, email, date_entered) VALUES ('" +
  33. name + "', '" + email +"', date 'now')", m_cnc);
  34. Console.WriteLine ("Executing command '" + cmd.CommandText + "'");
  35. cmd.ExecuteNonQuery ();
  36. }
  37. void DisplayRow (OleDbDataReader reader)
  38. {
  39. for (int i = 0; i < reader.FieldCount; i++) {
  40. Console.WriteLine (" " + reader.GetDataTypeName (i) + ": " +
  41. reader.GetValue (i).ToString ());
  42. }
  43. }
  44. void TestDataReader ()
  45. {
  46. int i = 0;
  47. string sql = "SELECT * FROM mono_test_table";
  48. Console.WriteLine ("Executing SELECT command...");
  49. OleDbCommand cmd = new OleDbCommand (sql, m_cnc);
  50. OleDbDataReader reader = cmd.ExecuteReader ();
  51. Console.WriteLine (" Recordset description:");
  52. for (i = 0; i < reader.FieldCount; i++) {
  53. Console.WriteLine (" Field " + i + ": " +
  54. reader.GetName (i) + " (" +
  55. reader.GetDataTypeName (i) + ")");
  56. }
  57. Console.WriteLine ("Reading data...");
  58. i = 0;
  59. while (reader.Read ()) {
  60. Console.WriteLine ("Row " + i + ":");
  61. DisplayRow (reader);
  62. i++;
  63. }
  64. reader.Close ();
  65. }
  66. void TestTransaction ()
  67. {
  68. Console.WriteLine ("Starting transaction...");
  69. OleDbTransaction xaction = m_cnc.BeginTransaction ();
  70. Console.WriteLine ("Aborting transaction...");
  71. xaction.Rollback ();
  72. }
  73. void Close ()
  74. {
  75. OleDbCommand cmd = new OleDbCommand ("DROP TABLE mono_test_table", m_cnc);
  76. cmd.ExecuteNonQuery ();
  77. m_cnc.Close ();
  78. }
  79. static void Main (string[] args)
  80. {
  81. try {
  82. TestOleDb test = new TestOleDb ();
  83. test.TestDataReader ();
  84. test.TestTransaction ();
  85. test.Close ();
  86. } catch (Exception e) {
  87. Console.WriteLine ("An error has occured: {0}", e.ToString ());
  88. }
  89. }
  90. }
  91. }