OleDbCommand_ExecuteNonQuery.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. //
  2. // Copyright (c) 2006 Mainsoft Co.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining
  5. // a copy of this software and associated documentation files (the
  6. // "Software"), to deal in the Software without restriction, including
  7. // without limitation the rights to use, copy, modify, merge, publish,
  8. // distribute, sublicense, and/or sell copies of the Software, and to
  9. // permit persons to whom the Software is furnished to do so, subject to
  10. // the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be
  13. // included in all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  16. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  17. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  18. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  19. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  20. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  21. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  22. //
  23. using System;
  24. using System.Data;
  25. using System.Data.OleDb ;
  26. using MonoTests.System.Data.Utils;
  27. using NUnit.Framework;
  28. namespace MonoTests.System.Data.OleDb
  29. {
  30. [TestFixture]
  31. public class OleDbCommand_ExecuteNonQuery : GHTBase
  32. {
  33. OleDbConnection con;
  34. OleDbCommand cmd;
  35. [SetUp]
  36. public void SetUp()
  37. {
  38. Exception exp = null;
  39. BeginCase("Setup");
  40. try
  41. {
  42. //prepare Data
  43. OleDbCommand cmdPrepare = new OleDbCommand("", new OleDbConnection(MonoTests.System.Data.Utils.ConnectedDataProvider.ConnectionString));
  44. cmdPrepare.Connection.Open();
  45. cmdPrepare.CommandText = "DELETE FROM Employees WHERE EmployeeID = 99999";
  46. cmdPrepare.ExecuteScalar();
  47. cmdPrepare.Connection.Close();
  48. cmdPrepare.Dispose();
  49. con = new OleDbConnection(MonoTests.System.Data.Utils.ConnectedDataProvider.ConnectionString);
  50. cmd = new OleDbCommand("", con);
  51. con.Open();
  52. }
  53. catch(Exception ex){exp = ex;}
  54. finally{EndCase(exp); exp = null;}
  55. }
  56. [TearDown]
  57. public void TearDown()
  58. {
  59. if (con != null)
  60. {
  61. if (con.State == ConnectionState.Open) con.Close();
  62. }
  63. }
  64. public static void Main()
  65. {
  66. OleDbCommand_ExecuteNonQuery tc = new OleDbCommand_ExecuteNonQuery();
  67. Exception exp = null;
  68. try
  69. {
  70. tc.BeginTest("OleDbCommand_ExecuteNonQuery");
  71. tc.SetUp();
  72. tc.run();
  73. tc.TearDown();
  74. }
  75. catch(Exception ex){exp = ex;}
  76. finally {tc.EndTest(exp);}
  77. }
  78. [Test]
  79. public void run()
  80. {
  81. Exception exp = null;
  82. int intRecordsAffected = 0;
  83. try
  84. {
  85. BeginCase("Execute Insert");
  86. cmd.CommandText = "INSERT INTO Employees (EmployeeID,FirstName, LastName) VALUES (99999,'OferXYZ', 'Testing')";
  87. intRecordsAffected = cmd.ExecuteNonQuery();
  88. Compare(intRecordsAffected, 1);
  89. }
  90. catch(Exception ex){exp = ex;}
  91. finally{EndCase(exp); exp = null;}
  92. try
  93. {
  94. BeginCase("Check insert operation");
  95. cmd.CommandText = "SELECT FirstName FROM Employees WHERE (EmployeeID = 99999)";
  96. string strFirstName = cmd.ExecuteScalar().ToString();
  97. Compare(strFirstName, "OferXYZ");
  98. }
  99. catch(Exception ex){exp = ex;}
  100. finally{EndCase(exp); exp = null;}
  101. try
  102. {
  103. BeginCase("Execute Select");
  104. cmd.CommandText = "SELECT EmployeeID FROM Employees WHERE (EmployeeID = 99999)";
  105. intRecordsAffected = cmd.ExecuteNonQuery();
  106. switch (ConnectedDataProvider.GetDbType())
  107. {
  108. case DataBaseServer.PostgreSQL:
  109. // postgres odbc returns 1
  110. #if !JAVA
  111. {
  112. Compare(intRecordsAffected, 1);
  113. }
  114. #else
  115. {
  116. Compare(intRecordsAffected, -1);
  117. }
  118. #endif
  119. break;
  120. default:
  121. Compare(intRecordsAffected, -1);
  122. break;
  123. }
  124. }
  125. catch(Exception ex){exp = ex;}
  126. finally{EndCase(exp); exp = null;}
  127. try
  128. {
  129. BeginCase("Execute UPDATE");
  130. cmd.CommandText = "UPDATE Employees SET FirstName = 'OferABC', LastName = 'TestXYZ' WHERE (EmployeeID = 99999)";
  131. intRecordsAffected = cmd.ExecuteNonQuery();
  132. Compare(intRecordsAffected, 1);
  133. }
  134. catch(Exception ex){exp = ex;}
  135. finally{EndCase(exp); exp = null;}
  136. try
  137. {
  138. BeginCase("Check Update operation");
  139. cmd.CommandText = "SELECT FirstName FROM Employees WHERE (EmployeeID = 99999)";
  140. string strFirstName = cmd.ExecuteScalar().ToString();
  141. Compare(strFirstName, "OferABC");
  142. }
  143. catch(Exception ex){exp = ex;}
  144. finally{EndCase(exp); exp = null;}
  145. try
  146. {
  147. BeginCase("Execute UPDATE");
  148. cmd.CommandText = "DELETE FROM Employees WHERE (EmployeeID = 99999)";
  149. intRecordsAffected = cmd.ExecuteNonQuery();
  150. Compare(intRecordsAffected, 1);
  151. }
  152. catch(Exception ex){exp = ex;}
  153. finally{EndCase(exp); exp = null;}
  154. try
  155. {
  156. BeginCase("Check Delete operation");
  157. cmd.CommandText = "SELECT FirstName FROM Employees WHERE (EmployeeID = 99999)";
  158. object obj = cmd.ExecuteScalar();
  159. Compare(obj==null, true);
  160. }
  161. catch(Exception ex){exp = ex;}
  162. finally{EndCase(exp); exp = null;}
  163. try
  164. {
  165. BeginCase("Check OleDBException - update with bad value");
  166. cmd.CommandText = "UPDATE Employees SET BirthDate = 'bad value' WHERE (EmployeeID = 1)";
  167. try
  168. {
  169. cmd.ExecuteNonQuery();
  170. }
  171. catch (OleDbException ex)
  172. {
  173. exp = ex;
  174. }
  175. Compare(exp.GetType().FullName, typeof(OleDbException).FullName);
  176. exp=null;
  177. }
  178. catch(Exception ex){exp = ex;}
  179. finally{EndCase(exp); exp = null;}
  180. try
  181. {
  182. BeginCase("Check OleDBException - missing EmployeeID");
  183. cmd.CommandText = "INSERT INTO Employees (FirstName, BirthDate) VALUES ('Dado', 'Ben David')";
  184. try
  185. {
  186. cmd.ExecuteNonQuery();
  187. }
  188. catch (OleDbException ex)
  189. {
  190. exp = ex;
  191. }
  192. Compare(exp.GetType().FullName, typeof(OleDbException).FullName);
  193. exp=null;
  194. }
  195. catch(Exception ex){exp = ex;}
  196. finally{EndCase(exp); exp = null;}
  197. }
  198. }
  199. }