Пример #1
0
        /// <summary>
        /// Creates a Computer Parameter from an established SQL Connection
        /// </summary>
        /// <param name="Connection">The connection to use</param>
        public ComputerParameter(System.Data.SqlClient.SqlConnection Connection)
        {
            InputObject = Connection;
            ComputerParameter tempParam = new ComputerParameter(Connection.DataSource);

            ComputerName = tempParam.ComputerName;
        }
Пример #2
0
        /// <summary>
        /// Generates a Computer Parameter object from string
        /// </summary>
        /// <param name="ComputerName">The name to use as input</param>
        public ComputerParameter(string ComputerName)
        {
            InputObject = ComputerName;

            if (string.IsNullOrWhiteSpace(ComputerName))
            {
                throw new ArgumentException("Computername cannot be empty!");
            }

            string tempString = ComputerName.Trim();

            if (ComputerName == ".")
            {
                this.ComputerName = "localhost";
                return;
            }

            if (UtilityHost.IsLike(tempString, "*.WORKGROUP"))
            {
                tempString = Regex.Replace(tempString, @"\.WORKGROUP$", "", RegexOptions.IgnoreCase);
            }

            if (UtilityHost.IsValidComputerTarget(tempString))
            {
                this.ComputerName = tempString;
                return;
            }

            // Named Pipe path notation interpretation
            if (Regex.IsMatch(tempString, @"^\\\\[^\\]+\\pipe\\([^\\]+\\){0,1}sql\\query$", RegexOptions.IgnoreCase))
            {
                try
                {
                    this.ComputerName = Regex.Match(tempString, @"^\\\\([^\\]+)\\").Groups[1].Value;
                    return;
                }
                catch (Exception e)
                {
                    throw new ArgumentException(String.Format("Failed to interpret named pipe path notation: {0} | {1}", InputObject, e.Message), e);
                }
            }

            // Connection String interpretation
            try
            {
                System.Data.SqlClient.SqlConnectionStringBuilder connectionString = new System.Data.SqlClient.SqlConnectionStringBuilder(tempString);
                ComputerParameter tempParam = new ComputerParameter(connectionString.DataSource);
                this.ComputerName = tempParam.ComputerName;

                return;
            }
            catch (ArgumentException ex)
            {
                string name = "unknown";
                try
                {
                    name = ex.TargetSite.GetParameters()[0].Name;
                }
                catch
                {
                }
                if (name == "keyword")
                {
                    throw;
                }
            }
            catch (FormatException)
            {
                throw;
            }
            catch { }

            throw new ArgumentException(String.Format("Could not resolve computer name: {0}", ComputerName));
        }