DBHelper.cs 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. // DBHelper.cs : Helper class for executing queries with database.
  2. //
  3. // Authors:
  4. // Sureshkumar T ([email protected])
  5. //
  6. // Copyright (c) 2004 Novell Inc., and the individuals listed on the
  7. // ChangeLog entries.
  8. //
  9. //
  10. // Permission is hereby granted, free of charge, to any person
  11. // obtaining a copy of this software and associated documentation
  12. // files (the "Software"), to deal in the Software without
  13. // restriction, including without limitation the rights to use, copy,
  14. // modify, merge, publish, distribute, sublicense, and/or sell copies
  15. // of the Software, and to permit persons to whom the Software is
  16. // furnished to do so, subject to the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be
  19. // included in all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  25. // BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  26. // ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  27. // CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  28. // SOFTWARE.
  29. using System;
  30. using System.Data;
  31. using System.Text;
  32. using System.Data.Common;
  33. using System.Data.Odbc;
  34. using Mono.Data;
  35. using NUnit.Framework;
  36. namespace MonoTests.System.Data
  37. {
  38. public sealed class DBHelper
  39. {
  40. public static Random random = new Random ( (int) DateTime.Now.Ticks);
  41. public static int ExecuteNonQuery (IDbConnection connection ,string query)
  42. {
  43. IDbCommand command = connection.CreateCommand ();
  44. command.CommandType = CommandType.Text;
  45. command.CommandText = query;
  46. int result = -1;
  47. try {
  48. result = command.ExecuteNonQuery ();
  49. } catch (Exception e) {
  50. return -2;
  51. }
  52. return result;
  53. }
  54. public static int ExecuteSimpleSP (IDbConnection connection ,string proc)
  55. {
  56. IDbCommand command = connection.CreateCommand ();
  57. command.CommandType = CommandType.StoredProcedure;
  58. command.CommandText = proc;
  59. int result = -1;
  60. try {
  61. result = command.ExecuteNonQuery ();
  62. } catch (Exception e) {
  63. return -2;
  64. }
  65. return result;
  66. }
  67. public static string GetRandomName (string prefix, int length)
  68. {
  69. StringBuilder s = new StringBuilder (prefix.Length + 1 + length);
  70. s.Append (prefix);
  71. s.Append ("_");
  72. for (int i = 0; i < length; i++) {
  73. s.Append (random.Next (25) + 'A');
  74. }
  75. return s.ToString ();
  76. }
  77. }
  78. }