SqlParameter_Direction.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. using System;
  2. using System.Text;
  3. using System.Data;
  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 SqlParameter_Direction : GHTBase
  11. {
  12. private Exception exp;
  13. public static void Main()
  14. {
  15. SqlParameter_Direction tc = new SqlParameter_Direction();
  16. tc.exp = null;
  17. tc.TestSetup();
  18. try
  19. {
  20. // Every Test must begin with BeginTest
  21. tc.BeginTest("SqlParameter_Direction");
  22. tc.run();
  23. }
  24. catch(Exception ex)
  25. {
  26. tc.exp = ex;
  27. }
  28. finally
  29. {
  30. tc.EndTest(tc.exp);
  31. tc.TestTearDown();
  32. }
  33. }
  34. public void run()
  35. {
  36. TestBug4703();
  37. }
  38. [TestFixtureSetUp]
  39. public void TestSetup()
  40. {
  41. if (ConnectedDataProvider.GetDbType() != DataBaseServer.SQLServer) {
  42. //All tests in this class are only for MSSQLServer.
  43. Log(string.Format("All tests in this class are only for MSSQLServer and cannot be tested on {0}", ConnectedDataProvider.GetDbType()));
  44. return;
  45. }
  46. BeginCase("Test Setup");
  47. SqlConnection con = new SqlConnection(ConnectedDataProvider.ConnectionStringSQLClient);
  48. StringBuilder createTestSpBuilder = new StringBuilder();
  49. createTestSpBuilder.Append("CREATE PROCEDURE dbo.GHSP_DateTimeOutputTest");
  50. createTestSpBuilder.Append("(");
  51. createTestSpBuilder.Append(" @LastRefresh datetime OUTPUT");
  52. createTestSpBuilder.Append(")");
  53. createTestSpBuilder.Append("AS ");
  54. createTestSpBuilder.Append("SET @LastRefresh = GETDATE() ");
  55. createTestSpBuilder.Append("RETURN");
  56. SqlCommand createTestSpCmd = null;
  57. try
  58. {
  59. createTestSpCmd = new SqlCommand(createTestSpBuilder.ToString(), con);
  60. con.Open();
  61. createTestSpCmd.ExecuteNonQuery();
  62. Pass("Test setup completed successfuly.");
  63. }
  64. catch (Exception ex)
  65. {
  66. Fail("Test setup failed");
  67. exp = ex;
  68. }
  69. finally
  70. {
  71. EndCase(exp);
  72. if (con != null && con.State != ConnectionState.Closed)
  73. {
  74. con.Close();
  75. }
  76. }
  77. }
  78. [TestFixtureTearDown()]
  79. public void TestTearDown()
  80. {
  81. if (ConnectedDataProvider.GetDbType() != DataBaseServer.SQLServer) {
  82. //All tests in this class are only for MSSQLServer.
  83. Log(string.Format("All tests in this class are only for MSSQLServer and cannot be tested on {0}", ConnectedDataProvider.GetDbType()));
  84. return;
  85. }
  86. BeginCase("Test Teardown");
  87. SqlConnection con = new SqlConnection(ConnectedDataProvider.ConnectionStringSQLClient);
  88. StringBuilder createTestSpBuilder = new StringBuilder();
  89. string dropTestSpSql = "DROP PROCEDURE dbo.GHSP_DateTimeOutputTest";
  90. SqlCommand dropTestSpCmd = null;
  91. try
  92. {
  93. dropTestSpCmd = new SqlCommand(dropTestSpSql, con);
  94. con.Open();
  95. dropTestSpCmd.ExecuteNonQuery();
  96. Pass("Test teardown completed successfuly.");
  97. }
  98. catch (Exception ex)
  99. {
  100. Fail("Test teardown failed");
  101. exp = ex;
  102. }
  103. finally
  104. {
  105. EndCase(exp);
  106. if (con != null && con.State != ConnectionState.Closed)
  107. {
  108. con.Close();
  109. }
  110. }
  111. }
  112. [Test]
  113. public void TestBug4703()
  114. {
  115. if (ConnectedDataProvider.GetDbType() != DataBaseServer.SQLServer) {
  116. //All tests in this class are only for MSSQLServer.
  117. Log(string.Format("All tests in this class are only for MSSQLServer and cannot be tested on {0}", ConnectedDataProvider.GetDbType()));
  118. return;
  119. }
  120. try
  121. {
  122. BeginCase("Test Bug 4703 - DateTime output parameter of stored procedure contains incorrect time ( always 12:00 AM )");
  123. string strConnection = ConnectedDataProvider.ConnectionStringSQLClient;
  124. SqlConnection conn = new SqlConnection(strConnection);
  125. conn.Open();
  126. SqlCommand command = conn.CreateCommand();
  127. SqlParameter param = null;
  128. command.CommandType = CommandType.StoredProcedure;
  129. command.CommandText = "GHSP_DateTimeOutputTest";
  130. param = command.CreateParameter();
  131. param.ParameterName="@LastRefresh";
  132. param.DbType = DbType.DateTime;
  133. param.Direction = ParameterDirection.InputOutput;
  134. DateTime testValue = DateTime.Now;
  135. param.Value = testValue;
  136. command.Parameters.Add( param );
  137. Compare(param.Value, testValue);
  138. }
  139. catch(Exception ex)
  140. {
  141. exp = ex;
  142. }
  143. finally
  144. {
  145. EndCase(exp);
  146. exp = null;
  147. }
  148. }
  149. }
  150. }