9df61cecd01171717fccb63fcbe2b451017dd970
[mono.git] / mcs / class / referencesource / System.Data / System / Data / SqlClient / SqlConnectionStringBuilder.cs
1 //------------------------------------------------------------------------------
2 // <copyright file="SqlConnectionStringBuilder.cs" company="Microsoft">
3 //      Copyright (c) Microsoft Corporation.  All rights reserved.
4 // </copyright>
5 // <owner current="true" primary="true">Microsoft</owner>
6 // <owner current="true" primary="false">Microsoft</owner>
7 //------------------------------------------------------------------------------
8
9     using System;
10     using System.Collections;
11     using System.Collections.Generic;
12     using System.ComponentModel;
13     using System.Data;
14     using System.Data.Common;
15     using System.Diagnostics;
16     using System.Globalization;
17     using System.Runtime.Serialization;
18     using System.Security.Permissions;
19     using System.Text;
20     using System.Diagnostics.CodeAnalysis;
21
22 namespace System.Data.SqlClient {
23
24     [DefaultProperty("DataSource")]
25     [System.ComponentModel.TypeConverterAttribute(typeof(SqlConnectionStringBuilder.SqlConnectionStringBuilderConverter))]
26     public sealed class SqlConnectionStringBuilder : DbConnectionStringBuilder {
27
28         private enum Keywords { // specific ordering for ConnectionString output construction
29 //            NamedConnection,
30
31             DataSource,
32             FailoverPartner,
33             AttachDBFilename,
34             InitialCatalog,
35             IntegratedSecurity,
36             PersistSecurityInfo,
37             UserID,
38             Password,
39
40             Enlist,
41             Pooling,
42             MinPoolSize,
43             MaxPoolSize,
44
45             AsynchronousProcessing,
46             ConnectionReset,
47             MultipleActiveResultSets,
48             Replication,
49
50             ConnectTimeout,
51             Encrypt,
52             TrustServerCertificate,
53             LoadBalanceTimeout,
54             NetworkLibrary,
55             PacketSize,
56             TypeSystemVersion,
57
58             Authentication,
59
60             ApplicationName,
61             CurrentLanguage,
62             WorkstationID,
63
64             UserInstance,
65
66             ContextConnection,
67
68             TransactionBinding,
69
70             ApplicationIntent,
71
72             MultiSubnetFailover,
73
74             ConnectRetryCount,
75
76             ConnectRetryInterval,
77
78             ColumnEncryptionSetting,
79
80             // keep the count value last
81             KeywordsCount
82         }
83
84         internal const int KeywordsCount = (int)Keywords.KeywordsCount;
85
86         private static readonly string[] _validKeywords;
87         private static readonly Dictionary<string,Keywords> _keywords;
88
89         private ApplicationIntent _applicationIntent = DbConnectionStringDefaults.ApplicationIntent;
90         private string _applicationName   = DbConnectionStringDefaults.ApplicationName;
91         private string _attachDBFilename  = DbConnectionStringDefaults.AttachDBFilename;
92         private string _currentLanguage   = DbConnectionStringDefaults.CurrentLanguage;
93         private string _dataSource        = DbConnectionStringDefaults.DataSource;
94         private string _failoverPartner   = DbConnectionStringDefaults.FailoverPartner;
95         private string _initialCatalog    = DbConnectionStringDefaults.InitialCatalog;
96 //      private string _namedConnection   = DbConnectionStringDefaults.NamedConnection;
97         private string _networkLibrary    = DbConnectionStringDefaults.NetworkLibrary;
98         private string _password          = DbConnectionStringDefaults.Password;
99         private string _transactionBinding = DbConnectionStringDefaults.TransactionBinding;
100         private string _typeSystemVersion = DbConnectionStringDefaults.TypeSystemVersion;
101         private string _userID            = DbConnectionStringDefaults.UserID;
102         private string _workstationID     = DbConnectionStringDefaults.WorkstationID;
103
104         private int _connectTimeout      = DbConnectionStringDefaults.ConnectTimeout;
105         private int _loadBalanceTimeout  = DbConnectionStringDefaults.LoadBalanceTimeout;
106         private int _maxPoolSize         = DbConnectionStringDefaults.MaxPoolSize;
107         private int _minPoolSize         = DbConnectionStringDefaults.MinPoolSize;
108         private int _packetSize          = DbConnectionStringDefaults.PacketSize;
109         private int _connectRetryCount   = DbConnectionStringDefaults.ConnectRetryCount;
110         private int _connectRetryInterval = DbConnectionStringDefaults.ConnectRetryInterval;
111
112         private bool _asynchronousProcessing       = DbConnectionStringDefaults.AsynchronousProcessing;
113         private bool _connectionReset              = DbConnectionStringDefaults.ConnectionReset;
114         private bool _contextConnection            = DbConnectionStringDefaults.ContextConnection;
115         private bool _encrypt                      = DbConnectionStringDefaults.Encrypt;
116         private bool _trustServerCertificate       = DbConnectionStringDefaults.TrustServerCertificate;
117         private bool _enlist                       = DbConnectionStringDefaults.Enlist;
118         private bool _integratedSecurity           = DbConnectionStringDefaults.IntegratedSecurity;
119         private bool _multipleActiveResultSets     = DbConnectionStringDefaults.MultipleActiveResultSets;
120         private bool _multiSubnetFailover          = DbConnectionStringDefaults.MultiSubnetFailover;
121         private bool _persistSecurityInfo          = DbConnectionStringDefaults.PersistSecurityInfo;
122         private bool _pooling                      = DbConnectionStringDefaults.Pooling;
123         private bool _replication                  = DbConnectionStringDefaults.Replication;
124         private bool _userInstance                 = DbConnectionStringDefaults.UserInstance;
125         private SqlAuthenticationMethod _authentication     = DbConnectionStringDefaults.Authentication;
126         private SqlConnectionColumnEncryptionSetting _columnEncryptionSetting = DbConnectionStringDefaults.ColumnEncryptionSetting;
127
128         static SqlConnectionStringBuilder() {
129             string[] validKeywords = new string[KeywordsCount];
130             validKeywords[(int)Keywords.ApplicationIntent]        = DbConnectionStringKeywords.ApplicationIntent;
131             validKeywords[(int)Keywords.ApplicationName]          = DbConnectionStringKeywords.ApplicationName;
132             validKeywords[(int)Keywords.AsynchronousProcessing]   = DbConnectionStringKeywords.AsynchronousProcessing;
133             validKeywords[(int)Keywords.AttachDBFilename]         = DbConnectionStringKeywords.AttachDBFilename;
134             validKeywords[(int)Keywords.ConnectionReset]          = DbConnectionStringKeywords.ConnectionReset;
135             validKeywords[(int)Keywords.ContextConnection]        = DbConnectionStringKeywords.ContextConnection;
136             validKeywords[(int)Keywords.ConnectTimeout]           = DbConnectionStringKeywords.ConnectTimeout;
137             validKeywords[(int)Keywords.CurrentLanguage]          = DbConnectionStringKeywords.CurrentLanguage;
138             validKeywords[(int)Keywords.DataSource]               = DbConnectionStringKeywords.DataSource;
139             validKeywords[(int)Keywords.Encrypt]                  = DbConnectionStringKeywords.Encrypt;
140             validKeywords[(int)Keywords.Enlist]                   = DbConnectionStringKeywords.Enlist;
141             validKeywords[(int)Keywords.FailoverPartner]          = DbConnectionStringKeywords.FailoverPartner;
142             validKeywords[(int)Keywords.InitialCatalog]           = DbConnectionStringKeywords.InitialCatalog;
143             validKeywords[(int)Keywords.IntegratedSecurity]       = DbConnectionStringKeywords.IntegratedSecurity;
144             validKeywords[(int)Keywords.LoadBalanceTimeout]       = DbConnectionStringKeywords.LoadBalanceTimeout;
145             validKeywords[(int)Keywords.MaxPoolSize]              = DbConnectionStringKeywords.MaxPoolSize;
146             validKeywords[(int)Keywords.MinPoolSize]              = DbConnectionStringKeywords.MinPoolSize;
147             validKeywords[(int)Keywords.MultipleActiveResultSets] = DbConnectionStringKeywords.MultipleActiveResultSets;
148             validKeywords[(int)Keywords.MultiSubnetFailover]      = DbConnectionStringKeywords.MultiSubnetFailover;
149 //          validKeywords[(int)Keywords.NamedConnection]          = DbConnectionStringKeywords.NamedConnection;
150             validKeywords[(int)Keywords.NetworkLibrary]           = DbConnectionStringKeywords.NetworkLibrary;
151             validKeywords[(int)Keywords.PacketSize]               = DbConnectionStringKeywords.PacketSize;
152             validKeywords[(int)Keywords.Password]                 = DbConnectionStringKeywords.Password;
153             validKeywords[(int)Keywords.PersistSecurityInfo]      = DbConnectionStringKeywords.PersistSecurityInfo;
154             validKeywords[(int)Keywords.Pooling]                  = DbConnectionStringKeywords.Pooling;
155             validKeywords[(int)Keywords.Replication]              = DbConnectionStringKeywords.Replication;
156             validKeywords[(int)Keywords.TransactionBinding]       = DbConnectionStringKeywords.TransactionBinding;
157             validKeywords[(int)Keywords.TrustServerCertificate]   = DbConnectionStringKeywords.TrustServerCertificate;
158             validKeywords[(int)Keywords.TypeSystemVersion]        = DbConnectionStringKeywords.TypeSystemVersion;
159             validKeywords[(int)Keywords.UserID]                   = DbConnectionStringKeywords.UserID;
160             validKeywords[(int)Keywords.UserInstance]             = DbConnectionStringKeywords.UserInstance;
161             validKeywords[(int)Keywords.WorkstationID]            = DbConnectionStringKeywords.WorkstationID;
162             validKeywords[(int)Keywords.ConnectRetryCount]        = DbConnectionStringKeywords.ConnectRetryCount;
163             validKeywords[(int)Keywords.ConnectRetryInterval]     = DbConnectionStringKeywords.ConnectRetryInterval;
164             validKeywords[(int)Keywords.Authentication]           = DbConnectionStringKeywords.Authentication;
165             validKeywords[(int)Keywords.ColumnEncryptionSetting] = DbConnectionStringKeywords.ColumnEncryptionSetting;
166            _validKeywords = validKeywords;
167
168             Dictionary<string, Keywords> hash = new Dictionary<string, Keywords>(KeywordsCount + SqlConnectionString.SynonymCount, StringComparer.OrdinalIgnoreCase);
169             hash.Add(DbConnectionStringKeywords.ApplicationIntent,        Keywords.ApplicationIntent);
170             hash.Add(DbConnectionStringKeywords.ApplicationName,          Keywords.ApplicationName);
171             hash.Add(DbConnectionStringKeywords.AsynchronousProcessing,   Keywords.AsynchronousProcessing);
172             hash.Add(DbConnectionStringKeywords.AttachDBFilename,         Keywords.AttachDBFilename);
173             hash.Add(DbConnectionStringKeywords.ConnectTimeout,           Keywords.ConnectTimeout);
174             hash.Add(DbConnectionStringKeywords.ConnectionReset,          Keywords.ConnectionReset);
175             hash.Add(DbConnectionStringKeywords.ContextConnection,        Keywords.ContextConnection);
176             hash.Add(DbConnectionStringKeywords.CurrentLanguage,          Keywords.CurrentLanguage);
177             hash.Add(DbConnectionStringKeywords.DataSource,               Keywords.DataSource);
178             hash.Add(DbConnectionStringKeywords.Encrypt,                  Keywords.Encrypt);
179             hash.Add(DbConnectionStringKeywords.Enlist,                   Keywords.Enlist);
180             hash.Add(DbConnectionStringKeywords.FailoverPartner,          Keywords.FailoverPartner);
181             hash.Add(DbConnectionStringKeywords.InitialCatalog,           Keywords.InitialCatalog);
182             hash.Add(DbConnectionStringKeywords.IntegratedSecurity,       Keywords.IntegratedSecurity);
183             hash.Add(DbConnectionStringKeywords.LoadBalanceTimeout,       Keywords.LoadBalanceTimeout);
184             hash.Add(DbConnectionStringKeywords.MultipleActiveResultSets, Keywords.MultipleActiveResultSets);
185             hash.Add(DbConnectionStringKeywords.MaxPoolSize,              Keywords.MaxPoolSize);
186             hash.Add(DbConnectionStringKeywords.MinPoolSize,              Keywords.MinPoolSize);
187             hash.Add(DbConnectionStringKeywords.MultiSubnetFailover,      Keywords.MultiSubnetFailover);
188 //          hash.Add(DbConnectionStringKeywords.NamedConnection,          Keywords.NamedConnection);
189             hash.Add(DbConnectionStringKeywords.NetworkLibrary,           Keywords.NetworkLibrary);
190             hash.Add(DbConnectionStringKeywords.PacketSize,               Keywords.PacketSize);
191             hash.Add(DbConnectionStringKeywords.Password,                 Keywords.Password);
192             hash.Add(DbConnectionStringKeywords.PersistSecurityInfo,      Keywords.PersistSecurityInfo);
193             hash.Add(DbConnectionStringKeywords.Pooling,                  Keywords.Pooling);
194             hash.Add(DbConnectionStringKeywords.Replication,              Keywords.Replication);
195             hash.Add(DbConnectionStringKeywords.TransactionBinding,       Keywords.TransactionBinding);
196             hash.Add(DbConnectionStringKeywords.TrustServerCertificate,   Keywords.TrustServerCertificate);
197             hash.Add(DbConnectionStringKeywords.TypeSystemVersion,        Keywords.TypeSystemVersion);
198             hash.Add(DbConnectionStringKeywords.UserID,                   Keywords.UserID);
199             hash.Add(DbConnectionStringKeywords.UserInstance,             Keywords.UserInstance);
200             hash.Add(DbConnectionStringKeywords.WorkstationID,            Keywords.WorkstationID);
201             hash.Add(DbConnectionStringKeywords.ConnectRetryCount,        Keywords.ConnectRetryCount);
202             hash.Add(DbConnectionStringKeywords.ConnectRetryInterval,     Keywords.ConnectRetryInterval);
203             hash.Add(DbConnectionStringKeywords.Authentication,           Keywords.Authentication);
204             hash.Add(DbConnectionStringKeywords.ColumnEncryptionSetting, Keywords.ColumnEncryptionSetting);
205             hash.Add(DbConnectionStringSynonyms.APP,                      Keywords.ApplicationName);
206             hash.Add(DbConnectionStringSynonyms.Async,                    Keywords.AsynchronousProcessing);
207             hash.Add(DbConnectionStringSynonyms.EXTENDEDPROPERTIES,       Keywords.AttachDBFilename);
208             hash.Add(DbConnectionStringSynonyms.INITIALFILENAME,          Keywords.AttachDBFilename);
209             hash.Add(DbConnectionStringSynonyms.CONNECTIONTIMEOUT,        Keywords.ConnectTimeout);
210             hash.Add(DbConnectionStringSynonyms.TIMEOUT,                  Keywords.ConnectTimeout);
211             hash.Add(DbConnectionStringSynonyms.LANGUAGE,                 Keywords.CurrentLanguage);
212             hash.Add(DbConnectionStringSynonyms.ADDR,                     Keywords.DataSource);
213             hash.Add(DbConnectionStringSynonyms.ADDRESS,                  Keywords.DataSource);
214             hash.Add(DbConnectionStringSynonyms.NETWORKADDRESS,           Keywords.DataSource);
215             hash.Add(DbConnectionStringSynonyms.SERVER,                   Keywords.DataSource);
216             hash.Add(DbConnectionStringSynonyms.DATABASE,                 Keywords.InitialCatalog);
217             hash.Add(DbConnectionStringSynonyms.TRUSTEDCONNECTION,        Keywords.IntegratedSecurity);
218             hash.Add(DbConnectionStringSynonyms.ConnectionLifetime,       Keywords.LoadBalanceTimeout);
219             hash.Add(DbConnectionStringSynonyms.NET,                      Keywords.NetworkLibrary);
220             hash.Add(DbConnectionStringSynonyms.NETWORK,                  Keywords.NetworkLibrary);
221             hash.Add(DbConnectionStringSynonyms.Pwd,                      Keywords.Password);
222             hash.Add(DbConnectionStringSynonyms.PERSISTSECURITYINFO,      Keywords.PersistSecurityInfo);
223             hash.Add(DbConnectionStringSynonyms.UID,                      Keywords.UserID);
224             hash.Add(DbConnectionStringSynonyms.User,                     Keywords.UserID);
225             hash.Add(DbConnectionStringSynonyms.WSID,                     Keywords.WorkstationID);
226             Debug.Assert((KeywordsCount + SqlConnectionString.SynonymCount) == hash.Count, "initial expected size is incorrect");
227             _keywords = hash;
228
229         }
230
231         public SqlConnectionStringBuilder() : this((string)null) {
232         }
233
234         public SqlConnectionStringBuilder(string connectionString) : base() {
235             if (!ADP.IsEmpty(connectionString)) {
236                 ConnectionString = connectionString;
237             }
238         }
239
240         public override object this[string keyword] {
241             get {
242                 Keywords index = GetIndex(keyword);
243                 return GetAt(index);
244             }
245             set {
246                 if (null != value) {
247                     Keywords index = GetIndex(keyword);
248                     switch(index) {
249                     case Keywords.ApplicationIntent:        this.ApplicationIntent = ConvertToApplicationIntent(keyword, value); break;
250                     case Keywords.ApplicationName:          ApplicationName = ConvertToString(value); break;
251                     case Keywords.AttachDBFilename:         AttachDBFilename = ConvertToString(value); break;
252                     case Keywords.CurrentLanguage:          CurrentLanguage = ConvertToString(value); break;
253                     case Keywords.DataSource:               DataSource = ConvertToString(value); break;
254                     case Keywords.FailoverPartner:          FailoverPartner = ConvertToString(value); break;
255                     case Keywords.InitialCatalog:           InitialCatalog = ConvertToString(value); break;
256 //                  case Keywords.NamedConnection:          NamedConnection = ConvertToString(value); break;
257                     case Keywords.NetworkLibrary:           NetworkLibrary = ConvertToString(value); break;
258                     case Keywords.Password:                 Password = ConvertToString(value); break;
259                     case Keywords.UserID:                   UserID = ConvertToString(value); break;
260                     case Keywords.TransactionBinding:       TransactionBinding = ConvertToString(value); break;
261                     case Keywords.TypeSystemVersion:        TypeSystemVersion = ConvertToString(value); break;
262                     case Keywords.WorkstationID:            WorkstationID = ConvertToString(value); break;
263
264                     case Keywords.ConnectTimeout:           ConnectTimeout = ConvertToInt32(value); break;
265                     case Keywords.LoadBalanceTimeout:       LoadBalanceTimeout = ConvertToInt32(value); break;
266                     case Keywords.MaxPoolSize:              MaxPoolSize = ConvertToInt32(value); break;
267                     case Keywords.MinPoolSize:              MinPoolSize = ConvertToInt32(value); break;
268                     case Keywords.PacketSize:               PacketSize = ConvertToInt32(value); break;
269
270                     case Keywords.IntegratedSecurity:       IntegratedSecurity = ConvertToIntegratedSecurity(value); break;
271
272                     case Keywords.Authentication:           Authentication = ConvertToAuthenticationType(keyword, value); break;
273                     case Keywords.ColumnEncryptionSetting: ColumnEncryptionSetting = ConvertToColumnEncryptionSetting(keyword, value); break;
274                     case Keywords.AsynchronousProcessing:   AsynchronousProcessing = ConvertToBoolean(value); break;
275 #pragma warning disable 618 // Obsolete ConnectionReset
276                     case Keywords.ConnectionReset:          ConnectionReset = ConvertToBoolean(value); break;
277 #pragma warning restore 618
278                     case Keywords.ContextConnection:        ContextConnection = ConvertToBoolean(value); break;
279                     case Keywords.Encrypt:                  Encrypt = ConvertToBoolean(value); break;
280                     case Keywords.TrustServerCertificate:   TrustServerCertificate = ConvertToBoolean(value); break;
281                     case Keywords.Enlist:                   Enlist = ConvertToBoolean(value); break;
282                     case Keywords.MultipleActiveResultSets: MultipleActiveResultSets = ConvertToBoolean(value); break;
283                     case Keywords.MultiSubnetFailover:      MultiSubnetFailover = ConvertToBoolean(value); break;
284                     case Keywords.PersistSecurityInfo:      PersistSecurityInfo = ConvertToBoolean(value); break;
285                     case Keywords.Pooling:                  Pooling = ConvertToBoolean(value); break;
286                     case Keywords.Replication:              Replication = ConvertToBoolean(value); break;
287                     case Keywords.UserInstance:             UserInstance = ConvertToBoolean(value); break;
288                     case Keywords.ConnectRetryCount:        ConnectRetryCount = ConvertToInt32(value); break;
289                     case Keywords.ConnectRetryInterval:     ConnectRetryInterval = ConvertToInt32(value); break;
290
291                     default:
292                         Debug.Assert(false, "unexpected keyword");
293                         throw ADP.KeywordNotSupported(keyword);
294                     }
295                 }
296                 else {
297                     Remove(keyword);
298                 }
299             }
300         }
301
302         [DisplayName(DbConnectionStringKeywords.ApplicationIntent)]
303         [ResCategoryAttribute(Res.DataCategory_Initialization)]
304         [ResDescriptionAttribute(Res.DbConnectionString_ApplicationIntent)]
305         [RefreshPropertiesAttribute(RefreshProperties.All)]
306         public ApplicationIntent ApplicationIntent {
307             get { return _applicationIntent; }
308             set {
309                 if (!DbConnectionStringBuilderUtil.IsValidApplicationIntentValue(value)) {
310                     throw ADP.InvalidEnumerationValue(typeof(ApplicationIntent), (int)value);
311                 }
312
313                 SetApplicationIntentValue(value);
314                 _applicationIntent = value;
315             }
316         }
317
318         [DisplayName(DbConnectionStringKeywords.ApplicationName)]
319         [ResCategoryAttribute(Res.DataCategory_Context)]
320         [ResDescriptionAttribute(Res.DbConnectionString_ApplicationName)]
321         [RefreshPropertiesAttribute(RefreshProperties.All)]
322         public string ApplicationName {
323             get { return _applicationName; }
324             set {
325                 SetValue(DbConnectionStringKeywords.ApplicationName, value);
326                 _applicationName = value;
327             }
328         }
329
330         [DisplayName(DbConnectionStringKeywords.AsynchronousProcessing)]
331         [ResCategoryAttribute(Res.DataCategory_Initialization)]
332         [ResDescriptionAttribute(Res.DbConnectionString_AsynchronousProcessing)]
333         [RefreshPropertiesAttribute(RefreshProperties.All)]
334         public bool AsynchronousProcessing {
335             get { return _asynchronousProcessing; }
336             set {
337                 SetValue(DbConnectionStringKeywords.AsynchronousProcessing, value);
338                 _asynchronousProcessing = value;
339             }
340         }
341
342         [DisplayName(DbConnectionStringKeywords.AttachDBFilename)]
343         [ResCategoryAttribute(Res.DataCategory_Source)]
344         [ResDescriptionAttribute(Res.DbConnectionString_AttachDBFilename)]
345         [RefreshPropertiesAttribute(RefreshProperties.All)]
346         // 
347         [Editor("System.Windows.Forms.Design.FileNameEditor, " + AssemblyRef.SystemDesign, "System.Drawing.Design.UITypeEditor, " + AssemblyRef.SystemDrawing)]
348         public string AttachDBFilename {
349             get { return _attachDBFilename; }
350             set {
351                 SetValue(DbConnectionStringKeywords.AttachDBFilename, value);
352                 _attachDBFilename = value;
353             }
354         }
355
356         [Browsable(false)]
357         [DisplayName(DbConnectionStringKeywords.ConnectionReset)]
358         [Obsolete("ConnectionReset has been deprecated.  SqlConnection will ignore the 'connection reset' keyword and always reset the connection")] // SQLPT 41700
359         [ResCategoryAttribute(Res.DataCategory_Pooling)]
360         [ResDescriptionAttribute(Res.DbConnectionString_ConnectionReset)]
361         [RefreshPropertiesAttribute(RefreshProperties.All)]
362         public bool ConnectionReset {
363             get { return _connectionReset; }
364             set {
365                 SetValue(DbConnectionStringKeywords.ConnectionReset, value);
366                 _connectionReset = value;
367             }
368         }
369
370         [DisplayName(DbConnectionStringKeywords.ContextConnection)]
371         [ResCategoryAttribute(Res.DataCategory_Source)]
372         [ResDescriptionAttribute(Res.DbConnectionString_ContextConnection)]
373         [RefreshPropertiesAttribute(RefreshProperties.All)]
374         public bool ContextConnection {
375             get { return _contextConnection; }
376             set {
377                 SetValue(DbConnectionStringKeywords.ContextConnection, value);
378                 _contextConnection = value;
379             }
380         }
381
382         [DisplayName(DbConnectionStringKeywords.ConnectTimeout)]
383         [ResCategoryAttribute(Res.DataCategory_Initialization)]
384         [ResDescriptionAttribute(Res.DbConnectionString_ConnectTimeout)]
385         [RefreshPropertiesAttribute(RefreshProperties.All)]
386         public int ConnectTimeout {
387             get { return _connectTimeout; }
388             set {
389                 if (value < 0) {
390                     throw ADP.InvalidConnectionOptionValue(DbConnectionStringKeywords.ConnectTimeout);
391                 }
392                 SetValue(DbConnectionStringKeywords.ConnectTimeout, value);
393                 _connectTimeout = value;
394             }
395         }
396
397         [DisplayName(DbConnectionStringKeywords.CurrentLanguage)]
398         [ResCategoryAttribute(Res.DataCategory_Initialization)]
399         [ResDescriptionAttribute(Res.DbConnectionString_CurrentLanguage)]
400         [RefreshPropertiesAttribute(RefreshProperties.All)]
401         public string CurrentLanguage {
402             get { return _currentLanguage; }
403             set {
404                 SetValue(DbConnectionStringKeywords.CurrentLanguage, value);
405                 _currentLanguage = value;
406             }
407         }
408
409         [DisplayName(DbConnectionStringKeywords.DataSource)]
410         [ResCategoryAttribute(Res.DataCategory_Source)]
411         [ResDescriptionAttribute(Res.DbConnectionString_DataSource)]
412         [RefreshProperties(RefreshProperties.All)]
413         [TypeConverter(typeof(SqlDataSourceConverter))]
414         public string DataSource {
415             get { return _dataSource; }
416             set {
417                 SetValue(DbConnectionStringKeywords.DataSource, value);
418                 _dataSource = value;
419             }
420         }
421
422         [DisplayName(DbConnectionStringKeywords.Encrypt)]
423         [ResCategoryAttribute(Res.DataCategory_Security)]
424         [ResDescriptionAttribute(Res.DbConnectionString_Encrypt)]
425         [RefreshPropertiesAttribute(RefreshProperties.All)]
426         public bool Encrypt {
427             get { return _encrypt; }
428             set {
429                 SetValue(DbConnectionStringKeywords.Encrypt, value);
430                 _encrypt = value;
431             }
432         }
433
434         [DisplayName(DbConnectionStringKeywords.ColumnEncryptionSetting)]
435         [ResCategoryAttribute(Res.DataCategory_Security)]
436         [ResDescriptionAttribute(Res.TCE_DbConnectionString_ColumnEncryptionSetting)]
437         [RefreshPropertiesAttribute(RefreshProperties.All)]
438         public SqlConnectionColumnEncryptionSetting ColumnEncryptionSetting {
439             get { return _columnEncryptionSetting; }
440             set {
441                 if (!DbConnectionStringBuilderUtil.IsValidColumnEncryptionSetting(value)) {
442                     throw ADP.InvalidEnumerationValue(typeof(SqlConnectionColumnEncryptionSetting), (int)value);
443                 }
444
445                  SetColumnEncryptionSettingValue(value);
446                 _columnEncryptionSetting = value;
447             }
448         }
449         
450         [DisplayName(DbConnectionStringKeywords.TrustServerCertificate)]
451         [ResCategoryAttribute(Res.DataCategory_Security)]
452         [ResDescriptionAttribute(Res.DbConnectionString_TrustServerCertificate)]
453         [RefreshPropertiesAttribute(RefreshProperties.All)]
454         public bool TrustServerCertificate {
455             get { return _trustServerCertificate; }
456             set {
457                 SetValue(DbConnectionStringKeywords.TrustServerCertificate, value);
458                 _trustServerCertificate = value;
459             }
460         }
461         
462         [DisplayName(DbConnectionStringKeywords.Enlist)]
463         [ResCategoryAttribute(Res.DataCategory_Pooling)]
464         [ResDescriptionAttribute(Res.DbConnectionString_Enlist)]
465         [RefreshPropertiesAttribute(RefreshProperties.All)]
466         public bool Enlist {
467             get { return _enlist; }
468             set {
469                 SetValue(DbConnectionStringKeywords.Enlist, value);
470                 _enlist = value;
471             }
472         }
473
474         [DisplayName(DbConnectionStringKeywords.FailoverPartner)]
475         [ResCategoryAttribute(Res.DataCategory_Source)]
476         [ResDescriptionAttribute(Res.DbConnectionString_FailoverPartner)]
477         [RefreshPropertiesAttribute(RefreshProperties.All)]
478         [TypeConverter(typeof(SqlDataSourceConverter))]
479         public string FailoverPartner {
480             get { return _failoverPartner; }
481             set {
482                 SetValue(DbConnectionStringKeywords.FailoverPartner, value);
483                 _failoverPartner= value;
484             }
485         }
486
487         [DisplayName(DbConnectionStringKeywords.InitialCatalog)]
488         [ResCategoryAttribute(Res.DataCategory_Source)]
489         [ResDescriptionAttribute(Res.DbConnectionString_InitialCatalog)]
490         [RefreshPropertiesAttribute(RefreshProperties.All)]
491         [TypeConverter(typeof(SqlInitialCatalogConverter))]
492         public string InitialCatalog {
493             get { return _initialCatalog; }
494             set {
495                 SetValue(DbConnectionStringKeywords.InitialCatalog, value);
496                 _initialCatalog = value;
497             }
498         }
499
500         [DisplayName(DbConnectionStringKeywords.IntegratedSecurity)]
501         [ResCategoryAttribute(Res.DataCategory_Security)]
502         [ResDescriptionAttribute(Res.DbConnectionString_IntegratedSecurity)]
503         [RefreshPropertiesAttribute(RefreshProperties.All)]
504         public bool IntegratedSecurity {
505             get { return _integratedSecurity; }
506             set {
507                 SetValue(DbConnectionStringKeywords.IntegratedSecurity, value);
508                 _integratedSecurity = value;
509             }
510         }
511
512         [DisplayName(DbConnectionStringKeywords.Authentication)]
513         [ResCategoryAttribute(Res.DataCategory_Security)]
514         [ResDescriptionAttribute(Res.DbConnectionString_Authentication)]
515         [RefreshPropertiesAttribute(RefreshProperties.All)]
516         public SqlAuthenticationMethod Authentication {
517             get { return _authentication; }
518             set {
519                 if (!DbConnectionStringBuilderUtil.IsValidAuthenticationTypeValue(value)) {
520                     throw ADP.InvalidEnumerationValue(typeof(SqlAuthenticationMethod), (int)value);
521                 }
522
523                 SetAuthenticationValue(value);
524                 _authentication = value;
525             }
526         }
527
528         [DisplayName(DbConnectionStringKeywords.LoadBalanceTimeout)]
529         [ResCategoryAttribute(Res.DataCategory_Pooling)]
530         [ResDescriptionAttribute(Res.DbConnectionString_LoadBalanceTimeout)]
531         [RefreshPropertiesAttribute(RefreshProperties.All)]
532         public int LoadBalanceTimeout {
533             get { return _loadBalanceTimeout; }
534             set {
535                 if (value < 0) {
536                     throw ADP.InvalidConnectionOptionValue(DbConnectionStringKeywords.LoadBalanceTimeout);
537                 }
538                 SetValue(DbConnectionStringKeywords.LoadBalanceTimeout, value);
539                 _loadBalanceTimeout = value;
540             }
541         }
542
543         [DisplayName(DbConnectionStringKeywords.MaxPoolSize)]
544         [ResCategoryAttribute(Res.DataCategory_Pooling)]
545         [ResDescriptionAttribute(Res.DbConnectionString_MaxPoolSize)]
546         [RefreshPropertiesAttribute(RefreshProperties.All)]
547         public int MaxPoolSize {
548             get { return _maxPoolSize; }
549             set {
550                 if (value < 1) {
551                     throw ADP.InvalidConnectionOptionValue(DbConnectionStringKeywords.MaxPoolSize);
552                 }
553                 SetValue(DbConnectionStringKeywords.MaxPoolSize, value);
554                 _maxPoolSize = value;
555             }
556         }
557
558         [DisplayName(DbConnectionStringKeywords.ConnectRetryCount)]
559         [ResCategoryAttribute(Res.DataCategory_ConnectionResilency)]
560         [ResDescriptionAttribute(Res.DbConnectionString_ConnectRetryCount)]
561         [RefreshPropertiesAttribute(RefreshProperties.All)]
562         public int ConnectRetryCount {
563             get { return _connectRetryCount; }
564             set {
565                 if ((value < 0) || (value>255)) {
566                     throw ADP.InvalidConnectionOptionValue(DbConnectionStringKeywords.ConnectRetryCount);
567                 }
568                 SetValue(DbConnectionStringKeywords.ConnectRetryCount, value);
569                 _connectRetryCount = value;
570             }
571         }
572
573         [DisplayName(DbConnectionStringKeywords.ConnectRetryInterval)]
574         [ResCategoryAttribute(Res.DataCategory_ConnectionResilency)]
575         [ResDescriptionAttribute(Res.DbConnectionString_ConnectRetryInterval)]
576         [RefreshPropertiesAttribute(RefreshProperties.All)]
577         public int ConnectRetryInterval {
578             get { return _connectRetryInterval; }
579             set {
580                 if ((value < 1) || (value > 60)) {
581                     throw ADP.InvalidConnectionOptionValue(DbConnectionStringKeywords.ConnectRetryInterval);
582                 }
583                 SetValue(DbConnectionStringKeywords.ConnectRetryInterval, value);
584                 _connectRetryInterval = value;
585             }
586         }
587
588
589
590         [DisplayName(DbConnectionStringKeywords.MinPoolSize)]
591         [ResCategoryAttribute(Res.DataCategory_Pooling)]
592         [ResDescriptionAttribute(Res.DbConnectionString_MinPoolSize)]
593         [RefreshPropertiesAttribute(RefreshProperties.All)]
594         public int MinPoolSize {
595             get { return _minPoolSize; }
596             set {
597                 if (value < 0) {
598                     throw ADP.InvalidConnectionOptionValue(DbConnectionStringKeywords.MinPoolSize);
599                 }
600                 SetValue(DbConnectionStringKeywords.MinPoolSize, value);
601                 _minPoolSize = value;
602             }
603         }
604
605         [DisplayName(DbConnectionStringKeywords.MultipleActiveResultSets)]
606         [ResCategoryAttribute(Res.DataCategory_Advanced)]
607         [ResDescriptionAttribute(Res.DbConnectionString_MultipleActiveResultSets)]
608         [RefreshPropertiesAttribute(RefreshProperties.All)]
609         public bool MultipleActiveResultSets {
610             get { return _multipleActiveResultSets; }
611             set {
612                 SetValue(DbConnectionStringKeywords.MultipleActiveResultSets, value);
613                 _multipleActiveResultSets = value;
614             }
615         }
616
617         [SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", Justification = "Reviewed and Approved by UE")]
618         [DisplayName(DbConnectionStringKeywords.MultiSubnetFailover)]
619         [ResCategoryAttribute(Res.DataCategory_Source)]
620         [ResDescriptionAttribute(Res.DbConnectionString_MultiSubnetFailover)]
621         [RefreshPropertiesAttribute(RefreshProperties.All)]
622         public bool MultiSubnetFailover {
623             get { return _multiSubnetFailover; }
624             set {
625                 SetValue(DbConnectionStringKeywords.MultiSubnetFailover, value);
626                 _multiSubnetFailover = value;
627             }
628         }
629 /*
630         [DisplayName(DbConnectionStringKeywords.NamedConnection)]
631         [ResCategoryAttribute(Res.DataCategory_NamedConnectionString)]
632         [ResDescriptionAttribute(Res.DbConnectionString_NamedConnection)]
633         [RefreshPropertiesAttribute(RefreshProperties.All)]
634         [TypeConverter(typeof(NamedConnectionStringConverter))]
635         public string NamedConnection {
636             get { return _namedConnection; }
637             set {
638                 SetValue(DbConnectionStringKeywords.NamedConnection, value);
639                 _namedConnection = value;
640             }
641         }
642 */
643         [DisplayName(DbConnectionStringKeywords.NetworkLibrary)]
644         [ResCategoryAttribute(Res.DataCategory_Advanced)]
645         [ResDescriptionAttribute(Res.DbConnectionString_NetworkLibrary)]
646         [RefreshPropertiesAttribute(RefreshProperties.All)]
647         [TypeConverter(typeof(NetworkLibraryConverter))]
648         public string NetworkLibrary {
649             get { return _networkLibrary; }
650             set {
651                 if (null != value) {
652                     switch(value.Trim().ToLower(CultureInfo.InvariantCulture)) {
653                     case SqlConnectionString.NETLIB.AppleTalk:
654                         value = SqlConnectionString.NETLIB.AppleTalk;
655                         break;
656                     case SqlConnectionString.NETLIB.BanyanVines:
657                         value = SqlConnectionString.NETLIB.BanyanVines;
658                         break;
659                     case SqlConnectionString.NETLIB.IPXSPX:
660                         value = SqlConnectionString.NETLIB.IPXSPX;
661                         break;
662                     case SqlConnectionString.NETLIB.Multiprotocol:
663                         value = SqlConnectionString.NETLIB.Multiprotocol;
664                         break;
665                     case SqlConnectionString.NETLIB.NamedPipes:
666                         value = SqlConnectionString.NETLIB.NamedPipes;
667                         break;
668                     case SqlConnectionString.NETLIB.SharedMemory:
669                         value = SqlConnectionString.NETLIB.SharedMemory;
670                         break;
671                     case SqlConnectionString.NETLIB.TCPIP:
672                         value = SqlConnectionString.NETLIB.TCPIP;
673                         break;
674                     case SqlConnectionString.NETLIB.VIA:
675                         value = SqlConnectionString.NETLIB.VIA;
676                         break;
677                     default:
678                         throw ADP.InvalidConnectionOptionValue(DbConnectionStringKeywords.NetworkLibrary);
679                     }
680                 }
681                 SetValue(DbConnectionStringKeywords.NetworkLibrary, value);
682                 _networkLibrary = value;
683             }
684         }
685
686         [DisplayName(DbConnectionStringKeywords.PacketSize)]
687         [ResCategoryAttribute(Res.DataCategory_Advanced)]
688         [ResDescriptionAttribute(Res.DbConnectionString_PacketSize)]
689         [RefreshPropertiesAttribute(RefreshProperties.All)]
690         public int PacketSize {
691             get { return _packetSize; }
692             set {
693                 if ((value < TdsEnums.MIN_PACKET_SIZE) || (TdsEnums.MAX_PACKET_SIZE < value)) {
694                     throw SQL.InvalidPacketSizeValue();
695                 }
696                 SetValue(DbConnectionStringKeywords.PacketSize, value);
697                 _packetSize = value;
698             }
699         }
700
701         [DisplayName(DbConnectionStringKeywords.Password)]
702         [PasswordPropertyTextAttribute(true)]
703         [ResCategoryAttribute(Res.DataCategory_Security)]
704         [ResDescriptionAttribute(Res.DbConnectionString_Password)]
705         [RefreshPropertiesAttribute(RefreshProperties.All)]
706         public string Password {
707             get { return _password; }
708             set {
709                 SetValue(DbConnectionStringKeywords.Password, value);
710                 _password = value;
711             }
712         }
713
714         [DisplayName(DbConnectionStringKeywords.PersistSecurityInfo)]
715         [ResCategoryAttribute(Res.DataCategory_Security)]
716         [ResDescriptionAttribute(Res.DbConnectionString_PersistSecurityInfo)]
717         [RefreshPropertiesAttribute(RefreshProperties.All)]
718         public bool PersistSecurityInfo {
719             get { return _persistSecurityInfo; }
720             set {
721                 SetValue(DbConnectionStringKeywords.PersistSecurityInfo, value);
722                 _persistSecurityInfo = value;
723             }
724         }
725
726         [DisplayName(DbConnectionStringKeywords.Pooling)]
727         [ResCategoryAttribute(Res.DataCategory_Pooling)]
728         [ResDescriptionAttribute(Res.DbConnectionString_Pooling)]
729         [RefreshPropertiesAttribute(RefreshProperties.All)]
730         public bool Pooling {
731             get { return _pooling; }
732             set {
733                 SetValue(DbConnectionStringKeywords.Pooling, value);
734                 _pooling = value;
735             }
736         }
737
738         [DisplayName(DbConnectionStringKeywords.Replication)]
739         [ResCategoryAttribute(Res.DataCategory_Replication)]
740         [ResDescriptionAttribute(Res.DbConnectionString_Replication )]
741         [RefreshPropertiesAttribute(RefreshProperties.All)]
742         public bool Replication {
743             get { return _replication; }
744             set {
745                 SetValue(DbConnectionStringKeywords.Replication, value);
746                 _replication = value;
747             }
748         }
749
750         [DisplayName(DbConnectionStringKeywords.TransactionBinding)]
751         [ResCategoryAttribute(Res.DataCategory_Advanced)]
752         [ResDescriptionAttribute(Res.DbConnectionString_TransactionBinding)]
753         [RefreshPropertiesAttribute(RefreshProperties.All)]
754         public string TransactionBinding {
755             get { return _transactionBinding; }
756             set {
757                 SetValue(DbConnectionStringKeywords.TransactionBinding, value);
758                 _transactionBinding = value;
759             }
760         }
761
762         [DisplayName(DbConnectionStringKeywords.TypeSystemVersion)]
763         [ResCategoryAttribute(Res.DataCategory_Advanced)]
764         [ResDescriptionAttribute(Res.DbConnectionString_TypeSystemVersion)]
765         [RefreshPropertiesAttribute(RefreshProperties.All)]
766         public string TypeSystemVersion {
767             get { return _typeSystemVersion; }
768             set {
769                 SetValue(DbConnectionStringKeywords.TypeSystemVersion, value);
770                 _typeSystemVersion = value;
771             }
772         }
773
774         [DisplayName(DbConnectionStringKeywords.UserID)]
775         [ResCategoryAttribute(Res.DataCategory_Security)]
776         [ResDescriptionAttribute(Res.DbConnectionString_UserID)]
777         [RefreshPropertiesAttribute(RefreshProperties.All)]
778         public string UserID {
779             get { return _userID; }
780             set {
781                 SetValue(DbConnectionStringKeywords.UserID, value);
782                 _userID = value;
783             }
784         }
785
786         [DisplayName(DbConnectionStringKeywords.UserInstance)]
787         [ResCategoryAttribute(Res.DataCategory_Source)]
788         [ResDescriptionAttribute(Res.DbConnectionString_UserInstance)]
789         [RefreshPropertiesAttribute(RefreshProperties.All)]
790         public bool UserInstance {
791             get { return _userInstance; }
792             set {
793                 SetValue(DbConnectionStringKeywords.UserInstance, value);
794                 _userInstance = value;
795             }
796         }
797
798         [DisplayName(DbConnectionStringKeywords.WorkstationID)]
799         [ResCategoryAttribute(Res.DataCategory_Context)]
800         [ResDescriptionAttribute(Res.DbConnectionString_WorkstationID)]
801         [RefreshPropertiesAttribute(RefreshProperties.All)]
802         public string WorkstationID {
803             get { return _workstationID; }
804             set {
805                 SetValue(DbConnectionStringKeywords.WorkstationID, value);
806                 _workstationID = value;
807             }
808         }
809
810         public override bool IsFixedSize {
811             get {
812                 return true;
813             }
814         }
815
816         public override ICollection Keys {
817             get {
818                 return new System.Data.Common.ReadOnlyCollection<string>(_validKeywords);
819             }
820         }
821
822         public override ICollection Values {
823             get {
824                 // written this way so if the ordering of Keywords & _validKeywords changes
825                 // this is one less place to maintain
826                 object[] values = new object[_validKeywords.Length];
827                 for(int i = 0; i < values.Length; ++i) {
828                     values[i] = GetAt((Keywords)i);
829                 }
830                 return new System.Data.Common.ReadOnlyCollection<object>(values);
831             }
832         }
833
834         public override void Clear() {
835             base.Clear();
836             for(int i = 0; i < _validKeywords.Length; ++i) {
837                 Reset((Keywords)i);
838             }
839         }
840
841         public override bool ContainsKey(string keyword) {
842             ADP.CheckArgumentNull(keyword, "keyword");
843             return _keywords.ContainsKey(keyword);
844         }
845
846         private static bool ConvertToBoolean(object value) {
847             return DbConnectionStringBuilderUtil.ConvertToBoolean(value);
848         }
849         private static int ConvertToInt32(object value) {
850             return DbConnectionStringBuilderUtil.ConvertToInt32(value);
851         }
852         private static bool ConvertToIntegratedSecurity(object value) {
853             return DbConnectionStringBuilderUtil.ConvertToIntegratedSecurity(value);
854         }
855         private static string ConvertToString(object value) {
856             return DbConnectionStringBuilderUtil.ConvertToString(value);
857         }
858         private static ApplicationIntent ConvertToApplicationIntent(string keyword, object value) {
859             return DbConnectionStringBuilderUtil.ConvertToApplicationIntent(keyword, value);
860         }
861         private static SqlAuthenticationMethod ConvertToAuthenticationType(string keyword, object value) {
862             return DbConnectionStringBuilderUtil.ConvertToAuthenticationType(keyword, value);
863         }
864
865         /// <summary>
866         /// Convert to SqlConnectionColumnEncryptionSetting.
867         /// </summary>
868         /// <param name="keyword"></param>
869         /// <param name="value"></param>
870         private static SqlConnectionColumnEncryptionSetting ConvertToColumnEncryptionSetting(string keyword, object value) {
871             return DbConnectionStringBuilderUtil.ConvertToColumnEncryptionSetting(keyword, value);
872         }
873
874         internal override string ConvertValueToString(object value) {
875             if (value is SqlAuthenticationMethod) {
876                 return DbConnectionStringBuilderUtil.AuthenticationTypeToString((SqlAuthenticationMethod)value);
877             }
878             else {
879                 return base.ConvertValueToString(value);
880             }
881         }
882
883         private object GetAt(Keywords index) {
884             switch(index) {
885             case Keywords.ApplicationIntent:        return this.ApplicationIntent;
886             case Keywords.ApplicationName:          return ApplicationName;
887             case Keywords.AsynchronousProcessing:   return AsynchronousProcessing;
888             case Keywords.AttachDBFilename:         return AttachDBFilename;
889             case Keywords.ConnectTimeout:           return ConnectTimeout;
890 #pragma warning disable 618 // Obsolete ConnectionReset
891             case Keywords.ConnectionReset:          return ConnectionReset;
892 #pragma warning restore 618
893             case Keywords.ContextConnection:        return ContextConnection;
894             case Keywords.CurrentLanguage:          return CurrentLanguage;
895             case Keywords.DataSource:               return DataSource;
896             case Keywords.Encrypt:                  return Encrypt;
897             case Keywords.Enlist:                   return Enlist;
898             case Keywords.FailoverPartner:          return FailoverPartner;
899             case Keywords.InitialCatalog:           return InitialCatalog;
900             case Keywords.IntegratedSecurity:       return IntegratedSecurity;
901             case Keywords.LoadBalanceTimeout:       return LoadBalanceTimeout;
902             case Keywords.MultipleActiveResultSets: return MultipleActiveResultSets;
903             case Keywords.MaxPoolSize:              return MaxPoolSize;
904             case Keywords.MinPoolSize:              return MinPoolSize;
905             case Keywords.MultiSubnetFailover:      return MultiSubnetFailover;
906 //          case Keywords.NamedConnection:          return NamedConnection;
907             case Keywords.NetworkLibrary:           return NetworkLibrary;
908             case Keywords.PacketSize:               return PacketSize;
909             case Keywords.Password:                 return Password;
910             case Keywords.PersistSecurityInfo:      return PersistSecurityInfo;
911             case Keywords.Pooling:                  return Pooling;
912             case Keywords.Replication:              return Replication;
913             case Keywords.TransactionBinding:       return TransactionBinding;
914             case Keywords.TrustServerCertificate:   return TrustServerCertificate;
915             case Keywords.TypeSystemVersion:        return TypeSystemVersion;
916             case Keywords.UserID:                   return UserID;
917             case Keywords.UserInstance:             return UserInstance;
918             case Keywords.WorkstationID:            return WorkstationID;
919             case Keywords.ConnectRetryCount:        return ConnectRetryCount;
920             case Keywords.ConnectRetryInterval:     return ConnectRetryInterval;
921             case Keywords.Authentication:           return Authentication;
922             case Keywords.ColumnEncryptionSetting: return ColumnEncryptionSetting;
923             default:
924                 Debug.Assert(false, "unexpected keyword");
925                 throw ADP.KeywordNotSupported(_validKeywords[(int)index]);
926             }
927         }
928
929         private Keywords GetIndex(string keyword) {
930             ADP.CheckArgumentNull(keyword, "keyword");
931             Keywords index;
932             if (_keywords.TryGetValue(keyword, out index)) {
933                 return index;
934             }
935             throw ADP.KeywordNotSupported(keyword);            
936         }
937
938         protected override void GetProperties(Hashtable propertyDescriptors) {
939             foreach(PropertyDescriptor reflected in TypeDescriptor.GetProperties(this, true)) {
940                 bool refreshOnChange = false;
941                 bool isReadonly = false;
942                 string displayName = reflected.DisplayName;
943
944                 // 'Password' & 'User ID' will be readonly if 'Integrated Security' is true
945                 if (DbConnectionStringKeywords.IntegratedSecurity == displayName) {
946                     refreshOnChange = true;
947                     isReadonly = reflected.IsReadOnly;
948                 }
949                 else if ((DbConnectionStringKeywords.Password == displayName) ||
950                          (DbConnectionStringKeywords.UserID == displayName)) {
951                      isReadonly = IntegratedSecurity;
952                 }
953                 else {
954                     continue;
955                 }
956                 Attribute[] attributes = GetAttributesFromCollection(reflected.Attributes);
957                 DbConnectionStringBuilderDescriptor descriptor = new DbConnectionStringBuilderDescriptor(reflected.Name,
958                         reflected.ComponentType, reflected.PropertyType, isReadonly, attributes);
959                 descriptor.RefreshOnChange = refreshOnChange;
960                 propertyDescriptors[displayName] = descriptor;
961             }
962             base.GetProperties(propertyDescriptors);
963         }
964
965         public override bool Remove(string keyword) {
966             ADP.CheckArgumentNull(keyword, "keyword");
967             Keywords index;
968             if (_keywords.TryGetValue(keyword, out index)) {
969                 if (base.Remove(_validKeywords[(int)index])) {
970                     Reset(index);
971                     return true;
972                 }
973             }
974             return false;
975         }
976
977         private void Reset(Keywords index) {
978             switch(index) {
979             case Keywords.ApplicationIntent:
980                 _applicationIntent = DbConnectionStringDefaults.ApplicationIntent;
981                 break;
982             case Keywords.ApplicationName:
983                 _applicationName = DbConnectionStringDefaults.ApplicationName;
984                 break;
985             case Keywords.AsynchronousProcessing:
986                 _asynchronousProcessing = DbConnectionStringDefaults.AsynchronousProcessing;
987                 break;
988             case Keywords.AttachDBFilename:
989                 _attachDBFilename = DbConnectionStringDefaults.AttachDBFilename;
990                 break;
991             case Keywords.Authentication:
992                 _authentication = DbConnectionStringDefaults.Authentication;
993                 break;
994             case Keywords.ConnectTimeout:
995                 _connectTimeout = DbConnectionStringDefaults.ConnectTimeout;
996                 break;
997             case Keywords.ConnectionReset:
998                 _connectionReset = DbConnectionStringDefaults.ConnectionReset;
999                 break;
1000             case Keywords.ContextConnection:
1001                 _contextConnection = DbConnectionStringDefaults.ContextConnection;
1002                 break;
1003             case Keywords.CurrentLanguage:
1004                 _currentLanguage = DbConnectionStringDefaults.CurrentLanguage;
1005                 break;
1006             case Keywords.DataSource:
1007                 _dataSource = DbConnectionStringDefaults.DataSource;
1008                 break;
1009             case Keywords.Encrypt:
1010                 _encrypt = DbConnectionStringDefaults.Encrypt;
1011                 break;
1012             case Keywords.Enlist:
1013                 _enlist = DbConnectionStringDefaults.Enlist;
1014                 break;
1015             case Keywords.FailoverPartner:
1016                 _failoverPartner = DbConnectionStringDefaults.FailoverPartner;
1017                 break;
1018             case Keywords.InitialCatalog:
1019                 _initialCatalog = DbConnectionStringDefaults.InitialCatalog;
1020                 break;
1021             case Keywords.IntegratedSecurity:
1022                 _integratedSecurity = DbConnectionStringDefaults.IntegratedSecurity;
1023                 break;
1024             case Keywords.LoadBalanceTimeout:
1025                 _loadBalanceTimeout = DbConnectionStringDefaults.LoadBalanceTimeout;
1026                 break;
1027             case Keywords.MultipleActiveResultSets:
1028                 _multipleActiveResultSets = DbConnectionStringDefaults.MultipleActiveResultSets;
1029                 break;
1030             case Keywords.MaxPoolSize:
1031                 _maxPoolSize = DbConnectionStringDefaults.MaxPoolSize;
1032                 break;
1033             case Keywords.MinPoolSize:
1034                 _minPoolSize = DbConnectionStringDefaults.MinPoolSize;
1035                 break;
1036             case Keywords.MultiSubnetFailover:
1037                 _multiSubnetFailover = DbConnectionStringDefaults.MultiSubnetFailover;
1038                 break;
1039 //          case Keywords.NamedConnection:
1040 //              _namedConnection = DbConnectionStringDefaults.NamedConnection;
1041 //              break;
1042             case Keywords.NetworkLibrary:
1043                 _networkLibrary = DbConnectionStringDefaults.NetworkLibrary;
1044                 break;
1045             case Keywords.PacketSize:
1046                 _packetSize = DbConnectionStringDefaults.PacketSize;
1047                 break;
1048             case Keywords.Password:
1049                 _password = DbConnectionStringDefaults.Password;
1050                 break;
1051             case Keywords.PersistSecurityInfo:
1052                 _persistSecurityInfo = DbConnectionStringDefaults.PersistSecurityInfo;
1053                 break;
1054             case Keywords.Pooling:
1055                 _pooling = DbConnectionStringDefaults.Pooling;
1056                 break;
1057             case Keywords.ConnectRetryCount:
1058                 _connectRetryCount = DbConnectionStringDefaults.ConnectRetryCount;
1059                 break;
1060             case Keywords.ConnectRetryInterval:
1061                 _connectRetryInterval = DbConnectionStringDefaults.ConnectRetryInterval;
1062                 break;
1063             case Keywords.Replication:
1064                 _replication = DbConnectionStringDefaults.Replication;
1065                 break;
1066             case Keywords.TransactionBinding:
1067                 _transactionBinding = DbConnectionStringDefaults.TransactionBinding;
1068                 break;
1069             case Keywords.TrustServerCertificate:
1070                 _trustServerCertificate = DbConnectionStringDefaults.TrustServerCertificate;
1071                 break;
1072             case Keywords.TypeSystemVersion:
1073                 _typeSystemVersion = DbConnectionStringDefaults.TypeSystemVersion;
1074                 break;
1075             case Keywords.UserID:
1076                 _userID = DbConnectionStringDefaults.UserID;
1077                 break;
1078             case Keywords.UserInstance:
1079                 _userInstance = DbConnectionStringDefaults.UserInstance;
1080                 break;
1081             case Keywords.WorkstationID:
1082                 _workstationID = DbConnectionStringDefaults.WorkstationID;
1083                 break;
1084             case Keywords.ColumnEncryptionSetting:
1085                 _columnEncryptionSetting = DbConnectionStringDefaults.ColumnEncryptionSetting;
1086                 break;
1087             default:
1088                 Debug.Assert(false, "unexpected keyword");
1089                 throw ADP.KeywordNotSupported(_validKeywords[(int)index]);
1090             }
1091         }
1092
1093         private void SetValue(string keyword, bool value) {
1094             base[keyword] = value.ToString((System.IFormatProvider)null);
1095         }
1096         private void SetValue(string keyword, int value) {
1097             base[keyword] = value.ToString((System.IFormatProvider)null);
1098         }
1099         private void SetValue(string keyword, string value) {
1100             ADP.CheckArgumentNull(value, keyword);
1101             base[keyword] = value;
1102         }
1103         private void SetApplicationIntentValue(ApplicationIntent value) {
1104             Debug.Assert(DbConnectionStringBuilderUtil.IsValidApplicationIntentValue(value), "Invalid value for ApplicationIntent");
1105             base[DbConnectionStringKeywords.ApplicationIntent] = DbConnectionStringBuilderUtil.ApplicationIntentToString(value);
1106         }
1107         private void SetAuthenticationValue(SqlAuthenticationMethod value) {
1108             Debug.Assert(DbConnectionStringBuilderUtil.IsValidAuthenticationTypeValue(value), "Invalid value for AuthenticationType");
1109             base[DbConnectionStringKeywords.Authentication] = DbConnectionStringBuilderUtil.AuthenticationTypeToString(value);
1110         }
1111         private void SetColumnEncryptionSettingValue(SqlConnectionColumnEncryptionSetting value) {
1112             Debug.Assert(DbConnectionStringBuilderUtil.IsValidColumnEncryptionSetting(value), "Invalid value for SqlConnectionColumnEncryptionSetting");
1113             base[DbConnectionStringKeywords.ColumnEncryptionSetting] = DbConnectionStringBuilderUtil.ColumnEncryptionSettingToString(value);
1114         }
1115
1116         public override bool ShouldSerialize(string keyword) {
1117             ADP.CheckArgumentNull(keyword, "keyword");
1118             Keywords index;
1119             return _keywords.TryGetValue(keyword, out index) && base.ShouldSerialize(_validKeywords[(int)index]);
1120         }
1121
1122         public override bool TryGetValue(string keyword, out object value) {
1123             Keywords index;
1124             if (_keywords.TryGetValue(keyword, out index)) {
1125                 value = GetAt(index);
1126                 return true;
1127             }
1128             value = null;
1129             return false;
1130         }
1131
1132         private sealed class NetworkLibraryConverter : TypeConverter {
1133 //            private const string AppleTalk     = "Apple Talk (DBMSADSN)";  Invalid protocals
1134 //            private const string BanyanVines   = "Banyan VINES (DBMSVINN)";
1135 //            private const string IPXSPX        = "NWLink IPX/SPX (DBMSSPXN)";
1136 //            private const string Multiprotocol = "Multiprotocol (DBMSRPCN)";
1137             private const string NamedPipes    = "Named Pipes (DBNMPNTW)";   // valid protocols
1138             private const string SharedMemory  = "Shared Memory (DBMSLPCN)";
1139             private const string TCPIP         = "TCP/IP (DBMSSOCN)";
1140             private const string VIA           = "VIA (DBMSGNET)";
1141
1142             // these are correctly non-static, property grid will cache an instance
1143             private StandardValuesCollection _standardValues;
1144
1145             // converter classes should have public ctor
1146             public NetworkLibraryConverter() {
1147             }
1148
1149             public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType) {
1150                 // Only know how to convert from a string
1151                 return ((typeof(string) == sourceType) || base.CanConvertFrom(context, sourceType));
1152             }
1153
1154             public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) {
1155                 string svalue = (value as string);
1156                 if (null != svalue) {
1157                     svalue = svalue.Trim();
1158                     if (StringComparer.OrdinalIgnoreCase.Equals(svalue, NamedPipes)) {
1159                         return SqlConnectionString.NETLIB.NamedPipes;
1160                     }
1161                     else if (StringComparer.OrdinalIgnoreCase.Equals(svalue, SharedMemory)) {
1162                         return SqlConnectionString.NETLIB.SharedMemory;
1163                     }
1164                     else if (StringComparer.OrdinalIgnoreCase.Equals(svalue, TCPIP)) {
1165                         return SqlConnectionString.NETLIB.TCPIP;
1166                     }
1167                     else if (StringComparer.OrdinalIgnoreCase.Equals(svalue, VIA)) {
1168                         return SqlConnectionString.NETLIB.VIA;
1169                     }
1170                     else {
1171                         return svalue;
1172                     }
1173                 }
1174                 return base.ConvertFrom(context, culture, value);
1175             }
1176
1177             public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType) {
1178                 return ((typeof(string) == destinationType) || base.CanConvertTo(context, destinationType));
1179             }
1180
1181             public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType) {
1182                 string svalue = (value as string);
1183                 if ((null != svalue) && (destinationType == typeof(string))) {
1184                     switch(svalue.Trim().ToLower(CultureInfo.InvariantCulture)) {
1185                     case SqlConnectionString.NETLIB.NamedPipes:
1186                         return NamedPipes;
1187                     case SqlConnectionString.NETLIB.SharedMemory:
1188                         return SharedMemory;
1189                     case SqlConnectionString.NETLIB.TCPIP:
1190                         return TCPIP;
1191                     case SqlConnectionString.NETLIB.VIA:
1192                         return VIA;
1193                     default:
1194                         return svalue;
1195                     }
1196                 }
1197                 return base.ConvertTo(context, culture, value, destinationType);
1198             }
1199
1200             public override bool GetStandardValuesSupported(ITypeDescriptorContext context) {
1201                 return true;
1202             }
1203
1204             public override bool GetStandardValuesExclusive(ITypeDescriptorContext context) {
1205                 return false;
1206             }
1207
1208             public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context) {
1209
1210                 SqlConnectionStringBuilder constr = null;
1211                 if (null != context) {
1212                     constr = (context.Instance as SqlConnectionStringBuilder);
1213                 }
1214
1215                 StandardValuesCollection standardValues = _standardValues;
1216                 if (null == standardValues) {
1217                     string[] names = new string[] {
1218                         NamedPipes,
1219                         SharedMemory,
1220                         TCPIP,
1221                         VIA,
1222                     };
1223                     standardValues = new StandardValuesCollection(names);
1224                     _standardValues = standardValues;
1225                 }
1226                 return standardValues;
1227             }
1228         }
1229
1230         private sealed class SqlDataSourceConverter : StringConverter {
1231
1232             private StandardValuesCollection _standardValues;
1233
1234             // converter classes should have public ctor
1235             public SqlDataSourceConverter() {
1236             }
1237
1238             public override bool GetStandardValuesSupported(ITypeDescriptorContext context) {
1239                 return true;
1240             }
1241
1242             public override bool GetStandardValuesExclusive(ITypeDescriptorContext context) {
1243                 return false;
1244             }
1245
1246             public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context) {
1247                 StandardValuesCollection dataSourceNames = _standardValues;
1248                 if (null == _standardValues) {
1249                     // Get the sources rowset for the SQLOLEDB enumerator
1250                     DataTable table = SqlClientFactory.Instance.CreateDataSourceEnumerator().GetDataSources();
1251                     DataColumn serverName = table.Columns[System.Data.Sql.SqlDataSourceEnumerator.ServerName];
1252                     DataColumn instanceName = table.Columns[System.Data.Sql.SqlDataSourceEnumerator.InstanceName];
1253                     DataRowCollection rows = table.Rows;
1254
1255                     string[] serverNames = new string[rows.Count];
1256                     for(int i = 0; i < serverNames.Length; ++i) {
1257                         string server   = rows[i][serverName] as string;
1258                         string instance = rows[i][instanceName] as string;
1259                         if ((null == instance) || (0 == instance.Length) || ("MSSQLSERVER" == instance)) {
1260                             serverNames[i] = server;
1261                         }
1262                         else {
1263                             serverNames[i] = server + @"\" + instance;
1264                         }
1265                     }
1266                     Array.Sort<string>(serverNames);
1267
1268                     // Create the standard values collection that contains the sources
1269                     dataSourceNames = new StandardValuesCollection(serverNames);
1270                     _standardValues = dataSourceNames;
1271                 }
1272                 return dataSourceNames;
1273             }
1274         }
1275
1276         private sealed class SqlInitialCatalogConverter : StringConverter {
1277
1278             // converter classes should have public ctor
1279             public SqlInitialCatalogConverter() {
1280             }
1281
1282             public override bool GetStandardValuesSupported(ITypeDescriptorContext context) {
1283                 return GetStandardValuesSupportedInternal(context);
1284             }
1285
1286             private bool GetStandardValuesSupportedInternal(ITypeDescriptorContext context) {
1287                 // Only say standard values are supported if the connection string has enough
1288                 // information set to instantiate a connection and retrieve a list of databases
1289                 bool flag = false;
1290                 if (null != context) {
1291                     SqlConnectionStringBuilder constr = (context.Instance as SqlConnectionStringBuilder);
1292                     if (null != constr) {
1293                         if ((0 < constr.DataSource.Length) && (constr.IntegratedSecurity || (0 < constr.UserID.Length))) {
1294                             flag = true;
1295                         }
1296                     }
1297                 }
1298                 return flag;
1299             }
1300
1301             public override bool GetStandardValuesExclusive(ITypeDescriptorContext context) {
1302                 // Although theoretically this could be true, some people may want to just type in a name
1303                 return false;
1304             }
1305
1306             public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context) {
1307                 // There can only be standard values if the connection string is in a state that might
1308                 // be able to instantiate a connection
1309                 if (GetStandardValuesSupportedInternal(context)) {
1310
1311                     // Create an array list to store the database names
1312                     List<string> values = new List<string>();
1313
1314                     try {
1315                         SqlConnectionStringBuilder constr = (SqlConnectionStringBuilder)context.Instance;
1316
1317                         // Create a connection
1318                         using(SqlConnection connection = new SqlConnection()) {
1319
1320                             // Create a basic connection string from current property values
1321                             connection.ConnectionString = constr.ConnectionString;
1322
1323                             // Try to open the connection
1324                             connection.Open();
1325
1326                             DataTable databaseTable = connection.GetSchema("DATABASES");
1327
1328                             foreach (DataRow row in databaseTable.Rows) {
1329                                 string dbName = (string)row["database_name"];
1330                                 values.Add(dbName);
1331                             }
1332                         }
1333                     }
1334                     catch(SqlException e) {
1335                         ADP.TraceExceptionWithoutRethrow(e);
1336                         // silently fail
1337                     }
1338
1339                     // Return values as a StandardValuesCollection
1340                     return new StandardValuesCollection(values);
1341                 }
1342                 return null;
1343             }
1344         }
1345
1346         sealed internal class SqlConnectionStringBuilderConverter : ExpandableObjectConverter {
1347
1348             // converter classes should have public ctor
1349             public SqlConnectionStringBuilderConverter() {
1350             }
1351
1352             override public bool CanConvertTo(ITypeDescriptorContext context, Type destinationType) {
1353                 if (typeof(System.ComponentModel.Design.Serialization.InstanceDescriptor) == destinationType) {
1354                     return true;
1355                 }
1356                 return base.CanConvertTo(context, destinationType);
1357             }
1358
1359             override public object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType) {
1360                 if (destinationType == null) {
1361                     throw ADP.ArgumentNull("destinationType");
1362                 }
1363                 if (typeof(System.ComponentModel.Design.Serialization.InstanceDescriptor) == destinationType) {
1364                     SqlConnectionStringBuilder obj = (value as SqlConnectionStringBuilder);
1365                     if (null != obj) {
1366                         return ConvertToInstanceDescriptor(obj);
1367                     }
1368                 }
1369                 return base.ConvertTo(context, culture, value, destinationType);
1370             }
1371
1372             private System.ComponentModel.Design.Serialization.InstanceDescriptor ConvertToInstanceDescriptor(SqlConnectionStringBuilder options) {
1373                 Type[] ctorParams = new Type[] { typeof(string) };
1374                 object[] ctorValues = new object[] { options.ConnectionString };
1375                 System.Reflection.ConstructorInfo ctor = typeof(SqlConnectionStringBuilder).GetConstructor(ctorParams);
1376                 return new System.ComponentModel.Design.Serialization.InstanceDescriptor(ctor, ctorValues);
1377             }
1378         }
1379     }
1380
1381 }
1382