TestSqlException.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. //
  2. // TestSqlInsert.cs
  3. //
  4. // To Test SqlConnection and SqlCommand by connecting
  5. // to a PostgreSQL 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 TestSqlInsert.cs -r System.Data
  15. // mint TestSqlInsert.exe
  16. //
  17. // Author:
  18. // Rodrigo Moya ([email protected])
  19. // Daniel Morgan ([email protected])
  20. //
  21. // (C) Ximian, Inc 2002
  22. //
  23. using System;
  24. using System.Data;
  25. using System.Data.SqlClient;
  26. namespace TestSystemDataSqlClient
  27. {
  28. class TestSqlInsert
  29. {
  30. [STAThread]
  31. static void Main(string[] args) {
  32. SqlConnection conn;
  33. SqlCommand cmd;
  34. SqlTransaction trans;
  35. int rowsAffected;
  36. String connectionString;
  37. String insertStatement;
  38. String deleteStatement;
  39. connectionString =
  40. "host=localhost;" +
  41. "dbname=test;" +
  42. "user=danmorg;" +
  43. "password=viewsonic";
  44. insertStatement =
  45. "insert into NoSuchTable " +
  46. "(tid, tdesc) " +
  47. "values ('beer', 'Beer for All!') ";
  48. deleteStatement =
  49. "delete from sometable " +
  50. "where tid = 'beer' ";
  51. try {
  52. // Connect to a PostgreSQL database
  53. Console.WriteLine ("Connect to database...");
  54. conn = new SqlConnection(connectionString);
  55. conn.Open();
  56. // begin transaction
  57. Console.WriteLine ("Begin Transaction...");
  58. trans = conn.BeginTransaction();
  59. // create SQL DELETE command
  60. Console.WriteLine ("Create Command initializing " +
  61. "with an DELETE statement...");
  62. cmd = new SqlCommand (deleteStatement, conn);
  63. // execute the DELETE SQL command
  64. Console.WriteLine ("Execute DELETE SQL Command...");
  65. rowsAffected = cmd.ExecuteNonQuery();
  66. Console.WriteLine ("Rows Affected: " + rowsAffected);
  67. // change the SQL command to an SQL INSERT Command
  68. Console.WriteLine ("Now use INSERT SQL Command...");
  69. cmd.CommandText = insertStatement;
  70. // execute the INSERT SQL command
  71. Console.WriteLine ("Execute INSERT SQL Command...");
  72. rowsAffected = cmd.ExecuteNonQuery();
  73. Console.WriteLine ("Rows Affected: " + rowsAffected);
  74. // if successfull at INSERT, commit the transaction,
  75. // otherwise, do a rollback the transaction using
  76. // trans.Rollback();
  77. Console.WriteLine ("Commit transaction...");
  78. trans.Commit();
  79. // Close connection to database
  80. Console.WriteLine ("Close database connection...");
  81. conn.Close();
  82. Console.WriteLine ("Assuming everything " +
  83. "was successful.");
  84. Console.WriteLine ("Verify data in database to " +
  85. "see if row is there.");
  86. }
  87. catch(SqlException e) {
  88. // Display the SQL Errors and Rollback the database
  89. Console.WriteLine("SqlException caught: " +
  90. e.ToString());
  91. trans.Rollback();
  92. Console.WriteLine("Database has been Rolled back!");
  93. }
  94. finally {
  95. if(conn.State == ConnectionState.Open)
  96. conn.Close();
  97. }
  98. }
  99. }
  100. }