SqlDataSourceEnumerator.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. //------------------------------------------------------------------------------
  2. // <copyright file="SqlDataSourceEnumerator.cs" company="Microsoft">
  3. // Copyright (c) Microsoft Corporation. All rights reserved.
  4. // </copyright>
  5. // <owner current="true" primary="true">[....]</owner>
  6. // <owner current="true" primary="false">[....]</owner>
  7. //------------------------------------------------------------------------------
  8. namespace System.Data.Sql {
  9. using System;
  10. using System.Data;
  11. using System.Data.Common;
  12. using System.Data.SqlClient;
  13. using System.Diagnostics;
  14. using System.Globalization;
  15. using System.Runtime.CompilerServices;
  16. using System.Runtime.InteropServices;
  17. using System.Security;
  18. using System.Text;
  19. public sealed class SqlDataSourceEnumerator : DbDataSourceEnumerator {
  20. private static readonly SqlDataSourceEnumerator SingletonInstance = new SqlDataSourceEnumerator();
  21. internal const string ServerName = "ServerName";
  22. internal const string InstanceName = "InstanceName";
  23. internal const string IsClustered = "IsClustered";
  24. internal const string Version = "Version";
  25. private const int timeoutSeconds = ADP.DefaultCommandTimeout;
  26. private long timeoutTime; // variable used for timeout computations, holds the value of the hi-res performance counter at which this request should expire
  27. private SqlDataSourceEnumerator() : base() {
  28. }
  29. public static SqlDataSourceEnumerator Instance {
  30. get {
  31. return SqlDataSourceEnumerator.SingletonInstance;
  32. }
  33. }
  34. override public DataTable GetDataSources() {
  35. (new NamedPermissionSet("FullTrust")).Demand(); // SQLBUDT 244304
  36. char[] buffer = null;
  37. StringBuilder strbldr = new StringBuilder();
  38. Int32 bufferSize = 1024;
  39. Int32 readLength = 0;
  40. buffer = new char[bufferSize];
  41. bool more = true;
  42. bool failure = false;
  43. IntPtr handle = ADP.PtrZero;
  44. RuntimeHelpers.PrepareConstrainedRegions();
  45. try {
  46. timeoutTime = TdsParserStaticMethods.GetTimeoutSeconds(timeoutSeconds);
  47. RuntimeHelpers.PrepareConstrainedRegions();
  48. try {} finally {
  49. handle = SNINativeMethodWrapper.SNIServerEnumOpen();
  50. }
  51. if (ADP.PtrZero != handle) {
  52. while (more && !TdsParserStaticMethods.TimeoutHasExpired(timeoutTime)) {
  53. readLength = SNINativeMethodWrapper.SNIServerEnumRead(handle, buffer, bufferSize, ref more);
  54. if (readLength > bufferSize) {
  55. failure = true;
  56. more = false;
  57. }
  58. else if (0 < readLength) {
  59. strbldr.Append(buffer, 0, readLength);
  60. }
  61. }
  62. }
  63. }
  64. finally {
  65. if (ADP.PtrZero != handle) {
  66. SNINativeMethodWrapper.SNIServerEnumClose(handle);
  67. }
  68. }
  69. if (failure) {
  70. Debug.Assert(false, "GetDataSources:SNIServerEnumRead returned bad length");
  71. Bid.Trace("<sc.SqlDataSourceEnumerator.GetDataSources|ERR> GetDataSources:SNIServerEnumRead returned bad length, requested %d, received %d", bufferSize, readLength);
  72. throw ADP.ArgumentOutOfRange("readLength");
  73. }
  74. return ParseServerEnumString(strbldr.ToString());
  75. }
  76. private static string _Version = "Version:";
  77. private static string _Cluster = "Clustered:";
  78. private static int _clusterLength = _Cluster.Length;
  79. private static int _versionLength =_Version.Length;
  80. static private DataTable ParseServerEnumString(string serverInstances) {
  81. DataTable dataTable = new DataTable("SqlDataSources");
  82. dataTable.Locale = CultureInfo.InvariantCulture;
  83. dataTable.Columns.Add(ServerName, typeof(string));
  84. dataTable.Columns.Add(InstanceName, typeof(string));
  85. dataTable.Columns.Add(IsClustered, typeof(string));
  86. dataTable.Columns.Add(Version, typeof(string));
  87. DataRow dataRow = null;
  88. string serverName = null;
  89. string instanceName = null;
  90. string isClustered = null;
  91. string version = null;
  92. // Every row comes in the format "serverName\instanceName;Clustered:[Yes|No];Version:.."
  93. // Every row is terminated by a null character.
  94. // Process one row at a time
  95. foreach (string instance in serverInstances.Split('\0')) {
  96. string value = instance.Trim('\0'); // MDAC 91934
  97. if (0 == value.Length) {
  98. continue;
  99. }
  100. foreach (string instance2 in value.Split(';')) {
  101. if (serverName == null) {
  102. foreach(string instance3 in instance2.Split('\\')) {
  103. if (serverName == null) {
  104. serverName = instance3;
  105. continue;
  106. }
  107. Debug.Assert(instanceName == null);
  108. instanceName = instance3;
  109. }
  110. continue;
  111. }
  112. if (isClustered == null) {
  113. Debug.Assert(String.Compare(_Cluster, 0, instance2, 0, _clusterLength, StringComparison.OrdinalIgnoreCase) == 0);
  114. isClustered = instance2.Substring(_clusterLength);
  115. continue;
  116. }
  117. Debug.Assert(version == null);
  118. Debug.Assert(String.Compare(_Version, 0, instance2, 0, _versionLength, StringComparison.OrdinalIgnoreCase) == 0);
  119. version = instance2.Substring(_versionLength);
  120. }
  121. string query = "ServerName='"+serverName+"'";
  122. if (!ADP.IsEmpty(instanceName)) { // SQL BU DT 20006584: only append instanceName if present.
  123. query += " AND InstanceName='"+instanceName+"'";
  124. }
  125. // SNI returns dupes - do not add them. SQL BU DT 290323
  126. if (dataTable.Select(query).Length == 0) {
  127. dataRow = dataTable.NewRow();
  128. dataRow[0] = serverName;
  129. dataRow[1] = instanceName;
  130. dataRow[2] = isClustered;
  131. dataRow[3] = version;
  132. dataTable.Rows.Add(dataRow);
  133. }
  134. serverName = null;
  135. instanceName = null;
  136. isClustered = null;
  137. version = null;
  138. }
  139. foreach(DataColumn column in dataTable.Columns) {
  140. column.ReadOnly = true;
  141. }
  142. return dataTable;
  143. }
  144. }
  145. }