SqlDataReaderTest.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. //
  2. // SqlDataReaderTest.cs - NUnit Test Cases for testing the
  3. // SqlDataReader class
  4. // Author:
  5. // Umadevi S ([email protected])
  6. // Kornél Pál <http://www.kornelpal.hu/>
  7. //
  8. // Copyright (c) 2004 Novell Inc., and the individuals listed
  9. // on the ChangeLog entries.
  10. //
  11. // Permission is hereby granted, free of charge, to any person obtaining
  12. // a copy of this software and associated documentation files (the
  13. // "Software"), to deal in the Software without restriction, including
  14. // without limitation the rights to use, copy, modify, merge, publish,
  15. // distribute, sublicense, and/or sell copies of the Software, and to
  16. // permit persons to whom the Software is furnished to do so, subject to
  17. // the following conditions:
  18. //
  19. // The above copyright notice and this permission notice shall be
  20. // included in all copies or substantial portions of the Software.
  21. //
  22. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  23. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  24. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  25. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  26. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  27. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  28. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  29. //
  30. using System;
  31. using System.Data;
  32. using System.Data.Common;
  33. using System.Data.SqlClient;
  34. using NUnit.Framework;
  35. namespace MonoTests.System.Data.SqlClient
  36. {
  37. [TestFixture]
  38. public class SqlDataReaderTest : MSSqlTestClient {
  39. [SetUp]
  40. public void GetReady () {
  41. OpenConnection ();
  42. }
  43. [TearDown]
  44. public void Clean () {
  45. CloseConnection ();
  46. }
  47. [Test]
  48. public void ReadEmptyNTextFieldTest () {
  49. try {
  50. using (conn) {
  51. SqlCommand sqlCommand = conn.CreateCommand();
  52. sqlCommand.CommandText = "CREATE TABLE #MonoTest (NAME ntext)";
  53. sqlCommand.ExecuteNonQuery();
  54. sqlCommand.CommandText = "INSERT INTO #MonoTest VALUES ('')"; //('')";
  55. sqlCommand.ExecuteNonQuery();
  56. sqlCommand.CommandText = "SELECT * FROM #MonoTest";
  57. SqlDataReader dr = sqlCommand.ExecuteReader();
  58. while (dr.Read()) {
  59. Console.WriteLine(dr["NAME"].GetType().FullName);
  60. Assert.AreEqual("System.String",dr["NAME"].GetType().FullName);
  61. }
  62. }
  63. }
  64. catch (Exception e) {
  65. Assert.Fail("A#01 Got an exception");
  66. //Console.WriteLine(e.StackTrace);
  67. }
  68. finally { // try/catch is necessary to gracefully close connections^M
  69. CloseConnection ();
  70. }
  71. }
  72. [Test]
  73. public void ReadBingIntTest()
  74. {
  75. try
  76. {
  77. string createquery = "SELECT CAST(548967465189498 AS bigint) AS Value";
  78. SqlCommand cmd = new SqlCommand();
  79. cmd.Connection = conn;
  80. cmd.CommandText = createquery;
  81. SqlDataReader r = cmd.ExecuteReader();
  82. using (r)
  83. {
  84. if (r.Read())
  85. {
  86. long id = 0;
  87. try
  88. {
  89. id = r.GetInt64(0);
  90. }
  91. catch (Exception e)
  92. {
  93. Assert.Fail("A#01 Got an exception in GetInt64");
  94. }
  95. Assert.AreEqual(548967465189498, id);
  96. try
  97. {
  98. id = r.GetSqlInt64(0).Value;
  99. }
  100. catch (Exception e)
  101. {
  102. Assert.Fail("A#02 Got an exception in GetSqlInt64");
  103. }
  104. Assert.AreEqual(548967465189498, id);
  105. }
  106. else
  107. Assert.Fail("A#03 No rows returned");
  108. }
  109. }
  110. catch (Exception e)
  111. {
  112. Assert.Fail("A#04 Got an exception");
  113. //Console.WriteLine(e.StackTrace);
  114. }
  115. finally
  116. {
  117. // try/catch is necessary to gracefully close connections
  118. CloseConnection();
  119. }
  120. }
  121. }
  122. }