OleDbCommand_Prepare.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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_Prepare : GHTBase
  32. {
  33. public static void Main()
  34. {
  35. OleDbCommand_Prepare tc = new OleDbCommand_Prepare();
  36. Exception exp = null;
  37. try
  38. {
  39. tc.BeginTest("OleDbCommand_Prepare");
  40. tc.run();
  41. }
  42. catch(Exception ex){exp = ex;}
  43. finally {tc.EndTest(exp);}
  44. }
  45. [Test]
  46. public void run()
  47. {
  48. Exception exp = null;
  49. int intRecordsAffected = 0;
  50. string sql = "Update Shippers Set CompanyName=? Where ShipperID = 2";
  51. OleDbConnection con = new OleDbConnection(MonoTests.System.Data.Utils.ConnectedDataProvider.ConnectionString);
  52. OleDbCommand cmd = new OleDbCommand("", con);
  53. con.Open();
  54. //get expected result
  55. cmd.CommandText = "select count(*) from Shippers where ShipperID = 2";
  56. int ExpectedRows = int.Parse(cmd.ExecuteScalar().ToString());
  57. cmd.CommandText = sql;
  58. //Currently not running on DB2: .Net-Failed, GH:Pass
  59. //if (con.Provider.IndexOf("IBMDADB2") >= 0) return ;
  60. cmd.Parameters.Add(new OleDbParameter());
  61. cmd.Parameters[0].ParameterName = "CompName";
  62. cmd.Parameters[0].OleDbType = OleDbType.VarWChar; //System.InvalidOperationException:
  63. cmd.Parameters[0].Size = 20; //System.InvalidOperationException
  64. cmd.Parameters[0].SourceColumn = "CompanyName";
  65. cmd.Parameters[0].Value = "Comp1";
  66. try
  67. {
  68. BeginCase("Prepare Exception - missing OleDbType");
  69. try
  70. {
  71. cmd.Parameters[0].OleDbType = OleDbType.Empty ;
  72. cmd.Prepare();
  73. }
  74. catch (Exception ex) {exp = ex;}
  75. Compare(exp.GetType().FullName, typeof(InvalidOperationException).FullName );
  76. exp=null;
  77. }
  78. catch(Exception ex){exp = ex;}
  79. finally{EndCase(exp); exp = null;}
  80. cmd.Parameters[0].OleDbType = OleDbType.VarWChar;
  81. try
  82. {
  83. BeginCase("Prepare Exception - missing Size");
  84. try
  85. {
  86. cmd.Parameters[0].Size = 0;
  87. cmd.Prepare();
  88. }
  89. catch (Exception ex) {exp = ex;}
  90. Compare(exp.GetType().FullName, typeof(InvalidOperationException).FullName );
  91. exp=null;
  92. }
  93. catch(Exception ex){exp = ex;}
  94. finally{EndCase(exp); exp = null;}
  95. cmd.Parameters[0].Size = 20;
  96. try
  97. {
  98. BeginCase("Prepare Exception - missing Size");
  99. try
  100. {
  101. con.Close();
  102. cmd.Prepare();
  103. }
  104. catch (Exception ex) {exp = ex;}
  105. Compare(exp.GetType().FullName, typeof(InvalidOperationException).FullName );
  106. exp=null;
  107. }
  108. catch(Exception ex){exp = ex;}
  109. finally{EndCase(exp); exp = null;}
  110. con.Open();
  111. try
  112. {
  113. BeginCase("ExecuteNonQuery first time");
  114. intRecordsAffected = cmd.ExecuteNonQuery();
  115. Compare(intRecordsAffected , ExpectedRows);
  116. }
  117. catch(Exception ex){exp = ex;}
  118. finally{EndCase(exp); exp = null;}
  119. try
  120. {
  121. BeginCase("ExecuteNonQuery second time, chage value");
  122. cmd.Parameters[0].Value = "Comp2";
  123. intRecordsAffected = cmd.ExecuteNonQuery();
  124. Compare(intRecordsAffected , ExpectedRows);
  125. }
  126. catch(Exception ex){exp = ex;}
  127. finally{EndCase(exp); exp = null;}
  128. if (con.State == ConnectionState.Open) con.Close();
  129. }
  130. }
  131. }