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