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