TestSqlInsert.cs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. {
  33. SqlConnection conn;
  34. SqlCommand cmd;
  35. SqlTransaction trans;
  36. int rowsAffected;
  37. String connectionString;
  38. String insertStatement;
  39. String deleteStatement;
  40. connectionString =
  41. "host=localhost;" +
  42. "dbname=test;" +
  43. "user=danmorg;" +
  44. "password=viewsonic";
  45. insertStatement =
  46. "insert into sometable " +
  47. "(tid, tdesc) " +
  48. "values ('beer', 'Beer for All!') ";
  49. deleteStatement =
  50. "delete from sometable " +
  51. "where tid = 'beer' ";
  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. // FIXME: need to have exceptions working in
  78. // SqlClient classes before you can do rollback
  79. Console.WriteLine ("Commit transaction...");
  80. trans.Commit();
  81. // Close connection to database
  82. Console.WriteLine ("Close database connection...");
  83. conn.Close();
  84. Console.WriteLine ("Assuming everything " +
  85. "was successful.");
  86. Console.WriteLine ("Verify data in database to " +
  87. "see if row is there.");
  88. }
  89. }
  90. }