4 // To Test MySqlConnection and MySqlCommand by connecting
6 // and then executing an INSERT SQL statement
9 // change strings to your database, userid, tables, etc...:
14 // mcs TestMySqlInsert.cs -r Mono.Data.MySql.dll
15 // mint TestMySqlInsert.exe
18 // Daniel Morgan (danmorg@sc.rr.com)
20 // (C)Copyright 2002 Daniel Morgan
25 using Mono.Data.MySql;
27 namespace TestMonoDataMysql
32 static void Main(string[] args)
36 //MySqlTransaction trans;
40 String connectionString;
41 String insertStatement;
42 String deleteStatement;
48 "insert into sometable " +
50 "values ('beer', 'Beer for All!') ";
53 "delete from sometable " +
54 "where tid = 'beer' ";
56 // Connect to a MySQL database
57 Console.WriteLine ("Connect to database...");
58 conn = new MySqlConnection(connectionString);
62 //Console.WriteLine ("Begin Transaction...");
63 //trans = conn.BeginTransaction();
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);
71 // execute the DELETE SQL command
72 Console.WriteLine ("Execute DELETE SQL Command...");
73 rowsAffected = cmd.ExecuteNonQuery();
74 Console.WriteLine ("Rows Affected: " + rowsAffected);
76 // change the SQL command to an SQL INSERT Command
77 Console.WriteLine ("Now use INSERT SQL Command...");
78 cmd.CommandText = insertStatement;
80 // execute the INSERT SQL command
81 Console.WriteLine ("Execute INSERT SQL Command...");
82 rowsAffected = cmd.ExecuteNonQuery();
83 Console.WriteLine ("Rows Affected: " + rowsAffected);
85 // if successfull at INSERT, commit the transaction,
86 // otherwise, do a rollback the transaction using
88 // FIXME: need to have exceptions working in
89 // Mono.Data.MySql classes before you can do rollback
90 //Console.WriteLine ("Commit transaction...");
93 // Close connection to database
94 Console.WriteLine ("Close database connection...");
97 Console.WriteLine ("Assuming everything " +
99 Console.WriteLine ("Verify data in database to " +
100 "see if row is there.");