1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773
|
//------------------------------------------------------------------------------
// <copyright file="OleDbConnectionStringBuilder.cs" company="Microsoft">
// Copyright (c) Microsoft Corporation. All rights reserved.
// </copyright>
// <owner current="true" primary="true">Microsoft</owner>
// <owner current="true" primary="false">Microsoft</owner>
//------------------------------------------------------------------------------
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.Common;
using System.Diagnostics;
using System.Globalization;
using System.Runtime.Serialization;
using System.Security.Permissions;
using System.Text;
namespace System.Data.OleDb {
[DefaultProperty("Provider")]
[RefreshPropertiesAttribute(RefreshProperties.All)]
[System.ComponentModel.TypeConverterAttribute(typeof(OleDbConnectionStringBuilder.OleDbConnectionStringBuilderConverter))]
public sealed class OleDbConnectionStringBuilder : DbConnectionStringBuilder {
private enum Keywords { // specific ordering for ConnectionString output construction
// NamedConnection,
FileName,
Provider,
DataSource,
PersistSecurityInfo,
OleDbServices,
}
private static readonly string[] _validKeywords;
private static readonly Dictionary<string,Keywords> _keywords;
private string[] _knownKeywords;
private Dictionary<string,OleDbPropertyInfo> _propertyInfo;
// private string _namedConnection = DbConnectionStringDefaults.NamedConnection;
private string _fileName = DbConnectionStringDefaults.FileName;
private string _dataSource = DbConnectionStringDefaults.DataSource;
private string _provider = DbConnectionStringDefaults.Provider;
private int _oleDbServices = DbConnectionStringDefaults.OleDbServices;
private bool _persistSecurityInfo = DbConnectionStringDefaults.PersistSecurityInfo;
static OleDbConnectionStringBuilder() {
string[] validKeywords = new string[5];
validKeywords[(int)Keywords.DataSource] = DbConnectionStringKeywords.DataSource;
validKeywords[(int)Keywords.FileName] = DbConnectionStringKeywords.FileName;
// validKeywords[(int)Keywords.NamedConnection] = DbConnectionStringKeywords.NamedConnection;
validKeywords[(int)Keywords.OleDbServices] = DbConnectionStringKeywords.OleDbServices;
validKeywords[(int)Keywords.PersistSecurityInfo] = DbConnectionStringKeywords.PersistSecurityInfo;
validKeywords[(int)Keywords.Provider] = DbConnectionStringKeywords.Provider;
_validKeywords = validKeywords;
Dictionary<string,Keywords> hash = new Dictionary<string,Keywords>(9, StringComparer.OrdinalIgnoreCase);
hash.Add(DbConnectionStringKeywords.DataSource, Keywords.DataSource);
hash.Add(DbConnectionStringKeywords.FileName, Keywords.FileName);
// hash.Add(DbConnectionStringKeywords.NamedConnection, Keywords.NamedConnection);
hash.Add(DbConnectionStringKeywords.OleDbServices, Keywords.OleDbServices);
hash.Add(DbConnectionStringKeywords.PersistSecurityInfo, Keywords.PersistSecurityInfo);
hash.Add(DbConnectionStringKeywords.Provider, Keywords.Provider);
Debug.Assert(5 == hash.Count, "initial expected size is incorrect");
_keywords = hash;
}
public OleDbConnectionStringBuilder() : this(null) {
_knownKeywords = _validKeywords;
}
public OleDbConnectionStringBuilder(string connectionString) : base() {
if (!ADP.IsEmpty(connectionString)) {
ConnectionString = connectionString;
}
}
public override object this[string keyword] {
get {
ADP.CheckArgumentNull(keyword, "keyword");
object value;
Keywords index;
if (_keywords.TryGetValue(keyword, out index)) {
value = GetAt(index);
}
else if (!base.TryGetValue(keyword, out value)) {
Dictionary<string,OleDbPropertyInfo> dynamic = GetProviderInfo(Provider);
OleDbPropertyInfo info = dynamic[keyword];
value = info._defaultValue;
}
return value;
}
set {
if (null != value) {
ADP.CheckArgumentNull(keyword, "keyword");
Keywords index;
if (_keywords.TryGetValue(keyword, out index)) {
switch(index) {
case Keywords.DataSource: DataSource = ConvertToString(value); break;
case Keywords.FileName: FileName = ConvertToString(value); break;
// case Keywords.NamedConnection: NamedConnection = ConvertToString(value); break;
case Keywords.Provider: Provider = ConvertToString(value); break;
case Keywords.OleDbServices: OleDbServices = ConvertToInt32(value); break;
case Keywords.PersistSecurityInfo: PersistSecurityInfo = ConvertToBoolean(value); break;
default:
Debug.Assert(false, "unexpected keyword");
throw ADP.KeywordNotSupported(keyword);
}
}
else {
base[keyword] = value;
ClearPropertyDescriptors();
}
}
else {
Remove(keyword);
}
}
}
[DisplayName(DbConnectionStringKeywords.DataSource)]
[ResCategoryAttribute(Res.DataCategory_Source)]
[ResDescriptionAttribute(Res.DbConnectionString_DataSource)]
[RefreshPropertiesAttribute(RefreshProperties.All)]
//
public string DataSource {
get { return _dataSource; }
set {
SetValue(DbConnectionStringKeywords.DataSource, value);
_dataSource = value;
}
}
[DisplayName(DbConnectionStringKeywords.FileName)]
[ResCategoryAttribute(Res.DataCategory_NamedConnectionString)]
[ResDescriptionAttribute(Res.DbConnectionString_FileName)]
[RefreshPropertiesAttribute(RefreshProperties.All)]
//
[Editor("System.Windows.Forms.Design.FileNameEditor, " + AssemblyRef.SystemDesign, "System.Drawing.Design.UITypeEditor, " + AssemblyRef.SystemDrawing)]
public string FileName {
get { return _fileName; }
set {
SetValue(DbConnectionStringKeywords.FileName, value);
_fileName = value;
}
}
/*
[DisplayName(DbConnectionStringKeywords.NamedConnection)]
[ResCategoryAttribute(Res.DataCategory_NamedConnectionString)]
[ResDescriptionAttribute(Res.DbConnectionString_NamedConnection)]
[RefreshPropertiesAttribute(RefreshProperties.All)]
[TypeConverter(typeof(NamedConnectionStringConverter))]
public string NamedConnection {
get { return _namedConnection; }
set {
SetValue(DbConnectionStringKeywords.NamedConnection, value);
_namedConnection = value;
}
}
*/
[DisplayName(DbConnectionStringKeywords.OleDbServices)]
[ResCategoryAttribute(Res.DataCategory_Pooling)]
[ResDescriptionAttribute(Res.DbConnectionString_OleDbServices)]
[RefreshPropertiesAttribute(RefreshProperties.All)]
[TypeConverter(typeof(OleDbConnectionStringBuilder.OleDbServicesConverter))]
public int OleDbServices {
get { return _oleDbServices; }
set {
SetValue(DbConnectionStringKeywords.OleDbServices, value);
_oleDbServices = value;
}
}
[DisplayName(DbConnectionStringKeywords.PersistSecurityInfo)]
[ResCategoryAttribute(Res.DataCategory_Security)]
[ResDescriptionAttribute(Res.DbConnectionString_PersistSecurityInfo)]
[RefreshPropertiesAttribute(RefreshProperties.All)]
public bool PersistSecurityInfo {
get { return _persistSecurityInfo; }
set {
SetValue(DbConnectionStringKeywords.PersistSecurityInfo, value);
_persistSecurityInfo = value;
}
}
[DisplayName(DbConnectionStringKeywords.Provider)]
[ResCategoryAttribute(Res.DataCategory_Source)]
[ResDescriptionAttribute(Res.DbConnectionString_Provider)]
[RefreshPropertiesAttribute(RefreshProperties.All)]
[TypeConverter(typeof(OleDbConnectionStringBuilder.OleDbProviderConverter))]
public string Provider {
get { return _provider; }
set {
SetValue(DbConnectionStringKeywords.Provider, value);
_provider = value;
RestartProvider();
}
}
public override ICollection Keys {
get {
string[] knownKeywords = _knownKeywords;
if (null == knownKeywords) {
Dictionary<string,OleDbPropertyInfo> dynamic = GetProviderInfo(Provider);
if (0 < dynamic.Count) {
knownKeywords = new string[_validKeywords.Length + dynamic.Count];
_validKeywords.CopyTo(knownKeywords, 0);
dynamic.Keys.CopyTo(knownKeywords, _validKeywords.Length);
}
else {
knownKeywords = _validKeywords;
}
int count = 0;
foreach(string keyword in base.Keys) {
bool flag = true;
foreach(string s in knownKeywords) {
if (StringComparer.OrdinalIgnoreCase.Equals(s, keyword)) {
flag = false;
break;
}
}
if (flag) {
count++;
}
}
if (0 < count) {
string[] tmp = new string[knownKeywords.Length + count];
knownKeywords.CopyTo(tmp, 0);
int index = knownKeywords.Length;
foreach(string keyword in base.Keys) {
bool flag = true;
foreach(string s in knownKeywords) {
if (StringComparer.OrdinalIgnoreCase.Equals(s, keyword)) {
flag = false;
break;
}
}
if (flag) {
tmp[index++] = keyword;
}
}
knownKeywords = tmp;
}
_knownKeywords = knownKeywords;
}
return new System.Data.Common.ReadOnlyCollection<string>(knownKeywords);
}
}
public override bool ContainsKey(string keyword) {
ADP.CheckArgumentNull(keyword, "keyword");
return _keywords.ContainsKey(keyword) || base.ContainsKey(keyword);
}
private static bool ConvertToBoolean(object value) {
return DbConnectionStringBuilderUtil.ConvertToBoolean(value);
}
private static int ConvertToInt32(object value) {
return DbConnectionStringBuilderUtil.ConvertToInt32(value);
}
private static string ConvertToString(object value) {
return DbConnectionStringBuilderUtil.ConvertToString(value);
}
public override void Clear() {
base.Clear();
for(int i = 0; i < _validKeywords.Length; ++i) {
Reset((Keywords)i);
}
base.ClearPropertyDescriptors();
_knownKeywords = _validKeywords;
}
private object GetAt(Keywords index) {
switch(index) {
case Keywords.DataSource: return DataSource;
case Keywords.FileName: return FileName;
// case Keywords.NamedConnection: return NamedConnection;
case Keywords.OleDbServices: return OleDbServices;
case Keywords.PersistSecurityInfo: return PersistSecurityInfo;
case Keywords.Provider: return Provider;
default:
Debug.Assert(false, "unexpected keyword");
throw ADP.KeywordNotSupported(_validKeywords[(int)index]);
}
}
public override bool Remove(string keyword) {
ADP.CheckArgumentNull(keyword, "keyword");
bool value = base.Remove(keyword);
Keywords index;
if (_keywords.TryGetValue(keyword, out index)) {
Reset(index);
}
else if (value) {
ClearPropertyDescriptors();
}
return value;
}
private void Reset(Keywords index) {
switch(index) {
case Keywords.DataSource:
_dataSource = DbConnectionStringDefaults.DataSource;
break;
case Keywords.FileName:
_fileName = DbConnectionStringDefaults.FileName;
RestartProvider();
break;
// case Keywords.NamedConnection:
// _namedConnection = DbConnectionStringDefaults.NamedConnection;
// break;
case Keywords.OleDbServices:
_oleDbServices = DbConnectionStringDefaults.OleDbServices;
break;
case Keywords.PersistSecurityInfo:
_persistSecurityInfo = DbConnectionStringDefaults.PersistSecurityInfo;
break;
case Keywords.Provider:
_provider = DbConnectionStringDefaults.Provider;
RestartProvider();
break;
default:
Debug.Assert(false, "unexpected keyword");
throw ADP.KeywordNotSupported(_validKeywords[(int)index]);
}
}
private new void ClearPropertyDescriptors() {
base.ClearPropertyDescriptors();
_knownKeywords = null;
}
private void RestartProvider() {
ClearPropertyDescriptors();
_propertyInfo = null;
}
private void SetValue(string keyword, bool value) {
base[keyword] = value.ToString((System.IFormatProvider)null);
}
private void SetValue(string keyword, int value) {
base[keyword] = value.ToString((System.IFormatProvider)null);
}
private void SetValue(string keyword, string value) {
ADP.CheckArgumentNull(value, keyword);
base[keyword] = value;
}
public override bool TryGetValue(string keyword, out object value) {
ADP.CheckArgumentNull(keyword, "keyword");
Keywords index;
if (_keywords.TryGetValue(keyword, out index)) {
value = GetAt(index);
return true;
}
else if (!base.TryGetValue(keyword, out value)) {
Dictionary<string,OleDbPropertyInfo> dynamic = GetProviderInfo(Provider);
OleDbPropertyInfo info;
if (dynamic.TryGetValue(keyword, out info)) {
value = info._defaultValue;
return true;
}
return false;
}
return true;
}
private Dictionary<string,OleDbPropertyInfo> GetProviderInfo(string provider) {
Dictionary<string,OleDbPropertyInfo> providerInfo = _propertyInfo;
if (null == providerInfo) {
providerInfo = new Dictionary<string,OleDbPropertyInfo>(StringComparer.OrdinalIgnoreCase);
if (!ADP.IsEmpty(provider)) {
Dictionary<string,OleDbPropertyInfo> hash = null;
try {
StringBuilder builder = new StringBuilder();
AppendKeyValuePair(builder, DbConnectionStringKeywords.Provider, provider);
OleDbConnectionString constr = new OleDbConnectionString(builder.ToString(), true);
constr.CreatePermissionSet().Demand();
// load provider without calling Initialize or CreateDataSource
using(OleDbConnectionInternal connection = new OleDbConnectionInternal(constr, (OleDbConnection)null)) {
// get all the init property information for the provider
hash = connection.GetPropertyInfo(new Guid[] { OleDbPropertySetGuid.DBInitAll });
foreach(KeyValuePair<string,OleDbPropertyInfo> entry in hash) {
Keywords index;
OleDbPropertyInfo info = entry.Value;
if (!_keywords.TryGetValue(info._description, out index)) {
if ((OleDbPropertySetGuid.DBInit == info._propertySet) &&
((ODB.DBPROP_INIT_ASYNCH == info._propertyID) ||
(ODB.DBPROP_INIT_HWND == info._propertyID) ||
(ODB.DBPROP_INIT_PROMPT == info._propertyID))) {
continue; // skip this keyword
}
providerInfo[info._description] = info;
}
}
// what are the unique propertysets?
List<Guid> listPropertySets= new List<Guid>();
foreach(KeyValuePair<string,OleDbPropertyInfo> entry in hash) {
OleDbPropertyInfo info = entry.Value;
if (!listPropertySets.Contains(info._propertySet)) {
listPropertySets.Add(info._propertySet);
}
}
Guid[] arrayPropertySets = new Guid[listPropertySets.Count];
listPropertySets.CopyTo(arrayPropertySets, 0);
// get all the init property values for the provider
using(PropertyIDSet propidset = new PropertyIDSet(arrayPropertySets)) {
using(IDBPropertiesWrapper idbProperties = connection.IDBProperties()) {
OleDbHResult hr;
using(DBPropSet propset = new DBPropSet(idbProperties.Value, propidset, out hr)) {
// VSDD 671375: OleDbConnectionStringBuilder is ignoring/hiding potential errors of OLEDB provider when reading its properties information
if (0 <= (int)hr) {
int count = propset.PropertySetCount;
for(int i = 0; i < count; ++i) {
Guid propertyset;
tagDBPROP[] props = propset.GetPropertySet(i, out propertyset);
// attach the default property value to the property info
foreach(tagDBPROP prop in props) {
foreach(KeyValuePair<string,OleDbPropertyInfo> entry in hash) {
OleDbPropertyInfo info = entry.Value;
if ((info._propertyID == prop.dwPropertyID) && (info._propertySet == propertyset)) {
info._defaultValue = prop.vValue;
if (null == info._defaultValue) {
if (typeof(string) == info._type) {
info._defaultValue = "";
}
else if (typeof(Int32) == info._type) {
info._defaultValue = 0;
}
else if (typeof(Boolean) == info._type) {
info._defaultValue = false;
}
}
}
}
}
}
}
}
}
}
}
}
catch(System.InvalidOperationException e) {
ADP.TraceExceptionWithoutRethrow(e);
}
catch(System.Data.OleDb.OleDbException e) {
ADP.TraceExceptionWithoutRethrow(e);
}
catch(System.Security.SecurityException e) {
ADP.TraceExceptionWithoutRethrow(e);
}
}
_propertyInfo = providerInfo;
}
return providerInfo;
}
protected override void GetProperties(Hashtable propertyDescriptors) {
Dictionary<string,OleDbPropertyInfo> providerInfo = GetProviderInfo(Provider);
if (0 < providerInfo.Count) {
foreach(OleDbPropertyInfo info in providerInfo.Values) {
Keywords index;
if (!_keywords.TryGetValue(info._description, out index)) { // not a strongly typed property
bool isReadOnly = false;
bool refreshOnChange = false;
Attribute[] attributes;
if (OleDbPropertySetGuid.DBInit == info._propertySet) {
switch(info._propertyID) {
#if DEBUG
case ODB.DBPROP_AUTH_PERSIST_SENSITIVE_AUTHINFO:
case ODB.DBPROP_INIT_ASYNCH:
case ODB.DBPROP_INIT_DATASOURCE:
case ODB.DBPROP_INIT_HWND:
case ODB.DBPROP_INIT_OLEDBSERVICES:
Debug.Assert(false, "should be handled via strongly typed property");
goto default;
#endif
case ODB.DBPROP_INIT_CATALOG:
case ODB.DBPROP_INIT_LOCATION:
attributes = new Attribute[] {
BrowsableAttribute.Yes,
new ResCategoryAttribute(Res.DataCategory_Source),
RefreshPropertiesAttribute.All,
};
break;
case ODB.DBPROP_INIT_TIMEOUT:
case ODB.DBPROP_INIT_GENERALTIMEOUT:
attributes = new Attribute[] {
BrowsableAttribute.Yes,
new ResCategoryAttribute(Res.DataCategory_Initialization),
RefreshPropertiesAttribute.All,
};
break;
// 'Password' & 'User ID' will be readonly if 'Integrated Security' exists
case ODB.DBPROP_AUTH_PASSWORD:
attributes = new Attribute[] {
BrowsableAttribute.Yes,
PasswordPropertyTextAttribute.Yes,
new ResCategoryAttribute(Res.DataCategory_Security),
RefreshPropertiesAttribute.All,
};
isReadOnly = ContainsKey(DbConnectionStringKeywords.IntegratedSecurity);
refreshOnChange = true;
break;
case ODB.DBPROP_AUTH_USERID:
attributes = new Attribute[] {
BrowsableAttribute.Yes,
new ResCategoryAttribute(Res.DataCategory_Security),
RefreshPropertiesAttribute.All,
};
isReadOnly = ContainsKey(DbConnectionStringKeywords.IntegratedSecurity);
refreshOnChange = true;
break;
case ODB.DBPROP_AUTH_CACHE_AUTHINFO:
case ODB.DBPROP_AUTH_ENCRYPT_PASSWORD:
case ODB.DBPROP_AUTH_INTEGRATED:
case ODB.DBPROP_AUTH_MASK_PASSWORD:
case ODB.DBPROP_AUTH_PERSIST_ENCRYPTED:
attributes = new Attribute[] {
BrowsableAttribute.Yes,
new ResCategoryAttribute(Res.DataCategory_Security),
RefreshPropertiesAttribute.All,
};
refreshOnChange = (ODB.DBPROP_AUTH_INTEGRATED == info._propertyID);
break;
case ODB.DBPROP_INIT_BINDFLAGS:
case ODB.DBPROP_INIT_IMPERSONATION_LEVEL:
case ODB.DBPROP_INIT_LCID:
case ODB.DBPROP_INIT_MODE:
case ODB.DBPROP_INIT_PROTECTION_LEVEL:
case ODB.DBPROP_INIT_PROVIDERSTRING:
case ODB.DBPROP_INIT_LOCKOWNER:
attributes = new Attribute[] {
BrowsableAttribute.Yes,
new ResCategoryAttribute(Res.DataCategory_Advanced),
RefreshPropertiesAttribute.All,
};
break;
default:
Debug.Assert(false, "new standard propertyid");
attributes = new Attribute[] {
BrowsableAttribute.Yes,
RefreshPropertiesAttribute.All,
};
break;
}
}
else if (info._description.EndsWith(" Provider", StringComparison.OrdinalIgnoreCase)) {
attributes = new Attribute[] {
BrowsableAttribute.Yes,
RefreshPropertiesAttribute.All,
new ResCategoryAttribute(Res.DataCategory_Source),
new TypeConverterAttribute(typeof(OleDbConnectionStringBuilder.OleDbProviderConverter)),
};
refreshOnChange = true;
}
else {
attributes = new Attribute[] {
BrowsableAttribute.Yes,
RefreshPropertiesAttribute.All,
new CategoryAttribute(Provider),
};
}
DbConnectionStringBuilderDescriptor descriptor = new DbConnectionStringBuilderDescriptor(info._description,
typeof(OleDbConnectionStringBuilder), info._type, isReadOnly, attributes);
descriptor.RefreshOnChange = refreshOnChange;
propertyDescriptors[info._description] = descriptor;
}
// else strongly typed property already exists, i.e. DataSource
}
}
base.GetProperties(propertyDescriptors);
}
private sealed class OleDbProviderConverter : StringConverter {
private const int DBSOURCETYPE_DATASOURCE_TDP = 1;
private const int DBSOURCETYPE_DATASOURCE_MDP = 3;
private StandardValuesCollection _standardValues;
// converter classes should have public ctor
public OleDbProviderConverter() {
}
public override bool GetStandardValuesSupported(ITypeDescriptorContext context) {
return true;
}
public override bool GetStandardValuesExclusive(ITypeDescriptorContext context) {
return false;
}
public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context) {
StandardValuesCollection dataSourceNames = _standardValues;
if (null == _standardValues) {
// Get the sources rowset for the SQLOLEDB enumerator
DataTable table = (new OleDbEnumerator()).GetElements();
DataColumn column2 = table.Columns["SOURCES_NAME"];
DataColumn column5 = table.Columns["SOURCES_TYPE"];
//DataColumn column4 = table.Columns["SOURCES_DESCRIPTION"];
System.Collections.Generic.List<string> providerNames = new System.Collections.Generic.List<string>(table.Rows.Count);
foreach(DataRow row in table.Rows) {
int sourceType = (int)row[column5];
if (DBSOURCETYPE_DATASOURCE_TDP == sourceType || DBSOURCETYPE_DATASOURCE_MDP == sourceType) {
string progid = (string)row[column2];
if (!OleDbConnectionString.IsMSDASQL(progid.ToLower(CultureInfo.InvariantCulture))) {
if (0 > providerNames.IndexOf(progid)) {
providerNames.Add(progid);
}
}
}
}
// Create the standard values collection that contains the sources
dataSourceNames = new StandardValuesCollection(providerNames);
_standardValues = dataSourceNames;
}
return dataSourceNames;
}
}
[Flags()] internal enum OleDbServiceValues : int {
DisableAll = unchecked((int)0x00000000),
ResourcePooling = unchecked((int)0x00000001),
TransactionEnlistment = unchecked((int)0x00000002),
ClientCursor = unchecked((int)0x00000004),
AggregationAfterSession = unchecked((int)0x00000008),
EnableAll = unchecked((int)0xffffffff),
Default = ~(ClientCursor | AggregationAfterSession),
};
internal sealed class OleDbServicesConverter : TypeConverter {
private StandardValuesCollection _standardValues;
// converter classes should have public ctor
public OleDbServicesConverter() : base() {
}
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType) {
// Only know how to convert from a string
return ((typeof(string) == sourceType) || base.CanConvertFrom(context, sourceType));
}
public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) {
string svalue = (value as string);
if (null != svalue) {
Int32 services;
if(Int32.TryParse(svalue, out services)) {
return services;
}
else {
if (svalue.IndexOf(',') != -1) {
int convertedValue = 0;
string[] values = svalue.Split(new char[] {','});
foreach(string v in values) {
convertedValue |= (int)(OleDbServiceValues)Enum.Parse(typeof(OleDbServiceValues), v, true);
}
return (int)convertedValue;;
}
else {
return (int)(OleDbServiceValues)Enum.Parse(typeof(OleDbServiceValues), svalue, true);
}
}
}
return base.ConvertFrom(context, culture, value);
}
public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType) {
// Only know how to convert to the NetworkLibrary enumeration
return ((typeof(string) == destinationType) || base.CanConvertTo(context, destinationType));
}
public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType) {
if ((typeof(string) == destinationType) && (null != value) && (typeof(Int32) == value.GetType())) {
return Enum.Format(typeof(OleDbServiceValues), ((OleDbServiceValues)(int)value), "G");
}
return base.ConvertTo(context, culture, value, destinationType);
}
public override bool GetStandardValuesSupported(ITypeDescriptorContext context) {
return true;
}
public override bool GetStandardValuesExclusive(ITypeDescriptorContext context) {
return false;
}
public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context) {
StandardValuesCollection standardValues = _standardValues;
if (null == standardValues) {
Array objValues = Enum.GetValues(typeof(OleDbServiceValues));
Array.Sort(objValues, 0, objValues.Length);
standardValues = new StandardValuesCollection(objValues);
_standardValues = standardValues;
}
return standardValues;
}
public override bool IsValid(ITypeDescriptorContext context, object value) {
return true;
//return Enum.IsDefined(type, value);
}
}
sealed internal class OleDbConnectionStringBuilderConverter : ExpandableObjectConverter {
// converter classes should have public ctor
public OleDbConnectionStringBuilderConverter() {
}
override public bool CanConvertTo(ITypeDescriptorContext context, Type destinationType) {
if (typeof(System.ComponentModel.Design.Serialization.InstanceDescriptor) == destinationType) {
return true;
}
return base.CanConvertTo(context, destinationType);
}
override public object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType) {
if (destinationType == null) {
throw ADP.ArgumentNull("destinationType");
}
if (typeof(System.ComponentModel.Design.Serialization.InstanceDescriptor) == destinationType) {
OleDbConnectionStringBuilder obj = (value as OleDbConnectionStringBuilder);
if (null != obj) {
return ConvertToInstanceDescriptor(obj);
}
}
return base.ConvertTo(context, culture, value, destinationType);
}
private System.ComponentModel.Design.Serialization.InstanceDescriptor ConvertToInstanceDescriptor(OleDbConnectionStringBuilder options) {
Type[] ctorParams = new Type[] { typeof(string) };
object[] ctorValues = new object[] { options.ConnectionString };
System.Reflection.ConstructorInfo ctor = typeof(OleDbConnectionStringBuilder).GetConstructor(ctorParams);
return new System.ComponentModel.Design.Serialization.InstanceDescriptor(ctor, ctorValues);
}
}
}
}
|