MsSqlDataReaderTest.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. //
  2. // MsSqlDataReaderTest.cs :- A class derived from the 'BaseRetrieve' class
  3. // - Contains code specific to ms sql database
  4. //
  5. // Author:
  6. // Satya Sudha K ([email protected])
  7. //
  8. //
  9. // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
  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.Xml.XPath;
  32. using System.Data;
  33. using System.Data.SqlClient;
  34. using System.Text.RegularExpressions;
  35. namespace MonoTests.System.Data {
  36. public class MsSqlRetrieve : BaseRetrieve {
  37. public MsSqlRetrieve (string database) : base (database)
  38. {
  39. }
  40. // returns a Open connection
  41. public override void GetConnection ()
  42. {
  43. string connectionString = null;
  44. try {
  45. connectionString = ConfigClass.GetElement (configDoc, "database", "connectionString");
  46. } catch (XPathException e) {
  47. Console.WriteLine ("Error reading the config file!!!");
  48. Console.WriteLine (e.Message);
  49. con = null;
  50. return;
  51. }
  52. con = new SqlConnection (connectionString);
  53. try {
  54. con.Open ();
  55. } catch (SqlException e) {
  56. Console.WriteLine ("Cannot establish connection with the database " + e);
  57. Console.WriteLine ("Probably the database is down");
  58. con = null;
  59. } catch (InvalidOperationException e) {
  60. Console.WriteLine ("Cannot open connection ");
  61. Console.WriteLine ("Probably the connection already open");
  62. con = null;
  63. } catch (Exception e) {
  64. Console.WriteLine ("Cannot open connection ");
  65. con = null;
  66. }
  67. }
  68. public override object ConvertToBoolean (Type type, string value, ref string errorMsg)
  69. {
  70. short boolValue;
  71. Boolean boolval;
  72. try {
  73. boolValue = Convert.ToInt16 (value);
  74. boolval = Convert.ToBoolean (boolValue);
  75. } catch (Exception e) {
  76. errorMsg = "ERROR : " + e.Message;
  77. errorMsg += "\nSTACKTRACE : " + e.StackTrace;
  78. return false;
  79. }
  80. return boolval;
  81. }
  82. public override object ConvertToDecimal (Type type, string value, ref string errorMsg)
  83. {
  84. Decimal decVal ;
  85. try {
  86. decVal = Convert.ToDecimal (value);
  87. } catch (FormatException e) {
  88. // This may be bcoz value is of the form 'd.ddEdd'
  89. Double doubleVal = Convert.ToDouble (value);
  90. decVal = Convert.ToDecimal (doubleVal);
  91. }
  92. return decVal;
  93. }
  94. public override object ConvertToDateTime (Type type, string value, ref string errorMsg)
  95. {
  96. Regex re = new Regex ("\\b(?<month>\\d{1,2})/(?<day>\\d{1,2})/(?<year>\\d{2,4})\\s+(?<hour>\\d{1,2}):(?<min>\\d{1,2})(:(?<sec>\\d{1,2})(\\.(?<msec>\\d{1,3}))*)*");
  97. value = value.Trim ('\'');
  98. Match m = re.Match (value);
  99. int month, day, year, hour, min, sec, msec;
  100. month = day = year = hour = min = sec = msec = 0;
  101. month = Convert.ToInt32 (m.Result ("${month}"));
  102. day = Convert.ToInt32 (m.Result ("${day}"));
  103. year = Convert.ToInt32 (m.Result ("${year}"));
  104. string str = m.Result ("${hour}");
  105. if (!str.Equals (""))
  106. hour = Convert.ToInt32 (str);
  107. str = m.Result ("${min}");
  108. if (!str.Equals (""))
  109. min = Convert.ToInt32 (str);
  110. str = m.Result ("${sec}");
  111. if (!str.Equals (""))
  112. sec = Convert.ToInt32 (str);
  113. str = m.Result ("${msec}");
  114. if (!str.Equals (""))
  115. msec = Convert.ToInt32 (str);
  116. DateTime dateObj;
  117. try {
  118. if (hour == 0 && min == 0 && sec == 0)
  119. dateObj = new DateTime (year, month, day);
  120. else {
  121. if (msec != 0)
  122. dateObj = new DateTime (year, month, day, hour, min, sec, msec);
  123. else
  124. dateObj = new DateTime (year, month, day, hour, min, sec);
  125. }
  126. } catch (Exception e) {
  127. errorMsg = "Invalid DateTime Value\n";
  128. errorMsg += "ERROR : " + e.Message;
  129. errorMsg += "\nSTACKTRACE : " + e.StackTrace;
  130. return null;
  131. }
  132. return dateObj;
  133. }
  134. }
  135. }