TestSqlException.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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=postgres";
  43. insertStatement =
  44. "insert into NoSuchTable " +
  45. "(tid, tdesc) " +
  46. "values ('beer', 'Beer for All!') ";
  47. deleteStatement =
  48. "delete from sometable " +
  49. "where tid = 'beer' ";
  50. try {
  51. // Connect to a PostgreSQL database
  52. Console.WriteLine ("Connect to database...");
  53. conn = new SqlConnection(connectionString);
  54. conn.Open();
  55. // begin transaction
  56. Console.WriteLine ("Begin Transaction...");
  57. trans = conn.BeginTransaction();
  58. // create SQL DELETE command
  59. Console.WriteLine ("Create Command initializing " +
  60. "with an DELETE statement...");
  61. cmd = new SqlCommand (deleteStatement, conn);
  62. // execute the DELETE SQL command
  63. Console.WriteLine ("Execute DELETE SQL Command...");
  64. rowsAffected = cmd.ExecuteNonQuery();
  65. Console.WriteLine ("Rows Affected: " + rowsAffected);
  66. // change the SQL command to an SQL INSERT Command
  67. Console.WriteLine ("Now use INSERT SQL Command...");
  68. cmd.CommandText = insertStatement;
  69. // execute the INSERT SQL command
  70. Console.WriteLine ("Execute INSERT SQL Command...");
  71. rowsAffected = cmd.ExecuteNonQuery();
  72. Console.WriteLine ("Rows Affected: " + rowsAffected);
  73. // if successfull at INSERT, commit the transaction,
  74. // otherwise, do a rollback the transaction using
  75. // trans.Rollback();
  76. Console.WriteLine ("Commit transaction...");
  77. trans.Commit();
  78. // Close connection to database
  79. Console.WriteLine ("Close database connection...");
  80. conn.Close();
  81. Console.WriteLine ("Assuming everything " +
  82. "was successful.");
  83. Console.WriteLine ("Verify data in database to " +
  84. "see if row is there.");
  85. }
  86. catch(SqlException e) {
  87. // Display the SQL Errors and Rollback the database
  88. Console.WriteLine("SqlException caught: " +
  89. e.ToString());
  90. if(trans != null) {
  91. trans.Rollback();
  92. Console.WriteLine("Database has been Rolled back!");
  93. }
  94. }
  95. finally {
  96. if(conn != null)
  97. if(conn.State == ConnectionState.Open)
  98. conn.Close();
  99. }
  100. }
  101. }
  102. }