2002-08-23 Nick Drochak <ndrochak@gol.com>
[mono.git] / mcs / class / Mono.Data.MySql / Test / TestMySqlInsert.cs
1 //
2 // TestSqlInsert.cs
3 //
4 // To Test MySqlConnection and MySqlCommand by connecting
5 // to a MySQL database 
6 // and then executing an INSERT SQL statement
7 //
8 // To use:
9 //   change strings to your database, userid, tables, etc...:
10 //        connectionString
11 //        insertStatement
12 //
13 // To test:
14 //   mcs TestMySqlInsert.cs -r Mono.Data.MySql.dll
15 //   mint TestMySqlInsert.exe
16 //
17 // Author:
18 //   Daniel Morgan (danmorg@sc.rr.com)
19 //
20 // (C)Copyright 2002 Daniel Morgan
21 //
22
23 using System;
24 using System.Data;
25 using Mono.Data.MySql;
26
27 namespace TestMonoDataMysql
28 {
29         class TestMySqlInsert
30         {
31                 [STAThread]
32                 static void Main(string[] args)
33                 {
34                         MySqlConnection conn;
35                         MySqlCommand cmd;
36                         //MySqlTransaction trans;
37
38                         int rowsAffected;
39
40                         String connectionString;
41                         String insertStatement;
42                         String deleteStatement;
43         
44                         connectionString = 
45                                 "dbname=test";
46
47                         insertStatement = 
48                                 "insert into sometable " +
49                                 "(tid, tdesc) " +
50                                 "values ('beer', 'Beer for All!') ";
51
52                         deleteStatement = 
53                                 "delete from sometable " +
54                                 "where tid = 'beer' ";
55
56                         // Connect to a MySQL database
57                         Console.WriteLine ("Connect to database...");
58                         conn = new MySqlConnection(connectionString);
59                         conn.Open();
60
61                         // begin transaction
62                         //Console.WriteLine ("Begin Transaction...");
63                         //trans = conn.BeginTransaction();
64
65                         // create SQL DELETE command
66                         Console.WriteLine ("Create Command initializing " +
67                                 "with an DELETE statement...");
68                         //cmd = new MySqlCommand (deleteStatement, conn);
69                         cmd = new MySqlCommand (deleteStatement, conn);
70
71                         // execute the DELETE SQL command
72                         Console.WriteLine ("Execute DELETE SQL Command...");
73                         rowsAffected = cmd.ExecuteNonQuery();
74                         Console.WriteLine ("Rows Affected: " + rowsAffected);
75
76                         // change the SQL command to an SQL INSERT Command
77                         Console.WriteLine ("Now use INSERT SQL Command...");
78                         cmd.CommandText = insertStatement;
79
80                         // execute the INSERT SQL command
81                         Console.WriteLine ("Execute INSERT SQL Command...");
82                         rowsAffected = cmd.ExecuteNonQuery();
83                         Console.WriteLine ("Rows Affected: " + rowsAffected);
84
85                         // if successfull at INSERT, commit the transaction,
86                         // otherwise, do a rollback the transaction using
87                         // trans.Rollback();
88                         // FIXME: need to have exceptions working in
89                         //        Mono.Data.MySql classes before you can do rollback
90                         //Console.WriteLine ("Commit transaction...");
91                         //trans.Commit();
92
93                         // Close connection to database
94                         Console.WriteLine ("Close database connection...");
95                         conn.Close();
96
97                         Console.WriteLine ("Assuming everything " +
98                                 "was successful.");
99                         Console.WriteLine ("Verify data in database to " +
100                                 "see if row is there.");
101                 }
102         }
103 }