SqlDataAdapter_RowUpdating.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. using System;
  2. using System.Data;
  3. using System.Data.Common;
  4. using System.Data.SqlClient;
  5. using MonoTests.System.Data.Utils;
  6. using NUnit.Framework;
  7. namespace MonoTests.System.Data.SqlClient
  8. {
  9. [TestFixture]
  10. public class SqlDataAdapter_RowUpdating : ADONetTesterClass
  11. {
  12. public static void Main()
  13. {
  14. SqlDataAdapter_RowUpdating tc = new SqlDataAdapter_RowUpdating();
  15. Exception exp = null;
  16. try
  17. {
  18. tc.BeginTest("SqlDataAdapter_RowUpdating");
  19. tc.run();
  20. }
  21. catch(Exception ex)
  22. {
  23. exp = ex;
  24. }
  25. finally
  26. {
  27. tc.EndTest(exp);
  28. }
  29. }
  30. //public TestClass():base(true){}
  31. //Activate this constructor to log Failures to a log file
  32. //public TestClass(System.IO.TextWriter tw):base(tw, false){}
  33. //Activate this constructor to log All to a log file
  34. //public TestClass(System.IO.TextWriter tw):base(tw, true){}
  35. //BY DEFAULT LOGGING IS DONE TO THE STANDARD OUTPUT ONLY FOR FAILURES
  36. int EventCounter = 0;
  37. DataRow drInsert,drDelete,drUpdate;
  38. [Test]
  39. public void run()
  40. {
  41. if (ConnectedDataProvider.GetDbType() != DataBaseServer.SQLServer)
  42. {
  43. Log("Test \"SqlDataAdapter_RowUpdated\" skipped: [Test applies only to sql server]");
  44. return;
  45. }
  46. Exception exp = null;
  47. SqlDataAdapter sqlDa = new SqlDataAdapter();
  48. SqlConnection con = new SqlConnection(ConnectedDataProvider.ConnectionStringSQLClient);
  49. sqlDa.SelectCommand = new SqlCommand("",con);
  50. base.SqlDataAdapter_BuildUpdateCommands(ref sqlDa);
  51. // --------- get data from DB -----------------
  52. DataSet ds = base.PrepareDBData_Update((DbDataAdapter)sqlDa,true);
  53. // add event handler
  54. sqlDa.RowUpdating+=new SqlRowUpdatingEventHandler(sqlDa_RowUpdating);
  55. //insert ,delete, update
  56. drInsert = ds.Tables[0].NewRow();
  57. drInsert.ItemArray = new object[] {9991,"Ofer","Borshtein","Insert"};
  58. drDelete = ds.Tables[0].Rows.Find(9992);
  59. drUpdate = ds.Tables[0].Rows.Find(9993);
  60. ds.Tables[0].Rows.Add(drInsert);
  61. drDelete.Delete();
  62. drUpdate["Title"] = "Jack the ripper";
  63. //execute update to db, will raise events
  64. sqlDa.Update(ds);
  65. try
  66. {
  67. BeginCase("EventCounter ");
  68. Compare(EventCounter ,3);
  69. }
  70. catch(Exception ex) {exp = ex;}
  71. finally {EndCase(exp); exp = null;}
  72. sqlDa.RowUpdating-= new SqlRowUpdatingEventHandler(sqlDa_RowUpdating);
  73. //close connection
  74. if ( ((IDbDataAdapter)sqlDa).SelectCommand.Connection.State != ConnectionState.Closed )
  75. ((IDbDataAdapter)sqlDa).SelectCommand.Connection.Close();
  76. }
  77. private void sqlDa_RowUpdating(object sender, SqlRowUpdatingEventArgs e)
  78. {
  79. Exception exp = null;
  80. switch (e.StatementType)
  81. {
  82. case StatementType.Insert:
  83. try
  84. {
  85. BeginCase("RowInsert");
  86. Compare(drInsert ,e.Row );
  87. }
  88. catch(Exception ex) {exp = ex;}
  89. finally {EndCase(exp); exp = null;}
  90. EventCounter++;
  91. break;
  92. case StatementType.Delete:
  93. try
  94. {
  95. BeginCase("RowDelete");
  96. Compare(drDelete ,e.Row );
  97. }
  98. catch(Exception ex) {exp = ex;}
  99. finally {EndCase(exp); exp = null;}
  100. EventCounter++;
  101. break;
  102. case StatementType.Update:
  103. try
  104. {
  105. BeginCase("RowUpdate");
  106. Compare(drUpdate ,e.Row );
  107. }
  108. catch(Exception ex) {exp = ex;}
  109. finally {EndCase(exp); exp = null;}
  110. EventCounter++;
  111. break;
  112. }
  113. }
  114. }
  115. }