DbConnectionStringBuilder.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802
  1. //
  2. // System.Data.Common.DbConnectionStringBuilder.cs
  3. //
  4. // Author:
  5. // Sureshkumar T ([email protected])
  6. // Gert Driesen ([email protected]
  7. //
  8. // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining
  11. // a copy of this software and associated documentation files (the
  12. // "Software"), to deal in the Software without restriction, including
  13. // without limitation the rights to use, copy, modify, merge, publish,
  14. // distribute, sublicense, and/or sell copies of the Software, and to
  15. // permit persons to whom the Software is furnished to do so, subject to
  16. // 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 BE
  25. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  26. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  27. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. //
  29. #if NET_2_0
  30. using System;
  31. using System.Collections;
  32. using System.Collections.Generic;
  33. using System.Collections.ObjectModel;
  34. using System.ComponentModel;
  35. using System.Data;
  36. using System.Data.Common;
  37. using System.Reflection;
  38. using System.Text;
  39. namespace System.Data.Common
  40. {
  41. public class DbConnectionStringBuilder : IDictionary, ICollection, IEnumerable, ICustomTypeDescriptor
  42. {
  43. #region Fields
  44. readonly Dictionary<string, object> _dictionary;
  45. readonly bool useOdbcRules;
  46. #endregion Fields
  47. #region Constructors
  48. public DbConnectionStringBuilder () : this (false)
  49. {
  50. }
  51. public DbConnectionStringBuilder (bool useOdbcRules)
  52. {
  53. this.useOdbcRules = useOdbcRules;
  54. _dictionary = new Dictionary <string, object> (StringComparer.InvariantCultureIgnoreCase);
  55. }
  56. #endregion // Constructors
  57. #region Properties
  58. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  59. [EditorBrowsable (EditorBrowsableState.Never)]
  60. [Browsable (false)]
  61. [DesignOnly (true)]
  62. public bool BrowsableConnectionString {
  63. get { throw new NotImplementedException (); }
  64. set { throw new NotImplementedException (); }
  65. }
  66. [RefreshProperties (RefreshProperties.All)]
  67. public string ConnectionString {
  68. get {
  69. IDictionary<string, object> dictionary = (IDictionary <string, object>) _dictionary;
  70. StringBuilder sb = new StringBuilder ();
  71. foreach (string key in Keys) {
  72. object value = null;
  73. if (!dictionary.TryGetValue (key, out value))
  74. continue;
  75. string val = value.ToString ();
  76. AppendKeyValuePair (sb, key, val, useOdbcRules);
  77. }
  78. return sb.ToString ();
  79. }
  80. set {
  81. Clear ();
  82. if (value == null)
  83. return;
  84. if (value.Trim ().Length == 0)
  85. return;
  86. ParseConnectionString (value);
  87. }
  88. }
  89. [Browsable (false)]
  90. public virtual int Count
  91. {
  92. get { return _dictionary.Count; }
  93. }
  94. [Browsable (false)]
  95. public virtual bool IsFixedSize
  96. {
  97. get { return false; }
  98. }
  99. [Browsable (false)]
  100. public bool IsReadOnly
  101. {
  102. get { throw new NotImplementedException (); }
  103. }
  104. [Browsable (false)]
  105. public virtual object this [string keyword] {
  106. get {
  107. if (ContainsKey (keyword))
  108. return _dictionary [keyword];
  109. else
  110. throw new ArgumentException (string.Format (
  111. "Keyword '{0}' does not exist",
  112. keyword));
  113. }
  114. set {
  115. if (value == null) {
  116. Remove (keyword);
  117. return;
  118. }
  119. if (keyword == null)
  120. throw new ArgumentNullException ("keyword");
  121. if (keyword.Length == 0)
  122. throw CreateInvalidKeywordException (keyword);
  123. for (int i = 0; i < keyword.Length; i++) {
  124. char c = keyword [i];
  125. if (i == 0 && (Char.IsWhiteSpace (c) || c == ';'))
  126. throw CreateInvalidKeywordException (keyword);
  127. if (i == (keyword.Length - 1) && Char.IsWhiteSpace (c))
  128. throw CreateInvalidKeywordException (keyword);
  129. if (Char.IsControl (c))
  130. throw CreateInvalidKeywordException (keyword);
  131. }
  132. if (ContainsKey (keyword))
  133. _dictionary [keyword] = value;
  134. else
  135. _dictionary.Add (keyword, value);
  136. }
  137. }
  138. [Browsable (false)]
  139. public virtual ICollection Keys
  140. {
  141. get {
  142. string [] keys = new string [_dictionary.Keys.Count];
  143. ((ICollection<string>) _dictionary.Keys).CopyTo (keys, 0);
  144. ReadOnlyCollection<string> keyColl = new ReadOnlyCollection<string> (keys);
  145. return keyColl;
  146. }
  147. }
  148. bool ICollection.IsSynchronized
  149. {
  150. get { throw new NotImplementedException (); }
  151. }
  152. object ICollection.SyncRoot
  153. {
  154. get { throw new NotImplementedException (); }
  155. }
  156. object IDictionary.this [object keyword]
  157. {
  158. get { return this [(string) keyword]; }
  159. set { this [(string) keyword] = value; }
  160. }
  161. [Browsable (false)]
  162. public virtual ICollection Values {
  163. get {
  164. object [] values = new object [_dictionary.Values.Count];
  165. ((ICollection<object>) _dictionary.Values).CopyTo (values, 0);
  166. ReadOnlyCollection<object> valuesColl = new ReadOnlyCollection<object> (values);
  167. return valuesColl;
  168. }
  169. }
  170. #endregion // Properties
  171. #region Methods
  172. public void Add (string keyword, object value)
  173. {
  174. this [keyword] = value;
  175. }
  176. public static void AppendKeyValuePair (StringBuilder builder, string keyword, string value,
  177. bool useOdbcRules)
  178. {
  179. if (builder == null)
  180. throw new ArgumentNullException ("builder");
  181. if (keyword == null)
  182. throw new ArgumentNullException ("keyName");
  183. if (keyword.Length == 0)
  184. throw new ArgumentException ("Empty keyword is not valid.");
  185. if (builder.Length > 0)
  186. builder.Append (';');
  187. if (!useOdbcRules)
  188. builder.Append (keyword.Replace ("=", "=="));
  189. else
  190. builder.Append (keyword);
  191. builder.Append ('=');
  192. if (value == null || value.Length == 0)
  193. return;
  194. if (!useOdbcRules) {
  195. bool dquoteFound = (value.IndexOf ('\"') > -1);
  196. bool squoteFound = (value.IndexOf ('\'') > -1);
  197. if (dquoteFound && squoteFound) {
  198. builder.Append ('\"');
  199. builder.Append (value.Replace ("\"", "\"\""));
  200. builder.Append ('\"');
  201. } else if (dquoteFound) {
  202. builder.Append ('\'');
  203. builder.Append (value);
  204. builder.Append ('\'');
  205. } else if (squoteFound || value.IndexOf ('=') > -1 || value.IndexOf (';') > -1) {
  206. builder.Append ('\"');
  207. builder.Append (value);
  208. builder.Append ('\"');
  209. } else if (ValueNeedsQuoting (value)) {
  210. builder.Append ('\"');
  211. builder.Append (value);
  212. builder.Append ('\"');
  213. } else
  214. builder.Append (value);
  215. } else {
  216. int braces = 0;
  217. bool semicolonFound = false;
  218. int len = value.Length;
  219. bool needBraces = false;
  220. int lastChar = -1;
  221. for (int i = 0; i < len; i++) {
  222. int peek = 0;
  223. if (i == (len - 1))
  224. peek = -1;
  225. else
  226. peek = value [i + 1];
  227. char c = value [i];
  228. switch (c) {
  229. case '{':
  230. braces++;
  231. break;
  232. case '}':
  233. if (peek.Equals (c)) {
  234. i++;
  235. continue;
  236. } else {
  237. braces--;
  238. if (peek != -1)
  239. needBraces = true;
  240. }
  241. break;
  242. case ';':
  243. semicolonFound = true;
  244. break;
  245. default:
  246. break;
  247. }
  248. lastChar = c;
  249. }
  250. if (value [0] == '{' && (lastChar != '}' || (braces == 0 && needBraces))) {
  251. builder.Append ('{');
  252. builder.Append (value.Replace ("}", "}}"));
  253. builder.Append ('}');
  254. return;
  255. }
  256. bool isDriver = (string.Compare (keyword, "Driver", StringComparison.InvariantCultureIgnoreCase) == 0);
  257. if (isDriver) {
  258. if (value [0] == '{' && lastChar == '}' && !needBraces) {
  259. builder.Append (value);
  260. return;
  261. }
  262. builder.Append ('{');
  263. builder.Append (value.Replace ("}", "}}"));
  264. builder.Append ('}');
  265. return;
  266. }
  267. if (value [0] == '{' && (braces != 0 || lastChar != '}') && needBraces) {
  268. builder.Append ('{');
  269. builder.Append (value.Replace ("}", "}}"));
  270. builder.Append ('}');
  271. return;
  272. }
  273. if (value [0] != '{' && semicolonFound) {
  274. builder.Append ('{');
  275. builder.Append (value.Replace ("}", "}}"));
  276. builder.Append ('}');
  277. return;
  278. }
  279. builder.Append (value);
  280. }
  281. }
  282. public static void AppendKeyValuePair (StringBuilder builder, string keyword, string value)
  283. {
  284. AppendKeyValuePair (builder, keyword, value, false);
  285. }
  286. public virtual void Clear ()
  287. {
  288. _dictionary.Clear ();
  289. }
  290. public virtual bool ContainsKey (string keyword)
  291. {
  292. if (keyword == null)
  293. throw new ArgumentNullException ("keyword");
  294. return _dictionary.ContainsKey (keyword);
  295. }
  296. public virtual bool EquivalentTo (DbConnectionStringBuilder connectionStringBuilder)
  297. {
  298. bool ret = true;
  299. try {
  300. if (Count != connectionStringBuilder.Count)
  301. ret = false;
  302. else {
  303. foreach (string key in Keys) {
  304. if (!this [key].Equals (connectionStringBuilder [key])) {
  305. ret = false;
  306. break;
  307. }
  308. }
  309. }
  310. } catch (ArgumentException) {
  311. ret = false;
  312. }
  313. return ret;
  314. }
  315. [MonoTODO]
  316. protected virtual void GetProperties (Hashtable propertyDescriptors)
  317. {
  318. throw new NotImplementedException ();
  319. }
  320. [MonoTODO]
  321. protected internal void ClearPropertyDescriptors ()
  322. {
  323. throw new NotImplementedException ();
  324. }
  325. public virtual bool Remove (string keyword)
  326. {
  327. if (keyword == null)
  328. throw new ArgumentNullException ("keyword");
  329. return _dictionary.Remove (keyword);
  330. }
  331. public virtual bool ShouldSerialize (string keyword)
  332. {
  333. throw new NotImplementedException ();
  334. }
  335. void ICollection.CopyTo (Array array, int index)
  336. {
  337. if (array == null)
  338. throw new ArgumentNullException ("array");
  339. KeyValuePair<string, object> [] arr = array as KeyValuePair<string, object> [];
  340. if (arr == null)
  341. throw new ArgumentException ("Target array type is not compatible with the type of items in the collection");
  342. ((ICollection<KeyValuePair<string, object>>) _dictionary).CopyTo (arr, index);
  343. }
  344. void IDictionary.Add (object keyword, object value)
  345. {
  346. this.Add ((string) keyword, value);
  347. }
  348. bool IDictionary.Contains (object keyword)
  349. {
  350. return ContainsKey ((string) keyword);
  351. }
  352. IDictionaryEnumerator IDictionary.GetEnumerator ()
  353. {
  354. return (IDictionaryEnumerator) _dictionary.GetEnumerator ();
  355. }
  356. void IDictionary.Remove (object keyword)
  357. {
  358. Remove ((string) keyword);
  359. }
  360. IEnumerator IEnumerable.GetEnumerator ()
  361. {
  362. return (IEnumerator) _dictionary.GetEnumerator ();
  363. }
  364. private static object _staticAttributeCollection = null;
  365. AttributeCollection ICustomTypeDescriptor.GetAttributes ()
  366. {
  367. object value = _staticAttributeCollection;
  368. if (value == null) {
  369. CLSCompliantAttribute clsAttr = new CLSCompliantAttribute (true);
  370. DefaultMemberAttribute defMemAttr = new DefaultMemberAttribute ("Item");
  371. Attribute [] attrs = {clsAttr, defMemAttr};
  372. value = new AttributeCollection (attrs);
  373. }
  374. System.Threading.Interlocked.CompareExchange (ref _staticAttributeCollection, value, null);
  375. return _staticAttributeCollection as AttributeCollection;
  376. }
  377. string ICustomTypeDescriptor.GetClassName ()
  378. {
  379. return this.GetType ().ToString ();
  380. }
  381. string ICustomTypeDescriptor.GetComponentName ()
  382. {
  383. return null;
  384. }
  385. TypeConverter ICustomTypeDescriptor.GetConverter ()
  386. {
  387. return new CollectionConverter ();
  388. }
  389. EventDescriptor ICustomTypeDescriptor.GetDefaultEvent ()
  390. {
  391. return null;
  392. }
  393. PropertyDescriptor ICustomTypeDescriptor.GetDefaultProperty ()
  394. {
  395. return null;
  396. }
  397. object ICustomTypeDescriptor.GetEditor (Type editorBaseType)
  398. {
  399. return null;
  400. }
  401. EventDescriptorCollection ICustomTypeDescriptor.GetEvents ()
  402. {
  403. return EventDescriptorCollection.Empty;
  404. }
  405. EventDescriptorCollection ICustomTypeDescriptor.GetEvents (Attribute [] attributes)
  406. {
  407. return EventDescriptorCollection.Empty;
  408. }
  409. PropertyDescriptorCollection ICustomTypeDescriptor.GetProperties ()
  410. {
  411. return PropertyDescriptorCollection.Empty;
  412. }
  413. PropertyDescriptorCollection ICustomTypeDescriptor.GetProperties (Attribute [] attributes)
  414. {
  415. return PropertyDescriptorCollection.Empty;
  416. }
  417. object ICustomTypeDescriptor.GetPropertyOwner (PropertyDescriptor pd)
  418. {
  419. throw new NotImplementedException ();
  420. }
  421. public override string ToString ()
  422. {
  423. return ConnectionString;
  424. }
  425. public virtual bool TryGetValue (string keyword, out object value)
  426. {
  427. // FIXME : not sure, difference between this [keyword] and this method
  428. bool found = ContainsKey (keyword);
  429. if (found)
  430. value = this [keyword];
  431. else
  432. value = null;
  433. return found;
  434. }
  435. static ArgumentException CreateInvalidKeywordException (string keyword)
  436. {
  437. return new ArgumentException ("A keyword cannot contain "
  438. + "control characters, leading semicolons or "
  439. + "leading or trailing whitespace.", keyword);
  440. }
  441. static ArgumentException CreateConnectionStringInvalidException (int index)
  442. {
  443. return new ArgumentException ("Format of initialization "
  444. + "string does not conform to specifications at "
  445. + "index " + index + ".");
  446. }
  447. static bool ValueNeedsQuoting (string value)
  448. {
  449. foreach (char c in value) {
  450. if (char.IsWhiteSpace (c))
  451. return true;
  452. }
  453. return false;
  454. }
  455. void ParseConnectionString (string connectionString)
  456. {
  457. if (useOdbcRules)
  458. ParseConnectionStringOdbc (connectionString);
  459. else
  460. ParseConnectionStringNonOdbc (connectionString);
  461. }
  462. void ParseConnectionStringOdbc (string connectionString)
  463. {
  464. bool inQuote = false;
  465. bool inDQuote = false;
  466. bool inName = true;
  467. bool inBraces = false;
  468. string name = String.Empty;
  469. string val = String.Empty;
  470. StringBuilder sb = new StringBuilder ();
  471. int len = connectionString.Length;
  472. for (int i = 0; i < len; i++) {
  473. char c = connectionString [i];
  474. int peek = (i == (len - 1)) ? -1 : connectionString [i + 1];
  475. switch (c) {
  476. case '{':
  477. if (inName) {
  478. sb.Append (c);
  479. continue;
  480. }
  481. if (sb.Length == 0)
  482. inBraces = true;
  483. sb.Append (c);
  484. break;
  485. case '}':
  486. if (inName || !inBraces) {
  487. sb.Append (c);
  488. continue;
  489. }
  490. if (peek == -1) {
  491. sb.Append (c);
  492. inBraces = false;
  493. } else if (peek.Equals (c)) {
  494. sb.Append (c);
  495. sb.Append (c);
  496. i++;
  497. } else {
  498. int next = NextNonWhitespaceChar (connectionString, i);
  499. if (next != -1 && ((char) next) != ';')
  500. throw CreateConnectionStringInvalidException (next);
  501. sb.Append (c);
  502. inBraces = false;
  503. }
  504. break;
  505. case ';':
  506. if (inName || inBraces) {
  507. sb.Append (c);
  508. continue;
  509. }
  510. if (name.Length > 0 && sb.Length > 0) {
  511. val = sb.ToString ();
  512. name = name.ToLower ().TrimEnd ();
  513. this [name] = val;
  514. } else if (sb.Length > 0)
  515. throw CreateConnectionStringInvalidException (c);
  516. inName = true;
  517. name = String.Empty;
  518. sb.Length = 0;
  519. break;
  520. case '=':
  521. if (inBraces || !inName) {
  522. sb.Append (c);
  523. continue;
  524. }
  525. name = sb.ToString ();
  526. if (name.Length == 0)
  527. throw CreateConnectionStringInvalidException (c);
  528. sb.Length = 0;
  529. inName = false;
  530. break;
  531. default:
  532. if (inDQuote || inQuote || inBraces)
  533. sb.Append (c);
  534. else if (char.IsWhiteSpace (c)) {
  535. // ignore leading whitespace
  536. if (sb.Length > 0) {
  537. int nextChar = SkipTrailingWhitespace (connectionString, i);
  538. if (nextChar == -1)
  539. sb.Append (c);
  540. else
  541. i = nextChar;
  542. }
  543. } else
  544. sb.Append (c);
  545. break;
  546. }
  547. }
  548. if ((inName && sb.Length > 0) || inDQuote || inQuote || inBraces)
  549. throw CreateConnectionStringInvalidException (len - 1);
  550. if (name.Length > 0 && sb.Length > 0) {
  551. val = sb.ToString ();
  552. name = name.ToLower ().TrimEnd ();
  553. this [name] = val;
  554. }
  555. }
  556. void ParseConnectionStringNonOdbc (string connectionString)
  557. {
  558. bool inQuote = false;
  559. bool inDQuote = false;
  560. bool inName = true;
  561. string name = String.Empty;
  562. string val = String.Empty;
  563. StringBuilder sb = new StringBuilder ();
  564. int len = connectionString.Length;
  565. for (int i = 0; i < len; i++) {
  566. char c = connectionString [i];
  567. int peek = (i == (len - 1)) ? -1 : connectionString [i + 1];
  568. switch (c) {
  569. case '\'':
  570. if (inName) {
  571. sb.Append (c);
  572. continue;
  573. }
  574. if (inDQuote)
  575. sb.Append (c);
  576. else if (inQuote) {
  577. if (peek == -1)
  578. inQuote = false;
  579. else if (peek.Equals (c)) {
  580. sb.Append (c);
  581. i++;
  582. } else {
  583. int next = NextNonWhitespaceChar (connectionString, i);
  584. if (next != -1 && ((char) next) != ';')
  585. throw CreateConnectionStringInvalidException (next);
  586. inQuote = false;
  587. }
  588. if (!inQuote) {
  589. val = sb.ToString ();
  590. name = name.ToLower ().TrimEnd ();
  591. this [name] = val;
  592. inName = true;
  593. name = String.Empty;
  594. sb.Length = 0;
  595. }
  596. } else if (sb.Length == 0)
  597. inQuote = true;
  598. else
  599. sb.Append (c);
  600. break;
  601. case '"':
  602. if (inName) {
  603. sb.Append (c);
  604. continue;
  605. }
  606. if (inQuote)
  607. sb.Append (c);
  608. else if (inDQuote) {
  609. if (peek == -1)
  610. inDQuote = false;
  611. else if (peek.Equals (c)) {
  612. sb.Append (c);
  613. i++;
  614. } else {
  615. int next = NextNonWhitespaceChar (connectionString, i);
  616. if (next != -1 && ((char) next) != ';')
  617. throw CreateConnectionStringInvalidException (next);
  618. inDQuote = false;
  619. }
  620. } else if (sb.Length == 0)
  621. inDQuote = true;
  622. else
  623. sb.Append (c);
  624. break;
  625. case ';':
  626. if (inName) {
  627. sb.Append (c);
  628. continue;
  629. }
  630. if (inDQuote || inQuote)
  631. sb.Append (c);
  632. else {
  633. if (name.Length > 0 && sb.Length > 0) {
  634. val = sb.ToString ();
  635. name = name.ToLower ().TrimEnd ();
  636. this [name] = val;
  637. } else if (sb.Length > 0)
  638. throw CreateConnectionStringInvalidException (c);
  639. inName = true;
  640. name = String.Empty;
  641. sb.Length = 0;
  642. }
  643. break;
  644. case '=':
  645. if (inDQuote || inQuote || !inName)
  646. sb.Append (c);
  647. else if (peek != -1 && peek.Equals (c)) {
  648. sb.Append (c);
  649. i++;
  650. } else {
  651. name = sb.ToString ();
  652. if (name.Length == 0)
  653. throw CreateConnectionStringInvalidException (c);
  654. sb.Length = 0;
  655. inName = false;
  656. }
  657. break;
  658. default:
  659. if (inDQuote || inQuote)
  660. sb.Append (c);
  661. else if (char.IsWhiteSpace (c)) {
  662. // ignore leading whitespace
  663. if (sb.Length > 0) {
  664. int nextChar = SkipTrailingWhitespace (connectionString, i);
  665. if (nextChar == -1)
  666. sb.Append (c);
  667. else
  668. i = nextChar;
  669. }
  670. } else
  671. sb.Append (c);
  672. break;
  673. }
  674. }
  675. if ((inName && sb.Length > 0) || inDQuote || inQuote)
  676. throw CreateConnectionStringInvalidException (len -1);
  677. if (name.Length > 0 && sb.Length > 0) {
  678. val = sb.ToString ();
  679. name = name.ToLower ().TrimEnd ();
  680. this [name] = val;
  681. }
  682. }
  683. static int SkipTrailingWhitespace (string value, int index)
  684. {
  685. int len = value.Length;
  686. for (int i = (index + 1); i < len; i++) {
  687. char c = value [i];
  688. if (c == ';')
  689. return (i - 1);
  690. if (!char.IsWhiteSpace (c))
  691. return -1;
  692. }
  693. return len - 1;
  694. }
  695. static int NextNonWhitespaceChar (string value, int index)
  696. {
  697. int len = value.Length;
  698. for (int i = (index + 1); i < len; i++) {
  699. char c = value [i];
  700. if (!char.IsWhiteSpace (c))
  701. return (int) c;
  702. }
  703. return -1;
  704. }
  705. #endregion // Public Methods
  706. }
  707. }
  708. #endif // NET_2_0 using