TestSqlIsolationLevel.cs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. //
  2. // TestSqlIsolationLevel.
  3. //
  4. // To Test Setting Isolation Level of SqlTransaction
  5. // to a PostgreSQL database
  6. //
  7. // To use:
  8. // change strings to your database, userid, tables, etc...:
  9. // connectionString
  10. // insertStatement
  11. //
  12. // To test:
  13. // mcs TestSqlIsolationLevel.cs -r System.Data
  14. // mint TestSqlIsolationLevel.exe
  15. //
  16. // Author:
  17. // Rodrigo Moya ([email protected])
  18. // Daniel Morgan ([email protected])
  19. //
  20. // (C) Ximian, Inc 2002
  21. //
  22. using System;
  23. using System.Data;
  24. using System.Data.SqlClient;
  25. namespace TestSystemDataSqlClient
  26. {
  27. class TestSqlInsert
  28. {
  29. [STAThread]
  30. static void Main(string[] args)
  31. {
  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 sometable " +
  46. "(tid, tdesc) " +
  47. "values ('beer', 'Beer for All!') ";
  48. deleteStatement =
  49. "delete from sometable " +
  50. "where tid = 'beer' ";
  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(IsolationLevel.Serializable);
  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. cmd.ExecuteNonQuery();
  65. // change the SQL command to an SQL INSERT Command
  66. Console.WriteLine ("Now use INSERT SQL Command...");
  67. cmd.CommandText = insertStatement;
  68. // execute the INSERT SQL command
  69. Console.WriteLine ("Execute INSERT SQL Command...");
  70. rowsAffected = cmd.ExecuteNonQuery();
  71. Console.WriteLine ("Rows Affected: " + rowsAffected);
  72. // if successfull at INSERT, commit the transaction,
  73. // otherwise, do a rollback the transaction using
  74. // trans.Rollback();
  75. // FIXME: need to have exceptions working in
  76. // SqlClient classes before you can do 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. }
  88. }