MsSqlDbAdapterTest.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. //
  2. // MsSqlDbAdapterTest.cs :- Defines a class 'MsSqlAdapter' derived from the
  3. // 'BaseAdapter' class
  4. // - Contains code specific to ms sql database
  5. //
  6. // Author:
  7. // Satya Sudha K ([email protected])
  8. //
  9. //
  10. // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
  11. //
  12. // Permission is hereby granted, free of charge, to any person obtaining
  13. // a copy of this software and associated documentation files (the
  14. // "Software"), to deal in the Software without restriction, including
  15. // without limitation the rights to use, copy, modify, merge, publish,
  16. // distribute, sublicense, and/or sell copies of the Software, and to
  17. // permit persons to whom the Software is furnished to do so, subject to
  18. // the following conditions:
  19. //
  20. // The above copyright notice and this permission notice shall be
  21. // included in all copies or substantial portions of the Software.
  22. //
  23. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  27. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  28. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  29. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  30. //
  31. using System;
  32. using System.Xml.XPath;
  33. using System.Data;
  34. using System.Data.SqlClient;
  35. using System.Text.RegularExpressions;
  36. namespace MonoTests.System.Data {
  37. public class MsSqlAdapter : BaseAdapter {
  38. public MsSqlAdapter (string database) : base (database)
  39. {
  40. dataAdapter = new SqlDataAdapter ();
  41. }
  42. // returns a Open connection
  43. public override void GetConnection ()
  44. {
  45. string connectionString = null;
  46. try {
  47. connectionString = ConfigClass.GetElement (configDoc, "database", "connectionString");
  48. } catch (XPathException e) {
  49. Console.WriteLine ("Error reading the config file!!!");
  50. Console.WriteLine (e.Message);
  51. con = null;
  52. return;
  53. }
  54. con = new SqlConnection (connectionString);
  55. try {
  56. con.Open ();
  57. } catch (SqlException e) {
  58. Console.WriteLine ("Cannot establish connection with the database");
  59. Console.WriteLine ("Probably the database is down");
  60. con = null;
  61. } catch (InvalidOperationException e) {
  62. Console.WriteLine ("Cannot open connection ");
  63. Console.WriteLine ("Probably the connection already open");
  64. con = null;
  65. } catch (Exception e) {
  66. Console.WriteLine ("Cannot open connection ");
  67. con = null;
  68. }
  69. }
  70. public override object ConvertToBoolean (Type type, string value, ref string errorMsg)
  71. {
  72. short boolValue;
  73. Boolean boolval;
  74. try {
  75. boolValue = Convert.ToInt16 (value);
  76. boolval = Convert.ToBoolean (boolValue);
  77. } catch (Exception e) {
  78. errorMsg = "ERROR : " + e.Message;
  79. errorMsg += "\nSTACKTRACE : " + e.StackTrace;
  80. return false;
  81. }
  82. return boolval;
  83. }
  84. public override object ConvertToDecimal (Type type, string value, ref string errorMsg)
  85. {
  86. Decimal decVal ;
  87. try {
  88. decVal = Convert.ToDecimal (value);
  89. } catch (FormatException e) {
  90. // This may be bcoz value is of the form 'd.ddEdd'
  91. Double doubleVal = Convert.ToDouble (value);
  92. decVal = Convert.ToDecimal (doubleVal);
  93. }
  94. return decVal;
  95. }
  96. public override object ConvertToDateTime (Type type, string value, ref string errorMsg)
  97. {
  98. 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}))*)*");
  99. value = value.Trim ('\'');
  100. Match m = re.Match (value);
  101. int month, day, year, hour, min, sec, msec;
  102. month = day = year = hour = min = sec = msec = 0;
  103. month = Convert.ToInt32 (m.Result ("${month}"));
  104. day = Convert.ToInt32 (m.Result ("${day}"));
  105. year = Convert.ToInt32 (m.Result ("${year}"));
  106. string str = m.Result ("${hour}");
  107. if (!str.Equals (""))
  108. hour = Convert.ToInt32 (str);
  109. str = m.Result ("${min}");
  110. if (!str.Equals (""))
  111. min = Convert.ToInt32 (str);
  112. str = m.Result ("${sec}");
  113. if (!str.Equals (""))
  114. sec = Convert.ToInt32 (str);
  115. str = m.Result ("${msec}");
  116. if (!str.Equals (""))
  117. msec = Convert.ToInt32 (str);
  118. DateTime dateObj;
  119. try {
  120. if (hour == 0 && min == 0 && sec == 0)
  121. dateObj = new DateTime (year, month, day);
  122. else {
  123. if (msec != 0) {
  124. dateObj = new DateTime (year, month, day, hour, min, sec, msec);
  125. } else {
  126. dateObj = new DateTime (year, month, day, hour, min, sec);
  127. }
  128. }
  129. } catch (Exception e) {
  130. errorMsg = "Invalid DateTime Value\n";
  131. errorMsg += "ERROR : " + e.Message;
  132. errorMsg += "\nSTACKTRACE : " + e.StackTrace;
  133. return false;
  134. }
  135. return dateObj;
  136. }
  137. public override void PopulateDataSetFromTable (string queryStr, string tableName)
  138. {
  139. cmd.CommandText = queryStr;
  140. SqlDataAdapter da = (SqlDataAdapter) dataAdapter;
  141. da.SelectCommand = (SqlCommand) cmd;
  142. dataset = new DataSet ();
  143. da.Fill (dataset, tableName);
  144. }
  145. public override bool ReconcileChanges (string tableName, ref string errorMsg)
  146. {
  147. try {
  148. new SqlCommandBuilder ((SqlDataAdapter) dataAdapter);
  149. dataAdapter.Update (dataset, tableName);
  150. } catch (Exception e) {
  151. errorMsg = "ERROR : " + e.Message;
  152. errorMsg += "\nSTACKTRACE : " + e.StackTrace;
  153. return false;
  154. }
  155. return true;
  156. }
  157. }
  158. }