SqlConnectionStringBuilder.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623
  1. //
  2. // System.Data.SqlClient.SqlConnectionStringBuilder.cs
  3. //
  4. // Author:
  5. // Sureshkumar T ([email protected])
  6. //
  7. // Copyright (C) 2004 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.Text;
  31. using System.Collections;
  32. using System.Collections.Generic;
  33. using System.Data;
  34. using System.Data.Common;
  35. namespace System.Data.SqlClient
  36. {
  37. public sealed class SqlConnectionStringBuilder : DbConnectionStringBuilder
  38. {
  39. private const string DEF_APPLICATIONNAME = ".NET SqlClient Data Provider";
  40. private const bool DEF_ASYNCHRONOUSPROCESSING = false;
  41. private const string DEF_ATTACHDBFILENAME = "";
  42. private const bool DEF_CONNECTIONRESET = true;
  43. private const int DEF_CONNECTTIMEOUT = 15;
  44. private const string DEF_CURRENTLANGUAGE = "";
  45. private const string DEF_DATASOURCE = "";
  46. private const bool DEF_ENCRYPT = false;
  47. private const bool DEF_ENLIST = false;
  48. private const string DEF_FAILOVERPARTNER = "";
  49. private const string DEF_INITIALCATALOG = "";
  50. private const bool DEF_INTEGRATEDSECURITY = false;
  51. private const int DEF_LOADBALANCETIMEOUT = 0;
  52. private const int DEF_MAXPOOLSIZE = 100;
  53. private const int DEF_MINPOOLSIZE = 0;
  54. private const bool DEF_MULTIPLEACTIVERESULTSETS = false;
  55. private const string DEF_NETWORKLIBRARY = "";
  56. private const int DEF_PACKETSIZE = 8000;
  57. private const string DEF_PASSWORD = "";
  58. private const bool DEF_PERSISTSECURITYINFO = false;
  59. private const bool DEF_POOLING = true;
  60. private const bool DEF_REPLICATION = false;
  61. private const string DEF_USERID = "";
  62. private const string DEF_WORKSTATIONID = "";
  63. #region // Fields
  64. private string _applicationName;
  65. private bool _asynchronousProcessing;
  66. private string _attachDBFilename;
  67. private bool _connectionReset;
  68. private int _connectTimeout;
  69. private string _currentLanguage;
  70. private string _dataSource;
  71. private bool _encrypt;
  72. private bool _enlist;
  73. private string _failoverPartner;
  74. private string _initialCatalog;
  75. private bool _integratedSecurity;
  76. private int _loadBalanceTimeout;
  77. private int _maxPoolSize;
  78. private int _minPoolSize;
  79. private bool _multipleActiveResultSets;
  80. private string _networkLibrary;
  81. private int _packetSize;
  82. private string _password;
  83. private bool _persistSecurityInfo;
  84. private bool _pooling;
  85. private bool _replication;
  86. private string _userID;
  87. private string _workstationID;
  88. private static Dictionary <string, string> _keywords; // for mapping duplicate keywords
  89. #endregion // Fields
  90. #region Constructors
  91. public SqlConnectionStringBuilder () : this (String.Empty)
  92. {
  93. }
  94. public SqlConnectionStringBuilder (string connectionString)
  95. {
  96. Init ();
  97. base.ConnectionString = connectionString;
  98. }
  99. static SqlConnectionStringBuilder ()
  100. {
  101. _keywords = new Dictionary <string, string> ();
  102. _keywords ["APP"] = "Application Name";
  103. _keywords ["APPLICATION NAME"] = "Application Name";
  104. _keywords ["ATTACHDBFILENAME"] = "AttachDBFilename";
  105. _keywords ["EXTENDED PROPERTIES"] = "Extended Properties";
  106. _keywords ["INITIAL FILE NAME"] = "Initial File Name";
  107. _keywords ["TIMEOUT"] = "Connect Timeout";
  108. _keywords ["CONNECT TIMEOUT"] = "Connect Timeout";
  109. _keywords ["CONNECTION TIMEOUT"] = "Connect Timeout";
  110. _keywords ["CONNECTION RESET"] = "Connection Reset";
  111. _keywords ["LANGUAGE"] = "Current Language";
  112. _keywords ["CURRENT LANGUAGE"] = "Current Language";
  113. _keywords ["DATA SOURCE"] = "Data Source";
  114. _keywords ["SERVER"] = "Data Source";
  115. _keywords ["ADDRESS"] = "Data Source";
  116. _keywords ["ADDR"] = "Data Source";
  117. _keywords ["NETWORK ADDRESS"] = "Data Source";
  118. _keywords ["ENCRYPT"] = "Encrypt";
  119. _keywords ["ENLIST"] = "Enlist";
  120. _keywords ["INITIAL CATALOG"] = "Initial Catalog";
  121. _keywords ["DATABASE"] = "Initial Catalog";
  122. _keywords ["INTEGRATED SECURITY"] = "Integrated Security";
  123. _keywords ["TRUSTED_CONNECTION"] = "Integrated Security";
  124. _keywords ["MAX POOL SIZE"] = "Max Pool Size";
  125. _keywords ["MIN POOL SIZE"] = "Min Pool Size";
  126. _keywords ["MULTIPLEACTIVERESULTSETS"] = "Multipleactiveresultset";
  127. _keywords ["ASYNCHRONOUS PROCESSING"] = "Asynchronous Processing";
  128. _keywords ["ASYNC"] = "Async";
  129. _keywords ["NET"] = "Network Library";
  130. _keywords ["NETWORK"] = "Network Library";
  131. _keywords ["NETWORK LIBRARY"] = "Network Library";
  132. _keywords ["PACKET SIZE"] = "Packet Size";
  133. _keywords ["PASSWORD"] = "Password";
  134. _keywords ["PWD"] = "Password";
  135. _keywords ["PERSISTSECURITYINFO"] = "Persist Security Info";
  136. _keywords ["PERSIST SECURITY INFO"] = "Persist Security Info";
  137. _keywords ["POOLING"] = "Pooling";
  138. _keywords ["UID"] = "User Id";
  139. _keywords ["USER"] = "User Id";
  140. _keywords ["USER ID"] = "User Id";
  141. _keywords ["WSID"] = "Workstation Id";
  142. _keywords ["WORKSTATION ID"] = "Workstation Id";
  143. }
  144. #endregion // Constructors
  145. #region Properties
  146. public string ApplicationName {
  147. get { return _applicationName; }
  148. set {
  149. base ["Application Name"] = value;
  150. _applicationName = value;
  151. }
  152. }
  153. public bool AsynchronousProcessing {
  154. get { return _asynchronousProcessing; }
  155. set {
  156. base ["Asynchronous Processing"] = value;
  157. _asynchronousProcessing = value;
  158. }
  159. }
  160. public string AttachDBFilename {
  161. get { return _attachDBFilename; }
  162. set {
  163. base ["AttachDbFilename"] = value;
  164. _attachDBFilename = value;
  165. }
  166. }
  167. public bool ConnectionReset {
  168. get { return _connectionReset; }
  169. set {
  170. base ["Connection Reset"] = value;
  171. _connectionReset = value;
  172. }
  173. }
  174. public int ConnectTimeout {
  175. get { return _connectTimeout; }
  176. set {
  177. base ["Connect Timeout"] = value;
  178. _connectTimeout = value;
  179. }
  180. }
  181. public string CurrentLanguage {
  182. get { return _currentLanguage; }
  183. set {
  184. base ["Current Language"] = value;
  185. _currentLanguage = value;
  186. }
  187. }
  188. public string DataSource {
  189. get { return _dataSource; }
  190. set {
  191. base ["Data Source"] = value;
  192. _dataSource = value;
  193. }
  194. }
  195. public bool Encrypt {
  196. get { return _encrypt; }
  197. set {
  198. base ["Encrypt"] = value;
  199. _encrypt = value;
  200. }
  201. }
  202. public bool Enlist {
  203. get { return _enlist; }
  204. set {
  205. base ["Enlist"] = value;
  206. _enlist = value;
  207. }
  208. }
  209. public string FailoverPartner {
  210. get { return _failoverPartner; }
  211. set {
  212. base ["Failover Partner"] = value;
  213. _failoverPartner = value;
  214. }
  215. }
  216. public string InitialCatalog {
  217. get { return _initialCatalog; }
  218. set {
  219. base ["Initial Catalog"] = value;
  220. _initialCatalog = value;
  221. }
  222. }
  223. public bool IntegratedSecurity {
  224. get { return _integratedSecurity; }
  225. set {
  226. base ["Integrated Security"] = value;
  227. _integratedSecurity = value;
  228. }
  229. }
  230. public override bool IsFixedSize {
  231. get { return true; }
  232. }
  233. public override object this [string keyword] {
  234. get {
  235. string mapped = MapKeyword (keyword);
  236. return base [mapped];
  237. }
  238. set {SetValue (keyword, value);}
  239. }
  240. public override ICollection Keys {
  241. get { return base.Keys; }
  242. }
  243. public int LoadBalanceTimeout {
  244. get { return _loadBalanceTimeout; }
  245. set {
  246. base ["Load Balance Timeout"] = value;
  247. _loadBalanceTimeout = value;
  248. }
  249. }
  250. public int MaxPoolSize {
  251. get { return _maxPoolSize; }
  252. set {
  253. base ["Max Pool Size"] = value;
  254. _maxPoolSize = value;
  255. }
  256. }
  257. public int MinPoolSize {
  258. get { return _minPoolSize; }
  259. set {
  260. base ["Min Pool Size"] = value;
  261. _minPoolSize = value;
  262. }
  263. }
  264. public bool MultipleActiveResultSets {
  265. get { return _multipleActiveResultSets; }
  266. set {
  267. base ["Multiple Active Resultsets"] = value;
  268. _multipleActiveResultSets = value;
  269. }
  270. }
  271. public string NetworkLibrary {
  272. get { return _networkLibrary; }
  273. set {
  274. base ["Network Library"] = value;
  275. _networkLibrary = value;
  276. }
  277. }
  278. public int PacketSize {
  279. get { return _packetSize; }
  280. set {
  281. base ["Packet Size"] = value;
  282. _packetSize = value;
  283. }
  284. }
  285. public string Password {
  286. get { return _password; }
  287. set {
  288. base ["Password"] = value;
  289. _password = value;
  290. }
  291. }
  292. public bool PersistSecurityInfo {
  293. get { return _persistSecurityInfo; }
  294. set {
  295. base ["Persist Security Info"] = value;
  296. _persistSecurityInfo = value;
  297. }
  298. }
  299. public bool Pooling {
  300. get { return _pooling; }
  301. set {
  302. base ["Pooling"] = value;
  303. _pooling = value;
  304. }
  305. }
  306. public bool Replication {
  307. get { return _replication; }
  308. set {
  309. base ["Replication"] = value;
  310. _replication = value;
  311. }
  312. }
  313. public string UserID {
  314. get { return _userID; }
  315. set {
  316. base ["User Id"]= value;
  317. _userID = value;
  318. }
  319. }
  320. public override ICollection Values {
  321. get { return base.Values; }
  322. }
  323. public string WorkstationID {
  324. get { return _workstationID; }
  325. set {
  326. base ["Workstation Id"] = value;
  327. _workstationID = value;
  328. }
  329. }
  330. #endregion // Properties
  331. #region Methods
  332. private void Init ()
  333. {
  334. _applicationName = DEF_APPLICATIONNAME;
  335. _asynchronousProcessing = DEF_ASYNCHRONOUSPROCESSING;
  336. _attachDBFilename = DEF_ATTACHDBFILENAME;
  337. _connectionReset = DEF_CONNECTIONRESET;
  338. _connectTimeout = DEF_CONNECTTIMEOUT;
  339. _currentLanguage = DEF_CURRENTLANGUAGE;
  340. _dataSource = DEF_DATASOURCE;
  341. _encrypt = DEF_ENCRYPT;
  342. _enlist = DEF_ENLIST;
  343. _failoverPartner = DEF_FAILOVERPARTNER;
  344. _initialCatalog = DEF_INITIALCATALOG;
  345. _integratedSecurity = DEF_INTEGRATEDSECURITY;
  346. _loadBalanceTimeout = DEF_LOADBALANCETIMEOUT;
  347. _maxPoolSize = DEF_MAXPOOLSIZE;
  348. _minPoolSize = DEF_MINPOOLSIZE;
  349. _multipleActiveResultSets= DEF_MULTIPLEACTIVERESULTSETS;
  350. _networkLibrary = DEF_NETWORKLIBRARY;
  351. _packetSize = DEF_PACKETSIZE;
  352. _password = DEF_PASSWORD;
  353. _persistSecurityInfo = DEF_PERSISTSECURITYINFO;
  354. _pooling = DEF_POOLING;
  355. _replication = DEF_REPLICATION;
  356. _userID = DEF_USERID;
  357. _workstationID = DEF_WORKSTATIONID;
  358. }
  359. public override void Clear ()
  360. {
  361. base.Clear ();
  362. Init ();
  363. }
  364. public override bool ContainsKey (string keyword)
  365. {
  366. keyword = keyword.ToUpper ().Trim ();
  367. if (_keywords.ContainsKey (keyword))
  368. return base.ContainsKey (_keywords [keyword]);
  369. return false;
  370. }
  371. public override bool Remove (string keyword)
  372. {
  373. if (!ContainsKey (keyword))
  374. return false;
  375. this [keyword] = null;
  376. return true;
  377. }
  378. public override bool ShouldSerialize (string keyword)
  379. {
  380. if (!ContainsKey (keyword))
  381. return false;
  382. keyword = keyword.ToUpper ().Trim ();
  383. // Assuming passwords cannot be serialized.
  384. if (_keywords [keyword] == "Password")
  385. return false;
  386. return base.ShouldSerialize (_keywords [keyword]);
  387. }
  388. public override bool TryGetValue (string keyword, out object value)
  389. {
  390. if (! ContainsKey (keyword)) {
  391. value = String.Empty;
  392. return false;
  393. }
  394. return base.TryGetValue (_keywords [keyword.ToUpper ().Trim ()], out value);
  395. }
  396. #endregion // Methods
  397. #region Private Methods
  398. private string MapKeyword (string keyword)
  399. {
  400. keyword = keyword.ToUpper ().Trim ();
  401. if (! _keywords.ContainsKey (keyword))
  402. throw new ArgumentException("Keyword not supported :" + keyword);
  403. return _keywords [keyword];
  404. }
  405. private void SetValue (string key, object value)
  406. {
  407. if (key == null)
  408. throw new ArgumentNullException ("key cannot be null!");
  409. string mappedKey = MapKeyword (key);
  410. switch (mappedKey.ToUpper ().Trim ()) {
  411. case "APPLICATION NAME" :
  412. if (value == null) {
  413. _applicationName = DEF_APPLICATIONNAME;
  414. base.Remove (mappedKey);
  415. } else
  416. this.ApplicationName = value.ToString ();
  417. break;
  418. case "ATTACHDBFILENAME" :
  419. throw new NotImplementedException ("Attachable database support is " +
  420. "not implemented.");
  421. case "CONNECT TIMEOUT" :
  422. if (value == null) {
  423. _connectTimeout = DEF_CONNECTTIMEOUT;
  424. base.Remove (mappedKey);
  425. } else
  426. this.ConnectTimeout = DbConnectionStringBuilderHelper.ConvertToInt32 (value);
  427. break;
  428. case "CONNECTION LIFETIME" :
  429. break;
  430. case "CONNECTION RESET" :
  431. if (value == null) {
  432. _connectionReset = DEF_CONNECTIONRESET;
  433. base.Remove (mappedKey);
  434. } else
  435. this.ConnectionReset = DbConnectionStringBuilderHelper.ConvertToBoolean (value);
  436. break;
  437. case "CURRENT LANGUAGE" :
  438. if (value == null) {
  439. _currentLanguage = DEF_CURRENTLANGUAGE;
  440. base.Remove (mappedKey);
  441. } else
  442. this.CurrentLanguage = value.ToString ();
  443. break;
  444. case "DATA SOURCE" :
  445. if (value == null) {
  446. _dataSource = DEF_DATASOURCE;
  447. base.Remove (mappedKey);
  448. } else
  449. this.DataSource = value.ToString ();
  450. break;
  451. case "ENCRYPT":
  452. if (value == null) {
  453. _encrypt = DEF_ENCRYPT;
  454. base.Remove (mappedKey);
  455. }else if (DbConnectionStringBuilderHelper.ConvertToBoolean(value))
  456. throw new NotImplementedException("SSL encryption for"
  457. + " data sent between client and server is not"
  458. + " implemented.");
  459. break;
  460. case "ENLIST" :
  461. if (value == null) {
  462. _enlist = DEF_ENLIST;
  463. base.Remove (mappedKey);
  464. } else if ( ! DbConnectionStringBuilderHelper.ConvertToBoolean(value))
  465. throw new NotImplementedException("Disabling the automatic"
  466. + " enlistment of connections in the thread's current"
  467. + " transaction context is not implemented.");
  468. break;
  469. case "INITIAL CATALOG" :
  470. if (value == null) {
  471. _initialCatalog = DEF_INITIALCATALOG;
  472. base.Remove (mappedKey);
  473. } else
  474. this.InitialCatalog = value.ToString ();
  475. break;
  476. case "INTEGRATED SECURITY" :
  477. if (value == null) {
  478. _integratedSecurity = DEF_INTEGRATEDSECURITY;
  479. base.Remove (mappedKey);
  480. } else
  481. this.IntegratedSecurity = DbConnectionStringBuilderHelper.ConvertToBoolean (value);
  482. break;
  483. case "MAX POOL SIZE" :
  484. if (value == null) {
  485. _maxPoolSize = DEF_MAXPOOLSIZE;
  486. base.Remove (mappedKey);
  487. } else
  488. this.MaxPoolSize = DbConnectionStringBuilderHelper.ConvertToInt32 (value);
  489. break;
  490. case "MIN POOL SIZE" :
  491. if (value == null) {
  492. _minPoolSize = DEF_MINPOOLSIZE;
  493. base.Remove (mappedKey);
  494. } else
  495. this.MinPoolSize = DbConnectionStringBuilderHelper.ConvertToInt32 (value);
  496. break;
  497. case "MULTIPLEACTIVERESULTSETS":
  498. if (value == null) {
  499. _multipleActiveResultSets = DEF_MULTIPLEACTIVERESULTSETS;
  500. base.Remove (mappedKey);
  501. } else if ( DbConnectionStringBuilderHelper.ConvertToBoolean (value))
  502. throw new NotImplementedException ("MARS is not yet implemented!");
  503. break;
  504. case "ASYNCHRONOUS PROCESSING" :
  505. if (value == null) {
  506. _asynchronousProcessing = DEF_ASYNCHRONOUSPROCESSING;
  507. base.Remove (mappedKey);
  508. } else
  509. this.AsynchronousProcessing = DbConnectionStringBuilderHelper.ConvertToBoolean (value);
  510. break;
  511. case "NETWORK LIBRARY" :
  512. if (value == null) {
  513. _networkLibrary = DEF_NETWORKLIBRARY;
  514. base.Remove (mappedKey);
  515. } else {
  516. if (!value.ToString ().ToUpper ().Equals ("DBMSSOCN"))
  517. throw new ArgumentException ("Unsupported network library.");
  518. this.NetworkLibrary = value.ToString ().ToLower ();
  519. }
  520. break;
  521. case "PACKET SIZE" :
  522. if (value == null) {
  523. _packetSize = DEF_PACKETSIZE;
  524. base.Remove (mappedKey);
  525. } else
  526. this.PacketSize = DbConnectionStringBuilderHelper.ConvertToInt32 (value);
  527. break;
  528. case "PASSWORD" :
  529. if (value == null) {
  530. _password = DEF_PASSWORD;
  531. base.Remove (mappedKey);
  532. } else
  533. this.Password = value.ToString ();
  534. break;
  535. case "PERSIST SECURITY INFO" :
  536. if (value == null) {
  537. _persistSecurityInfo = DEF_PERSISTSECURITYINFO;
  538. base.Remove (mappedKey);
  539. } else if (DbConnectionStringBuilderHelper.ConvertToBoolean (value))
  540. throw new NotImplementedException ("Persisting security info" +
  541. " is not yet implemented");
  542. break;
  543. case "POOLING" :
  544. if (value == null) {
  545. _pooling = DEF_POOLING;
  546. base.Remove (mappedKey);
  547. } else
  548. this.Pooling = DbConnectionStringBuilderHelper.ConvertToBoolean (value);
  549. break;
  550. case "USER ID" :
  551. if (value == null) {
  552. _userID = DEF_USERID;
  553. base.Remove (mappedKey);
  554. } else
  555. this.UserID = value.ToString ();
  556. break;
  557. case "WORKSTATION ID" :
  558. if (value == null) {
  559. _workstationID = DEF_WORKSTATIONID;
  560. base.Remove (mappedKey);
  561. } else
  562. this.WorkstationID = value.ToString ();
  563. break;
  564. default :
  565. throw new ArgumentException("Keyword not supported :" + key);
  566. }
  567. }
  568. #endregion // Private Methods
  569. }
  570. }
  571. #endif // NET_2_0