| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623 |
- //
- // System.Data.SqlClient.SqlConnectionStringBuilder.cs
- //
- // Author:
- // Sureshkumar T ([email protected])
- //
- // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
- //
- // Permission is hereby granted, free of charge, to any person obtaining
- // a copy of this software and associated documentation files (the
- // "Software"), to deal in the Software without restriction, including
- // without limitation the rights to use, copy, modify, merge, publish,
- // distribute, sublicense, and/or sell copies of the Software, and to
- // permit persons to whom the Software is furnished to do so, subject to
- // the following conditions:
- //
- // The above copyright notice and this permission notice shall be
- // included in all copies or substantial portions of the Software.
- //
- // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
- // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
- // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
- // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
- // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
- // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
- // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
- //
- #if NET_2_0
- using System;
- using System.Text;
- using System.Collections;
- using System.Collections.Generic;
- using System.Data;
- using System.Data.Common;
- namespace System.Data.SqlClient
- {
- public sealed class SqlConnectionStringBuilder : DbConnectionStringBuilder
- {
- private const string DEF_APPLICATIONNAME = ".NET SqlClient Data Provider";
- private const bool DEF_ASYNCHRONOUSPROCESSING = false;
- private const string DEF_ATTACHDBFILENAME = "";
- private const bool DEF_CONNECTIONRESET = true;
- private const int DEF_CONNECTTIMEOUT = 15;
- private const string DEF_CURRENTLANGUAGE = "";
- private const string DEF_DATASOURCE = "";
- private const bool DEF_ENCRYPT = false;
- private const bool DEF_ENLIST = false;
- private const string DEF_FAILOVERPARTNER = "";
- private const string DEF_INITIALCATALOG = "";
- private const bool DEF_INTEGRATEDSECURITY = false;
- private const int DEF_LOADBALANCETIMEOUT = 0;
- private const int DEF_MAXPOOLSIZE = 100;
- private const int DEF_MINPOOLSIZE = 0;
- private const bool DEF_MULTIPLEACTIVERESULTSETS = false;
- private const string DEF_NETWORKLIBRARY = "";
- private const int DEF_PACKETSIZE = 8000;
- private const string DEF_PASSWORD = "";
- private const bool DEF_PERSISTSECURITYINFO = false;
- private const bool DEF_POOLING = true;
- private const bool DEF_REPLICATION = false;
- private const string DEF_USERID = "";
- private const string DEF_WORKSTATIONID = "";
- #region // Fields
- private string _applicationName;
- private bool _asynchronousProcessing;
- private string _attachDBFilename;
- private bool _connectionReset;
- private int _connectTimeout;
- private string _currentLanguage;
- private string _dataSource;
- private bool _encrypt;
- private bool _enlist;
- private string _failoverPartner;
- private string _initialCatalog;
- private bool _integratedSecurity;
- private int _loadBalanceTimeout;
- private int _maxPoolSize;
- private int _minPoolSize;
- private bool _multipleActiveResultSets;
- private string _networkLibrary;
- private int _packetSize;
- private string _password;
- private bool _persistSecurityInfo;
- private bool _pooling;
- private bool _replication;
- private string _userID;
- private string _workstationID;
- private static Dictionary <string, string> _keywords; // for mapping duplicate keywords
- #endregion // Fields
- #region Constructors
- public SqlConnectionStringBuilder () : this (String.Empty)
- {
- }
- public SqlConnectionStringBuilder (string connectionString)
- {
- Init ();
- base.ConnectionString = connectionString;
- }
- static SqlConnectionStringBuilder ()
- {
- _keywords = new Dictionary <string, string> ();
- _keywords ["APP"] = "Application Name";
- _keywords ["APPLICATION NAME"] = "Application Name";
- _keywords ["ATTACHDBFILENAME"] = "AttachDBFilename";
- _keywords ["EXTENDED PROPERTIES"] = "Extended Properties";
- _keywords ["INITIAL FILE NAME"] = "Initial File Name";
- _keywords ["TIMEOUT"] = "Connect Timeout";
- _keywords ["CONNECT TIMEOUT"] = "Connect Timeout";
- _keywords ["CONNECTION TIMEOUT"] = "Connect Timeout";
- _keywords ["CONNECTION RESET"] = "Connection Reset";
- _keywords ["LANGUAGE"] = "Current Language";
- _keywords ["CURRENT LANGUAGE"] = "Current Language";
- _keywords ["DATA SOURCE"] = "Data Source";
- _keywords ["SERVER"] = "Data Source";
- _keywords ["ADDRESS"] = "Data Source";
- _keywords ["ADDR"] = "Data Source";
- _keywords ["NETWORK ADDRESS"] = "Data Source";
- _keywords ["ENCRYPT"] = "Encrypt";
- _keywords ["ENLIST"] = "Enlist";
- _keywords ["INITIAL CATALOG"] = "Initial Catalog";
- _keywords ["DATABASE"] = "Initial Catalog";
- _keywords ["INTEGRATED SECURITY"] = "Integrated Security";
- _keywords ["TRUSTED_CONNECTION"] = "Integrated Security";
- _keywords ["MAX POOL SIZE"] = "Max Pool Size";
- _keywords ["MIN POOL SIZE"] = "Min Pool Size";
- _keywords ["MULTIPLEACTIVERESULTSETS"] = "Multipleactiveresultset";
- _keywords ["ASYNCHRONOUS PROCESSING"] = "Asynchronous Processing";
- _keywords ["ASYNC"] = "Async";
- _keywords ["NET"] = "Network Library";
- _keywords ["NETWORK"] = "Network Library";
- _keywords ["NETWORK LIBRARY"] = "Network Library";
- _keywords ["PACKET SIZE"] = "Packet Size";
- _keywords ["PASSWORD"] = "Password";
- _keywords ["PWD"] = "Password";
- _keywords ["PERSISTSECURITYINFO"] = "Persist Security Info";
- _keywords ["PERSIST SECURITY INFO"] = "Persist Security Info";
- _keywords ["POOLING"] = "Pooling";
- _keywords ["UID"] = "User Id";
- _keywords ["USER"] = "User Id";
- _keywords ["USER ID"] = "User Id";
- _keywords ["WSID"] = "Workstation Id";
- _keywords ["WORKSTATION ID"] = "Workstation Id";
- }
- #endregion // Constructors
- #region Properties
- public string ApplicationName {
- get { return _applicationName; }
- set {
- base ["Application Name"] = value;
- _applicationName = value;
- }
- }
- public bool AsynchronousProcessing {
- get { return _asynchronousProcessing; }
- set {
- base ["Asynchronous Processing"] = value;
- _asynchronousProcessing = value;
- }
- }
- public string AttachDBFilename {
- get { return _attachDBFilename; }
- set {
- base ["AttachDbFilename"] = value;
- _attachDBFilename = value;
- }
- }
- public bool ConnectionReset {
- get { return _connectionReset; }
- set {
- base ["Connection Reset"] = value;
- _connectionReset = value;
- }
- }
- public int ConnectTimeout {
- get { return _connectTimeout; }
- set {
- base ["Connect Timeout"] = value;
- _connectTimeout = value;
- }
- }
- public string CurrentLanguage {
- get { return _currentLanguage; }
- set {
- base ["Current Language"] = value;
- _currentLanguage = value;
- }
- }
- public string DataSource {
- get { return _dataSource; }
- set {
- base ["Data Source"] = value;
- _dataSource = value;
- }
- }
- public bool Encrypt {
- get { return _encrypt; }
- set {
- base ["Encrypt"] = value;
- _encrypt = value;
- }
- }
- public bool Enlist {
- get { return _enlist; }
- set {
- base ["Enlist"] = value;
- _enlist = value;
- }
- }
- public string FailoverPartner {
- get { return _failoverPartner; }
- set {
- base ["Failover Partner"] = value;
- _failoverPartner = value;
- }
- }
- public string InitialCatalog {
- get { return _initialCatalog; }
- set {
- base ["Initial Catalog"] = value;
- _initialCatalog = value;
- }
- }
- public bool IntegratedSecurity {
- get { return _integratedSecurity; }
- set {
- base ["Integrated Security"] = value;
- _integratedSecurity = value;
- }
- }
- public override bool IsFixedSize {
- get { return true; }
- }
- public override object this [string keyword] {
- get {
- string mapped = MapKeyword (keyword);
- return base [mapped];
- }
- set {SetValue (keyword, value);}
- }
- public override ICollection Keys {
- get { return base.Keys; }
- }
- public int LoadBalanceTimeout {
- get { return _loadBalanceTimeout; }
- set {
- base ["Load Balance Timeout"] = value;
- _loadBalanceTimeout = value;
- }
- }
- public int MaxPoolSize {
- get { return _maxPoolSize; }
- set {
- base ["Max Pool Size"] = value;
- _maxPoolSize = value;
- }
- }
- public int MinPoolSize {
- get { return _minPoolSize; }
- set {
- base ["Min Pool Size"] = value;
- _minPoolSize = value;
- }
- }
- public bool MultipleActiveResultSets {
- get { return _multipleActiveResultSets; }
- set {
- base ["Multiple Active Resultsets"] = value;
- _multipleActiveResultSets = value;
- }
- }
- public string NetworkLibrary {
- get { return _networkLibrary; }
- set {
- base ["Network Library"] = value;
- _networkLibrary = value;
- }
- }
- public int PacketSize {
- get { return _packetSize; }
- set {
- base ["Packet Size"] = value;
- _packetSize = value;
- }
- }
- public string Password {
- get { return _password; }
- set {
- base ["Password"] = value;
- _password = value;
- }
- }
- public bool PersistSecurityInfo {
- get { return _persistSecurityInfo; }
- set {
- base ["Persist Security Info"] = value;
- _persistSecurityInfo = value;
- }
- }
- public bool Pooling {
- get { return _pooling; }
- set {
- base ["Pooling"] = value;
- _pooling = value;
- }
- }
- public bool Replication {
- get { return _replication; }
- set {
- base ["Replication"] = value;
- _replication = value;
- }
- }
- public string UserID {
- get { return _userID; }
- set {
- base ["User Id"]= value;
- _userID = value;
- }
- }
-
- public override ICollection Values {
- get { return base.Values; }
- }
- public string WorkstationID {
- get { return _workstationID; }
- set {
- base ["Workstation Id"] = value;
- _workstationID = value;
- }
- }
- #endregion // Properties
- #region Methods
- private void Init ()
- {
- _applicationName = DEF_APPLICATIONNAME;
- _asynchronousProcessing = DEF_ASYNCHRONOUSPROCESSING;
- _attachDBFilename = DEF_ATTACHDBFILENAME;
- _connectionReset = DEF_CONNECTIONRESET;
- _connectTimeout = DEF_CONNECTTIMEOUT;
- _currentLanguage = DEF_CURRENTLANGUAGE;
- _dataSource = DEF_DATASOURCE;
- _encrypt = DEF_ENCRYPT;
- _enlist = DEF_ENLIST;
- _failoverPartner = DEF_FAILOVERPARTNER;
- _initialCatalog = DEF_INITIALCATALOG;
- _integratedSecurity = DEF_INTEGRATEDSECURITY;
- _loadBalanceTimeout = DEF_LOADBALANCETIMEOUT;
- _maxPoolSize = DEF_MAXPOOLSIZE;
- _minPoolSize = DEF_MINPOOLSIZE;
- _multipleActiveResultSets= DEF_MULTIPLEACTIVERESULTSETS;
- _networkLibrary = DEF_NETWORKLIBRARY;
- _packetSize = DEF_PACKETSIZE;
- _password = DEF_PASSWORD;
- _persistSecurityInfo = DEF_PERSISTSECURITYINFO;
- _pooling = DEF_POOLING;
- _replication = DEF_REPLICATION;
- _userID = DEF_USERID;
- _workstationID = DEF_WORKSTATIONID;
- }
- public override void Clear ()
- {
- base.Clear ();
- Init ();
- }
- public override bool ContainsKey (string keyword)
- {
- keyword = keyword.ToUpper ().Trim ();
- if (_keywords.ContainsKey (keyword))
- return base.ContainsKey (_keywords [keyword]);
- return false;
- }
- public override bool Remove (string keyword)
- {
- if (!ContainsKey (keyword))
- return false;
- this [keyword] = null;
- return true;
- }
- public override bool ShouldSerialize (string keyword)
- {
- if (!ContainsKey (keyword))
- return false;
- keyword = keyword.ToUpper ().Trim ();
- // Assuming passwords cannot be serialized.
- if (_keywords [keyword] == "Password")
- return false;
- return base.ShouldSerialize (_keywords [keyword]);
- }
- public override bool TryGetValue (string keyword, out object value)
- {
- if (! ContainsKey (keyword)) {
- value = String.Empty;
- return false;
- }
- return base.TryGetValue (_keywords [keyword.ToUpper ().Trim ()], out value);
- }
- #endregion // Methods
- #region Private Methods
- private string MapKeyword (string keyword)
- {
- keyword = keyword.ToUpper ().Trim ();
- if (! _keywords.ContainsKey (keyword))
- throw new ArgumentException("Keyword not supported :" + keyword);
- return _keywords [keyword];
- }
-
- private void SetValue (string key, object value)
- {
- if (key == null)
- throw new ArgumentNullException ("key cannot be null!");
- string mappedKey = MapKeyword (key);
- switch (mappedKey.ToUpper ().Trim ()) {
- case "APPLICATION NAME" :
- if (value == null) {
- _applicationName = DEF_APPLICATIONNAME;
- base.Remove (mappedKey);
- } else
- this.ApplicationName = value.ToString ();
- break;
- case "ATTACHDBFILENAME" :
- throw new NotImplementedException ("Attachable database support is " +
- "not implemented.");
- case "CONNECT TIMEOUT" :
- if (value == null) {
- _connectTimeout = DEF_CONNECTTIMEOUT;
- base.Remove (mappedKey);
- } else
- this.ConnectTimeout = DbConnectionStringBuilderHelper.ConvertToInt32 (value);
- break;
- case "CONNECTION LIFETIME" :
- break;
- case "CONNECTION RESET" :
- if (value == null) {
- _connectionReset = DEF_CONNECTIONRESET;
- base.Remove (mappedKey);
- } else
- this.ConnectionReset = DbConnectionStringBuilderHelper.ConvertToBoolean (value);
- break;
- case "CURRENT LANGUAGE" :
- if (value == null) {
- _currentLanguage = DEF_CURRENTLANGUAGE;
- base.Remove (mappedKey);
- } else
- this.CurrentLanguage = value.ToString ();
- break;
- case "DATA SOURCE" :
- if (value == null) {
- _dataSource = DEF_DATASOURCE;
- base.Remove (mappedKey);
- } else
- this.DataSource = value.ToString ();
- break;
- case "ENCRYPT":
- if (value == null) {
- _encrypt = DEF_ENCRYPT;
- base.Remove (mappedKey);
- }else if (DbConnectionStringBuilderHelper.ConvertToBoolean(value))
- throw new NotImplementedException("SSL encryption for"
- + " data sent between client and server is not"
- + " implemented.");
- break;
- case "ENLIST" :
- if (value == null) {
- _enlist = DEF_ENLIST;
- base.Remove (mappedKey);
- } else if ( ! DbConnectionStringBuilderHelper.ConvertToBoolean(value))
- throw new NotImplementedException("Disabling the automatic"
- + " enlistment of connections in the thread's current"
- + " transaction context is not implemented.");
- break;
- case "INITIAL CATALOG" :
- if (value == null) {
- _initialCatalog = DEF_INITIALCATALOG;
- base.Remove (mappedKey);
- } else
- this.InitialCatalog = value.ToString ();
- break;
- case "INTEGRATED SECURITY" :
- if (value == null) {
- _integratedSecurity = DEF_INTEGRATEDSECURITY;
- base.Remove (mappedKey);
- } else
- this.IntegratedSecurity = DbConnectionStringBuilderHelper.ConvertToBoolean (value);
- break;
- case "MAX POOL SIZE" :
- if (value == null) {
- _maxPoolSize = DEF_MAXPOOLSIZE;
- base.Remove (mappedKey);
- } else
- this.MaxPoolSize = DbConnectionStringBuilderHelper.ConvertToInt32 (value);
- break;
- case "MIN POOL SIZE" :
- if (value == null) {
- _minPoolSize = DEF_MINPOOLSIZE;
- base.Remove (mappedKey);
- } else
- this.MinPoolSize = DbConnectionStringBuilderHelper.ConvertToInt32 (value);
- break;
- case "MULTIPLEACTIVERESULTSETS":
- if (value == null) {
- _multipleActiveResultSets = DEF_MULTIPLEACTIVERESULTSETS;
- base.Remove (mappedKey);
- } else if ( DbConnectionStringBuilderHelper.ConvertToBoolean (value))
- throw new NotImplementedException ("MARS is not yet implemented!");
- break;
- case "ASYNCHRONOUS PROCESSING" :
- if (value == null) {
- _asynchronousProcessing = DEF_ASYNCHRONOUSPROCESSING;
- base.Remove (mappedKey);
- } else
- this.AsynchronousProcessing = DbConnectionStringBuilderHelper.ConvertToBoolean (value);
- break;
- case "NETWORK LIBRARY" :
- if (value == null) {
- _networkLibrary = DEF_NETWORKLIBRARY;
- base.Remove (mappedKey);
- } else {
- if (!value.ToString ().ToUpper ().Equals ("DBMSSOCN"))
- throw new ArgumentException ("Unsupported network library.");
- this.NetworkLibrary = value.ToString ().ToLower ();
- }
- break;
- case "PACKET SIZE" :
- if (value == null) {
- _packetSize = DEF_PACKETSIZE;
- base.Remove (mappedKey);
- } else
- this.PacketSize = DbConnectionStringBuilderHelper.ConvertToInt32 (value);
- break;
- case "PASSWORD" :
- if (value == null) {
- _password = DEF_PASSWORD;
- base.Remove (mappedKey);
- } else
- this.Password = value.ToString ();
- break;
- case "PERSIST SECURITY INFO" :
- if (value == null) {
- _persistSecurityInfo = DEF_PERSISTSECURITYINFO;
- base.Remove (mappedKey);
- } else if (DbConnectionStringBuilderHelper.ConvertToBoolean (value))
- throw new NotImplementedException ("Persisting security info" +
- " is not yet implemented");
- break;
- case "POOLING" :
- if (value == null) {
- _pooling = DEF_POOLING;
- base.Remove (mappedKey);
- } else
- this.Pooling = DbConnectionStringBuilderHelper.ConvertToBoolean (value);
- break;
- case "USER ID" :
- if (value == null) {
- _userID = DEF_USERID;
- base.Remove (mappedKey);
- } else
- this.UserID = value.ToString ();
- break;
- case "WORKSTATION ID" :
- if (value == null) {
- _workstationID = DEF_WORKSTATIONID;
- base.Remove (mappedKey);
- } else
- this.WorkstationID = value.ToString ();
- break;
- default :
- throw new ArgumentException("Keyword not supported :" + key);
- }
- }
- #endregion // Private Methods
- }
-
-
- }
- #endif // NET_2_0
|