2
0

TestSqlException.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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 sometable " +
  46. "(tid, tdesc) " +
  47. "values ('beer', 'Beer for All!') ";
  48. deleteStatement =
  49. "delete from NoSuchTable " +
  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. cmd.ExecuteNonQuery();
  66. rowsAffected = cmd.ExecuteNonQuery();
  67. Console.WriteLine ("Rows Affected: " + rowsAffected);
  68. // change the SQL command to an SQL INSERT Command
  69. Console.WriteLine ("Now use INSERT SQL Command...");
  70. cmd.CommandText = insertStatement;
  71. // execute the INSERT SQL command
  72. Console.WriteLine ("Execute INSERT SQL Command...");
  73. rowsAffected = cmd.ExecuteNonQuery();
  74. Console.WriteLine ("Rows Affected: " + rowsAffected);
  75. // if successfull at INSERT, commit the transaction,
  76. // otherwise, do a rollback the transaction using
  77. // trans.Rollback();
  78. Console.WriteLine ("Commit transaction...");
  79. trans.Commit();
  80. // Close connection to database
  81. Console.WriteLine ("Close database connection...");
  82. conn.Close();
  83. Console.WriteLine ("Assuming everything " +
  84. "was successful.");
  85. Console.WriteLine ("Verify data in database to " +
  86. "see if row is there.");
  87. }
  88. catch(SqlException e) {
  89. // Display the SQL Errors and Rollback the database
  90. Console.WriteLine("SqlException caught: " +
  91. e.ToString());
  92. trans.Rollback();
  93. Console.WriteLine("Database has been Rolled back!");
  94. }
  95. finally {
  96. if(conn.State == ConnectionState.Open)
  97. conn.Close();
  98. }
  99. }
  100. }
  101. }