SqlCommand_Parameters.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. using System;
  2. using System.Data;
  3. using System.Data.OleDb;
  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 SqlCommand_Parameters : ADONetTesterClass
  11. {
  12. Exception exp;
  13. public static void Main()
  14. {
  15. SqlCommand_Parameters tc = new SqlCommand_Parameters();
  16. tc.exp = null;
  17. try
  18. {
  19. tc.BeginTest("SqlCommand_Parameters");
  20. tc.run();
  21. }
  22. catch(Exception ex)
  23. {
  24. tc.exp = ex;
  25. }
  26. finally
  27. {
  28. tc.EndTest(tc.exp);
  29. }
  30. }
  31. [Test]
  32. public void run()
  33. {
  34. // testing only SQLServerr
  35. if (ConnectedDataProvider.GetDbType() != DataBaseServer.SQLServer)
  36. {
  37. Log("This test is relevant only for MSSQLServer!");
  38. return;
  39. }
  40. CommandParameterTreatBitAsBoolean();
  41. DoTestparametersBindByNameOnMSSQLServer();
  42. }
  43. //Bug 2814 - MSSQL - Command.Parameters treat bit as Boolean ----
  44. public void CommandParameterTreatBitAsBoolean()
  45. {
  46. exp=null;
  47. SqlConnection con = new SqlConnection(ConnectedDataProvider.ConnectionStringSQLClient);
  48. try
  49. {
  50. BeginCase("Bug 2814 - MSSQL - Command.Parameters treat bit as Boolean");
  51. SqlCommand cmd = new SqlCommand("SELECT * FROM Products where ProductID = @ProductID AND Discontinued = @Discontinued",con);
  52. cmd.Connection = con;
  53. con.Open();
  54. cmd.CommandType = CommandType.Text;
  55. cmd.Parameters.Add( new SqlParameter("@ProductID", SqlDbType.Int, 4));
  56. cmd.Parameters.Add( new SqlParameter("@Discontinued", SqlDbType.Int, 4));
  57. cmd.Parameters["@ProductID"].Value = 5;
  58. cmd.Parameters["@Discontinued"].Value = 1;
  59. SqlDataReader dr = cmd.ExecuteReader();
  60. if (dr.HasRows)
  61. {
  62. dr.Read();
  63. Compare(dr.GetValue(0).ToString(),"5");
  64. }
  65. else
  66. {
  67. Fail("HasRows is not 0.");
  68. }
  69. }
  70. catch(Exception ex)
  71. {
  72. exp = ex;
  73. }
  74. finally
  75. {
  76. if (con.State == ConnectionState.Open)
  77. {
  78. con.Close();
  79. }
  80. EndCase(exp);
  81. exp = null;
  82. }
  83. }
  84. /// <summary>
  85. /// Binding parameters in MSSQLServer should be done by parameter name, regardless of their order.
  86. /// </summary>
  87. public void DoTestparametersBindByNameOnMSSQLServer()
  88. {
  89. SqlConnection conn = new SqlConnection(ConnectedDataProvider.ConnectionStringSQLClient);
  90. SqlDataReader rdr;
  91. try
  92. {
  93. BeginCase("Insert parameters of the same types in different order.");
  94. SqlCommand cmd = new SqlCommand();
  95. conn.Open();
  96. cmd.Connection = conn;
  97. cmd.CommandText = "SalesByCategory";
  98. cmd.CommandType = CommandType.StoredProcedure;
  99. //Stored procedure is declared as "SalesByCategory @CategoryName nvarchar(15), @OrdYear nvarchar(4) = '1998'"
  100. //The test declares them in reverse order.
  101. cmd.Parameters.Add("@OrdYear", "1996");
  102. cmd.Parameters.Add("@CategoryName", "Beverages");
  103. rdr = cmd.ExecuteReader();
  104. int actualAffectedRows = 0;
  105. while (rdr.Read())
  106. {
  107. actualAffectedRows++;
  108. }
  109. Compare(actualAffectedRows, 12);
  110. }
  111. catch (Exception ex)
  112. {
  113. exp = ex;
  114. }
  115. finally
  116. {
  117. EndCase(exp);
  118. exp = null;
  119. if (conn.State != ConnectionState.Closed)
  120. {
  121. conn.Close();
  122. }
  123. }
  124. }
  125. }
  126. }