OleDbCommand_Parameters.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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 MonoTests.System.Data.Utils.Data;
  28. using NUnit.Framework;
  29. namespace MonoTests.System.Data.OleDb
  30. {
  31. [TestFixture]
  32. public class OleDbCommand_Parameters : ADONetTesterClass
  33. {
  34. //Used to test GUID.
  35. private const string TEST_GUID_STRING = "239A3C5E-8D41-11D1-B675-00C04FA3C554";
  36. private Exception exp;
  37. public static void Main()
  38. {
  39. OleDbCommand_Parameters tc = new OleDbCommand_Parameters();
  40. Exception exp = null;
  41. try
  42. {
  43. tc.BeginTest("OleDbCommand_Parameters");
  44. tc.run();
  45. }
  46. catch(Exception ex)
  47. {
  48. exp = ex;
  49. }
  50. finally
  51. {
  52. tc.EndTest(exp);
  53. }
  54. }
  55. [Test]
  56. public void run()
  57. {
  58. #region Simple Tests
  59. //string str="";
  60. string sql;
  61. OleDbConnection con = null;
  62. sql = "UPDATE Employees SET Region = ?, TitleOfCourtesy = ? WHERE EmployeeID=1";
  63. con = new OleDbConnection(MonoTests.System.Data.Utils.ConnectedDataProvider.ConnectionString);
  64. //not testing with DB2 provider
  65. if (ConnectedDataProvider.GetDbType(con) != DataBaseServer.DB2)
  66. {
  67. OleDbCommand cmd = new OleDbCommand(sql, con);
  68. cmd.Parameters.Add(new OleDbParameter("Region", OleDbType.VarWChar));
  69. cmd.Parameters.Add(new OleDbParameter("TitleOfCourtesy", OleDbType.VarWChar));
  70. con.Open();
  71. cmd.Parameters["Region"].Value = "WA";
  72. cmd.Parameters["TitleOfCourtesy"].Value = "Mr";
  73. //return the number of rows affected
  74. int i = cmd.ExecuteNonQuery();
  75. try
  76. {
  77. BeginCase("Check row count");
  78. Compare(i, 1);
  79. }
  80. catch(Exception ex){exp = ex;}
  81. finally{EndCase(exp); exp = null;}
  82. }
  83. if (con.State == ConnectionState.Open) con.Close();
  84. #endregion
  85. #region Test Parameter Types
  86. #region General
  87. TypesSubTests(ConnectedDataProvider.GetSimpleDbTypesParameters());
  88. TypesSubTests(ConnectedDataProvider.GetExtendedDbTypesParameters());
  89. #endregion
  90. #endregion
  91. }
  92. private void TypesSubTests(DbTypeParametersCollection typesToTest)
  93. {
  94. DbTypeParametersCollection currentlyTested = new DbTypeParametersCollection(typesToTest.TableName);
  95. int affectedRows;
  96. string rowId = string.Empty;
  97. foreach (DbTypeParameter currentParamType in typesToTest)
  98. {
  99. try
  100. {
  101. BeginCase("Test INSERT with parameter of type: " + currentParamType.DbTypeName);
  102. rowId = string.Format("13282_{0}", this.TestCaseNumber);
  103. currentlyTested.Clear();
  104. currentlyTested.Add(currentParamType);
  105. affectedRows = currentlyTested.ExecuteInsert(rowId);
  106. Compare(affectedRows, 1);
  107. }
  108. catch(Exception ex)
  109. {
  110. exp = ex;
  111. }
  112. finally
  113. {
  114. EndCase(exp);
  115. exp = null;
  116. currentlyTested.ExecuteDelete(rowId);
  117. }
  118. }
  119. }
  120. //Test insertion of a GUID parameter on MSSQLServer.
  121. [Test]
  122. public void DoTestGUIDOnMSSQLServer()
  123. {
  124. if (ConnectedDataProvider.GetDbType() != DataBaseServer.SQLServer)
  125. return;
  126. DbTypeParametersCollection guidRow = new DbTypeParametersCollection(ConnectedDataProvider.SPECIFIC_TYPES_TABLE_NAME);
  127. guidRow.Add("UNIQUEIDENTIFIER", new Guid(TEST_GUID_STRING));
  128. TypesSubTests(guidRow);
  129. }
  130. //Test problems specific to the TIME type on DB2.
  131. [Test]
  132. public void DoTestTimeOnDB2()
  133. {
  134. if (ConnectedDataProvider.GetDbType() != DataBaseServer.DB2)
  135. return;
  136. OleDbConnection conn = new OleDbConnection(ConnectedDataProvider.ConnectionString);
  137. string rowId = string .Empty;
  138. try
  139. {
  140. BeginCase("Test INSERT to TIME column using only TimeOfDate part of DateTime");
  141. rowId = "13282_" + this.TestCaseNumber.ToString();
  142. OleDbCommand cmd = new OleDbCommand();
  143. conn.Open();
  144. cmd.Connection = conn;
  145. cmd.CommandText = string.Format("INSERT INTO {0} (ID, T_TIME) VALUES ('{1}', ?)",ConnectedDataProvider.EXTENDED_TYPES_TABLE_NAME ,rowId);
  146. cmd.Parameters.Add("time", DateTime.Now.TimeOfDay);
  147. int affectedRows;
  148. affectedRows = cmd.ExecuteNonQuery();
  149. Compare(affectedRows, 1);
  150. }
  151. catch (Exception ex)
  152. {
  153. exp = ex;
  154. }
  155. finally
  156. {
  157. EndCase(exp);
  158. exp = null;
  159. DbTypeParametersCollection.ExecuteDelete(ConnectedDataProvider.EXTENDED_TYPES_TABLE_NAME, rowId);
  160. conn.Close();
  161. }
  162. }
  163. [Test]
  164. #if JAVA
  165. [Category("NotWorking")]
  166. #endif
  167. public void DoTestTimeOnDB2_bug3391()
  168. {
  169. if (ConnectedDataProvider.GetDbType() != DataBaseServer.DB2)
  170. return;
  171. OleDbConnection conn = new OleDbConnection(ConnectedDataProvider.ConnectionString);
  172. string rowId = string .Empty;
  173. try {
  174. BeginCase("Test INSERT to TIME column using all of the DateTime");
  175. rowId = "13282_" + this.TestCaseNumber.ToString();
  176. OleDbCommand cmd = new OleDbCommand();
  177. conn.Open();
  178. cmd.Connection = conn;
  179. cmd.CommandText = string.Format("INSERT INTO {0} (ID, T_TIME) VALUES ('{1}', ?)",ConnectedDataProvider.EXTENDED_TYPES_TABLE_NAME ,rowId);
  180. cmd.Parameters.Add("time", DateTime.Now);
  181. try {
  182. cmd.ExecuteNonQuery();
  183. ExpectedExceptionNotCaught("System.OleDbException");
  184. }
  185. catch (OleDbException ex) {
  186. ExpectedExceptionCaught(ex);
  187. }
  188. }
  189. catch (Exception ex) {
  190. exp = ex;
  191. }
  192. finally {
  193. EndCase(exp);
  194. exp = null;
  195. DbTypeParametersCollection.ExecuteDelete(ConnectedDataProvider.EXTENDED_TYPES_TABLE_NAME, rowId);
  196. conn.Close();
  197. }
  198. }
  199. }
  200. }