OdbcConnectionStringBuilder.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. //
  2. // System.Data.Odbc.OdbcConnectionStringBuilder
  3. //
  4. // Authors:
  5. // Nidhi Rawal ([email protected])
  6. //
  7. // Copyright (C) 2007 Novell, Inc (http://www.novell.com)
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining
  10. // a copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. #if NET_2_0
  29. using System;
  30. using System.Collections;
  31. using System.ComponentModel;
  32. using System.Collections.Generic;
  33. using System.Data;
  34. using System.Data.Common;
  35. using System.Data.Odbc;
  36. using System.Reflection;
  37. using System.Text;
  38. namespace System.Data.Odbc
  39. {
  40. [DefaultProperty ("Driver")]
  41. [TypeConverter ("System.Data.Odbc.OdbcConnectionStringBuilder+OdbcConnectionStringBuilderConverter, " + Consts.AssemblySystem_Data)]
  42. public sealed class OdbcConnectionStringBuilder : DbConnectionStringBuilder
  43. {
  44. #region Fields
  45. string driver;
  46. string dsn;
  47. #endregion //Fields
  48. #region Constructors
  49. public OdbcConnectionStringBuilder () : base (true)
  50. {
  51. }
  52. public OdbcConnectionStringBuilder (string connectionString) : base (true)
  53. {
  54. if (connectionString == null) {
  55. base.ConnectionString = string.Empty;
  56. return;
  57. }
  58. base.ConnectionString = connectionString;
  59. }
  60. #endregion // Constructors
  61. #region Properties
  62. public override Object this [string keyword] {
  63. get {
  64. if (keyword == null)
  65. throw new ArgumentNullException ("keyword");
  66. if (string.Compare (keyword, "Driver", StringComparison.InvariantCultureIgnoreCase) == 0)
  67. return Driver;
  68. if (string.Compare (keyword, "Dsn", StringComparison.InvariantCultureIgnoreCase) == 0)
  69. return Dsn;
  70. return base [keyword];
  71. }
  72. set {
  73. if (value == null) {
  74. Remove (keyword);
  75. return;
  76. }
  77. if (keyword == null)
  78. throw new ArgumentNullException ("keyword");
  79. string text_value = value.ToString ();
  80. if (string.Compare (keyword, "Driver", StringComparison.InvariantCultureIgnoreCase) == 0) {
  81. Driver = text_value;
  82. return;
  83. } else if (string.Compare (keyword, "Dsn", StringComparison.InvariantCultureIgnoreCase) == 0) {
  84. dsn = text_value;
  85. } else if (value.ToString ().IndexOf (';') != -1) {
  86. text_value = "{" + text_value + "}";
  87. }
  88. base [keyword] = value;
  89. }
  90. }
  91. public override ICollection Keys {
  92. get {
  93. List<string> keys = new List<string> ();
  94. keys.Add ("Dsn");
  95. keys.Add ("Driver");
  96. ICollection base_keys = base.Keys;
  97. foreach (string keyword in base_keys) {
  98. if (string.Compare (keyword, "Driver", StringComparison.InvariantCultureIgnoreCase) == 0)
  99. continue;
  100. if (string.Compare (keyword, "Dsn", StringComparison.InvariantCultureIgnoreCase) == 0)
  101. continue;
  102. keys.Add (keyword);
  103. }
  104. string [] final = new string [keys.Count];
  105. keys.CopyTo (final);
  106. return final;
  107. }
  108. }
  109. [DisplayName ("Driver")]
  110. [RefreshProperties (RefreshProperties.All)]
  111. public string Driver {
  112. get {
  113. if (driver == null)
  114. return string.Empty;
  115. return driver;
  116. }
  117. set {
  118. if (value == null)
  119. throw new ArgumentNullException ("Driver");
  120. driver = value;
  121. if (value.Length > 0) {
  122. int startBrace = value.IndexOf ('{');
  123. int endBrace = value.IndexOf ('}');
  124. if (startBrace == -1 || endBrace == -1)
  125. value = "{" + value + "}";
  126. else if (startBrace > 0 || endBrace < (value.Length - 1))
  127. value = "{" + value + "}";
  128. }
  129. base ["Driver"] = value;
  130. }
  131. }
  132. [DisplayName ("Dsn")]
  133. [RefreshProperties (RefreshProperties.All)]
  134. public string Dsn {
  135. get {
  136. if (dsn == null)
  137. return string.Empty;
  138. return dsn;
  139. }
  140. set {
  141. if (value == null)
  142. throw new ArgumentNullException ("Dsn");
  143. dsn = value;
  144. base ["Dsn"] = dsn;
  145. }
  146. }
  147. #endregion // Properties
  148. #region Methods
  149. public override bool ContainsKey (string keyword)
  150. {
  151. if (keyword == null)
  152. throw new ArgumentNullException ("keyword");
  153. if (string.Compare (keyword, "Driver", StringComparison.InvariantCultureIgnoreCase) == 0)
  154. return true;
  155. if (string.Compare (keyword, "Dsn", StringComparison.InvariantCultureIgnoreCase) == 0)
  156. return true;
  157. return base.ContainsKey (keyword);
  158. }
  159. public override bool Remove (string keyword)
  160. {
  161. if (keyword == null)
  162. throw new ArgumentNullException ("keyword");
  163. if (string.Compare (keyword, "Driver", StringComparison.InvariantCultureIgnoreCase) == 0)
  164. driver = string.Empty;
  165. else if (string.Compare (keyword, "Dsn", StringComparison.InvariantCultureIgnoreCase) == 0)
  166. dsn = string.Empty;
  167. return base.Remove (keyword);
  168. }
  169. public override void Clear ()
  170. {
  171. driver = null;
  172. dsn = null;
  173. base.Clear ();
  174. }
  175. public override bool TryGetValue (string keyword, out Object value)
  176. {
  177. if (keyword == null )
  178. throw new ArgumentNullException ("keyword");
  179. bool found = base.TryGetValue (keyword, out value);
  180. if (found)
  181. return found;
  182. if (string.Compare (keyword, "Driver", StringComparison.InvariantCultureIgnoreCase) == 0) {
  183. value = string.Empty;
  184. return true;
  185. } else if (string.Compare (keyword, "Dsn", StringComparison.InvariantCultureIgnoreCase) == 0) {
  186. value = string.Empty;
  187. return true;
  188. }
  189. return false;
  190. }
  191. #endregion // Methods
  192. }
  193. }
  194. #endif // NET_2_0 using