Ejemplo n.º 1
0
        override public DataTable GetDataSources()
        {
#if MONO
            timeoutTime = 0;
            throw new NotImplementedException();
#else
            (new NamedPermissionSet("FullTrust")).Demand(); // SQLBUDT 244304
            char[]        buffer  = null;
            StringBuilder strbldr = new StringBuilder();

            Int32 bufferSize = 1024;
            Int32 readLength = 0;
            buffer = new char[bufferSize];
            bool   more    = true;
            bool   failure = false;
            IntPtr handle  = ADP.PtrZero;

            RuntimeHelpers.PrepareConstrainedRegions();
            try {
                timeoutTime = TdsParserStaticMethods.GetTimeoutSeconds(timeoutSeconds);
                RuntimeHelpers.PrepareConstrainedRegions();
                try {} finally {
                    handle = SNINativeMethodWrapper.SNIServerEnumOpen();
                }

                if (ADP.PtrZero != handle)
                {
                    while (more && !TdsParserStaticMethods.TimeoutHasExpired(timeoutTime))
                    {
                        readLength = SNINativeMethodWrapper.SNIServerEnumRead(handle, buffer, bufferSize, ref more);
                        if (readLength > bufferSize)
                        {
                            failure = true;
                            more    = false;
                        }
                        else if (0 < readLength)
                        {
                            strbldr.Append(buffer, 0, readLength);
                        }
                    }
                }
            }
            finally {
                if (ADP.PtrZero != handle)
                {
                    SNINativeMethodWrapper.SNIServerEnumClose(handle);
                }
            }

            if (failure)
            {
                Debug.Assert(false, "GetDataSources:SNIServerEnumRead returned bad length");
                Bid.Trace("<sc.SqlDataSourceEnumerator.GetDataSources|ERR> GetDataSources:SNIServerEnumRead returned bad length, requested %d, received %d", bufferSize, readLength);
                throw ADP.ArgumentOutOfRange("readLength");
            }

            return(ParseServerEnumString(strbldr.ToString()));
#endif
        }
Ejemplo n.º 2
0
        public override DataTable GetDataSources()
        {
            new NamedPermissionSet("FullTrust").Demand();
            char[]        wStr      = null;
            StringBuilder builder   = new StringBuilder();
            int           pcbBuf    = 0x400;
            int           charCount = 0;

            wStr = new char[pcbBuf];
            bool   fMore   = true;
            bool   flag    = false;
            IntPtr ptrZero = ADP.PtrZero;

            RuntimeHelpers.PrepareConstrainedRegions();
            try
            {
                this.timeoutTime = TdsParserStaticMethods.GetTimeoutSeconds(30);
                RuntimeHelpers.PrepareConstrainedRegions();
                try
                {
                }
                finally
                {
                    ptrZero = SNINativeMethodWrapper.SNIServerEnumOpen();
                }
                if (ADP.PtrZero != ptrZero)
                {
                    while (fMore && !TdsParserStaticMethods.TimeoutHasExpired(this.timeoutTime))
                    {
                        charCount = SNINativeMethodWrapper.SNIServerEnumRead(ptrZero, wStr, pcbBuf, ref fMore);
                        if (charCount > pcbBuf)
                        {
                            flag  = true;
                            fMore = false;
                        }
                        else if (0 < charCount)
                        {
                            builder.Append(wStr, 0, charCount);
                        }
                    }
                }
            }
            finally
            {
                if (ADP.PtrZero != ptrZero)
                {
                    SNINativeMethodWrapper.SNIServerEnumClose(ptrZero);
                }
            }
            if (flag)
            {
                Bid.Trace("<sc.SqlDataSourceEnumerator.GetDataSources|ERR> GetDataSources:SNIServerEnumRead returned bad length, requested %d, received %d", pcbBuf, charCount);
                throw ADP.ArgumentOutOfRange("readLength");
            }
            return(ParseServerEnumString(builder.ToString()));
        }
        /// <summary>
        /// Retrieves a DataTable containing information about all visible SQL Server instances
        /// </summary>
        /// <returns></returns>
        internal static DataTable GetDataSources()
        {
            (new NamedPermissionSet("FullTrust")).Demand(); // SQLBUDT 244304
            char[]        buffer  = null;
            StringBuilder strbldr = new();

            int bufferSize = 1024;
            int readLength = 0;

            buffer = new char[bufferSize];
            bool   more    = true;
            bool   failure = false;
            IntPtr handle  = ADP.s_ptrZero;

            RuntimeHelpers.PrepareConstrainedRegions();
            try
            {
                long s_timeoutTime = TdsParserStaticMethods.GetTimeoutSeconds(ADP.DefaultCommandTimeout);
                RuntimeHelpers.PrepareConstrainedRegions();
                try
                { }
                finally
                {
                    handle = SNINativeMethodWrapper.SNIServerEnumOpen();
                    SqlClientEventSource.Log.TryTraceEvent("<sc.{0}.{1}|INFO> {2} returned handle = {3}.",
                                                           nameof(SqlDataSourceEnumeratorNativeHelper),
                                                           nameof(GetDataSources),
                                                           nameof(SNINativeMethodWrapper.SNIServerEnumOpen), handle);
                }

                if (handle != ADP.s_ptrZero)
                {
                    while (more && !TdsParserStaticMethods.TimeoutHasExpired(s_timeoutTime))
                    {
                        readLength = SNINativeMethodWrapper.SNIServerEnumRead(handle, buffer, bufferSize, out more);

                        SqlClientEventSource.Log.TryTraceEvent("<sc.{0}.{1}|INFO> {2} returned 'readlength':{3}, and 'more':{4} with 'bufferSize' of {5}",
                                                               nameof(SqlDataSourceEnumeratorNativeHelper),
                                                               nameof(GetDataSources),
                                                               nameof(SNINativeMethodWrapper.SNIServerEnumRead),
                                                               readLength, more, bufferSize);
                        if (readLength > bufferSize)
                        {
                            failure = true;
                            more    = false;
                        }
                        else if (readLength > 0)
                        {
                            strbldr.Append(buffer, 0, readLength);
                        }
                    }
                }
            }
            finally
            {
                if (handle != ADP.s_ptrZero)
                {
                    SNINativeMethodWrapper.SNIServerEnumClose(handle);
                    SqlClientEventSource.Log.TryTraceEvent("<sc.{0}.{1}|INFO> {2} called.",
                                                           nameof(SqlDataSourceEnumeratorNativeHelper),
                                                           nameof(GetDataSources),
                                                           nameof(SNINativeMethodWrapper.SNIServerEnumClose));
                }
            }

            if (failure)
            {
                Debug.Assert(false, $"{nameof(GetDataSources)}:{nameof(SNINativeMethodWrapper.SNIServerEnumRead)} returned bad length");
                SqlClientEventSource.Log.TryTraceEvent("<sc.{0}.{1}|ERR> {2} returned bad length, requested buffer {3}, received {4}",
                                                       nameof(SqlDataSourceEnumeratorNativeHelper),
                                                       nameof(GetDataSources),
                                                       nameof(SNINativeMethodWrapper.SNIServerEnumRead),
                                                       bufferSize, readLength);

                throw ADP.ArgumentOutOfRange(StringsHelper.GetString(Strings.ADP_ParameterValueOutOfRange, readLength), nameof(readLength));
            }
            return(ParseServerEnumString(strbldr.ToString()));
        }