Example #1
0
        /// <summary>
        /// 获取局域网内的所有数据库服务器名称
        /// </summary>
        /// <returns>服务器名称数组</returns>
        public static string[] GetLocalSqlServerNamesWithSqlClientFactory()
        {
            DbDataSourceEnumerator dbDataSourceEnumerator = SqlClientFactory.Instance.CreateDataSourceEnumerator();

            if (dbDataSourceEnumerator != null)
            {
                DataTable         dataSources = dbDataSourceEnumerator.GetDataSources();
                DataColumn        column2     = dataSources.Columns["ServerName"];
                DataColumn        column      = dataSources.Columns["InstanceName"];
                DataRowCollection rows        = dataSources.Rows;
                string[]          array       = new string[rows.Count];
                for (int i = 0; i < array.Length; i++)
                {
                    string str2 = rows[i][column2] as string;
                    string str  = rows[i][column] as string;
                    if (((string.IsNullOrEmpty(str))) || ("MSSQLSERVER" == str))
                    {
                        array[i] = str2;
                    }
                    else
                    {
                        array[i] = str2 + @"\" + str;
                    }
                }
                Array.Sort(array);

                return(array);
            }

            return(new string[0]);
        }
Example #2
0
        public static List <DataSourceInfo> GetDataSources(string providerInvariantName)
        {
            List <DataSourceInfo> result = new List <DataSourceInfo>();

            DbProviderFactory      providerFactory = DbProviderFactories.GetFactory(providerInvariantName);
            DbDataSourceEnumerator dsEnum          = providerFactory.CreateDataSourceEnumerator();

            if (dsEnum != null)
            {
                DataTable servers = dsEnum.GetDataSources();
                foreach (DataRow row in servers.Rows)
                {
                    var item = new DataSourceInfo()
                    {
                        Host        = DBCast <string>(row["ServerName"]),
                        Instance    = DBCast <string>(row["InstanceName"]),
                        IsClustered = new[] { "yes", "true" }.Contains((DBCast <string>(row["IsClustered"]) ?? string.Empty).ToLower()),
                        Version     = DBCast <string>(row["Version"])
                    };

                    //check if the instance name is empty
                    if (!string.IsNullOrEmpty(item.Instance))
                    {
                        //append the instance name to the server name
                        item.Host += String.Format("\\{0}", item.Instance);
                    }

                    result.Add(item);
                }
            }

            return(result);
        }
Example #3
0
        private void button9_Click(object sender, EventArgs e)
        {
            DbProviderFactory _ftry;

            _ftry = DbProviderFactories.GetFactory("Oracle.DataAccess.Client");
            DbDataSourceEnumerator _datasourceEnum = _ftry.CreateDataSourceEnumerator();
            DataTable _datasources = _datasourceEnum.GetDataSources();
            int       _counter;

            for (_counter = 0; _counter <= _datasources.Rows.Count - 1; _counter++)
            {
                MessageBox.Show(_datasources.Rows[_counter]["ServiceName"].ToString());
            }
        }
Example #4
0
        /// <summary>
        /// Retrieve a service listing.
        /// </summary>
        /// <returns></returns>
        public ServiceDefinitionList GetServices()
        {
            DbDataSourceEnumerator e = dataFactory.ProviderFactory.CreateDataSourceEnumerator();

            /*
             * ServerName
             * InstanceName
             * IsClustered
             * Version
             * FactoryName
             *
             */
            return(e.GetDataSources().ToServiceDefinitionList());
        }
Example #5
0
        /// <summary>
        /// retrieve a list of service available
        /// </summary>
        /// <param name="databaseProvider"></param>
        /// <returns></returns>
        public static ServiceDefinitionList GetService(DatabaseProvider databaseProvider)
        {
            /*
             * ServerName
             * InstanceName
             * IsClustered
             * Version
             * FactoryName
             *
             */
            string                 providerString  = Providers.ToProvider(databaseProvider);
            DbProviderFactory      providerFactory = DbProviderFactories.GetFactory(providerString);
            DbDataSourceEnumerator enumerator      = providerFactory.CreateDataSourceEnumerator();

            return(enumerator.GetDataSources().ToServiceDefinitionList());
        }
Example #6
0
        // Data Source Enumerator
        // Go to app.config
        public void DataSourceEnumerator()
        {
            if (factory.CanCreateDataSourceEnumerator)
            {
                DbDataSourceEnumerator dsenum = factory.CreateDataSourceEnumerator();
                DataTable dt = dsenum.GetDataSources();

                // Print the first column/row entry in the DataTable
                Console.WriteLine(dt.Columns[0] + " : " + dt.Rows[0][0]);
                Console.WriteLine(dt.Columns[1] + " : " + dt.Rows[0][1]);
                Console.WriteLine(dt.Columns[2] + " : " + dt.Rows[0][2]);
                Console.WriteLine(dt.Columns[3] + " : " + dt.Rows[0][3]);
                Console.WriteLine(dt.Columns[4] + " : " + dt.Rows[0][4]);
            }
            else
            {
                Console.Write("Data source enumeration is not supported by provider");
            }
        }
    private static void ListServers(DbProviderFactory factory)
    {
        // This procedure is provider-agnostic, and can list
        // instances of any provider's servers. Of course,
        // not all providers can create a data source enumerator,
        // so it's best to check the CanCreateDataSourceEnumerator
        // property before attempting to list the data sources.
        if (factory.CanCreateDataSourceEnumerator)
        {
            DbDataSourceEnumerator instance =
                factory.CreateDataSourceEnumerator();
            DataTable table = instance.GetDataSources();

            foreach (DataRow row in table.Rows)
            {
                Console.WriteLine("{0}\\{1}",
                                  row["ServerName"], row["InstanceName"]);
            }
        }
    }
        private string[] ReadTNSNames()
        {
            string[] databases = new string[1];

            DbProviderFactory factory = DbProviderFactories.GetFactory("Oracle.DataAccess.Client");

            int i = 1;

            if (factory.CanCreateDataSourceEnumerator)
            {
                DbDataSourceEnumerator dsenum = factory.CreateDataSourceEnumerator();
                DataTable dt = dsenum.GetDataSources();
                foreach (DataRow row in dt.Rows)
                {
                    Array.Resize <string>(ref databases, i);
                    databases[i - 1] = row[0].ToString().ToUpper();
                    i += 1;
                }
            }
            return(databases);
        }
Example #9
0
    static void Main()
    {
        string ProviderName = "Oracle.ManagedDataAccess.Client";

        DbProviderFactory factory = DbProviderFactories.GetFactory(ProviderName);

        if (factory.CanCreateDataSourceEnumerator)
        {
            DbDataSourceEnumerator dsenum = factory.CreateDataSourceEnumerator();
            DataTable dt = dsenum.GetDataSources();

            // Print the first column/row entry in the DataTable
            Console.WriteLine(dt.Columns[0] + " : " + dt.Rows[0][0]);
            Console.WriteLine(dt.Columns[1] + " : " + dt.Rows[0][1]);
            Console.WriteLine(dt.Columns[2] + " : " + dt.Rows[0][2]);
            Console.WriteLine(dt.Columns[3] + " : " + dt.Rows[0][3]);
            Console.WriteLine(dt.Columns[4] + " : " + dt.Rows[0][4]);
        }
        else
        {
            Console.Write("Data source enumeration is not supported by provider");
        }
    }
Example #10
0
        public List <string> GetServidores()
        {
            DbProviderFactory dbProviderFactory = DbProviderFactories.GetFactory("System.Data.SqlClient");

            if (dbProviderFactory.CanCreateDataSourceEnumerator)
            {
                DbDataSourceEnumerator dbDataSourceEnumerator = dbProviderFactory.CreateDataSourceEnumerator();

                if (dbDataSourceEnumerator != null)
                {
                    DataTable dt;
                    _servidor = new List <string>();
                    dt        = dbDataSourceEnumerator.GetDataSources();

                    foreach (DataRow row in dt.Rows)
                    {
                        _servidor.Add(row[0].ToString());
                    }
                }
            }

            return(_servidor);
        }
Example #11
0
        public static DataTable GetServer()
        {
            try
            {
                DataTable         dt = new DataTable();
                DbProviderFactory dbProviderFactory = DbProviderFactories.GetFactory("System.Data.SqlClient");

                if (dbProviderFactory.CanCreateDataSourceEnumerator)
                {
                    DbDataSourceEnumerator dbDataSourceEnumerator = dbProviderFactory.CreateDataSourceEnumerator();

                    if (dbDataSourceEnumerator != null)
                    {
                        dt = dbDataSourceEnumerator.GetDataSources();
                    }
                }
                return(dt);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Example #12
0
        static void findDataSources(DbProviderFactory f)
        {
            DbDataSourceEnumerator e = f.CreateDataSourceEnumerator();
            DataTable t = e.GetDataSources();

            Trace.WriteLine("here");

            for (int col = 0; col < t.Columns.Count; col++)
            {
                if (col > 0)
                {
                    Trace.Write(",");
                }
                Trace.Write(t.Columns[col].ColumnName);
            }
            Trace.WriteLine(string.Empty);
            for (int row = 0; row < t.Rows.Count; row++)
            {
                for (int col = 0; col < t.Columns.Count; col++)
                {
                    if (col > 0)
                    {
                        Trace.Write(",");
                    }
                    Trace.Write(t.Rows[row][col].ToString());
                }
                Trace.WriteLine(string.Empty);
            }
            Trace.WriteLine(string.Empty);
            // uses chooses a Data Row r
            //DataRow r = t.Rows[0];
            //string dataSource = (string) r["ServerName"];
            //if (r[InstanceName] != null)
            //    dataSource += ("\\" + r["InstanceName"]);
            //// this method is defined below
            //RewriteConnectionStringAndUpdateConfigFile(f, dataSource);
        }
Example #13
0
        /// <summary>
        /// Retrieves information about the SQL Server instances running in current network.
        /// </summary>
        /// <returns>A list of 'SQLServerInstance' objects, which contains information about each SQL Server instance.</returns>
        public static List <SQLServerInstance> GetNetworkSQLServerInstances()
        {
            DbProviderFactory factory = DbProviderFactories.GetFactory("System.Data.SqlClient");

            List <SQLServerInstance> ret = new List <SQLServerInstance>();

            if (factory.CanCreateDataSourceEnumerator)
            {
                DbDataSourceEnumerator dataSourceEnumerator = factory.CreateDataSourceEnumerator();

                if (dataSourceEnumerator != null)
                {
                    DataTable dt = dataSourceEnumerator.GetDataSources();

                    foreach (DataRow dr in dt.Rows)
                    {
                        SQLServerInstance t = _CreateSQLServerInstance(dr);
                        ret.Add(t);
                    }
                }
            }

            return(ret);;
        }
        public IEnumerable <string> Execute(TnsNamesQuery query)
        {
            if (string.IsNullOrEmpty(query.ProviderInvariantName))
            {
                throw new ArgumentNullException(nameof(query.ProviderInvariantName));
            }

            IEnumerable <string> tnsNames = Enumerable.Empty <string>();
            DbProviderFactory    factory  = DbProviderFactories.GetFactory(query.ProviderInvariantName);

            if (factory.CanCreateDataSourceEnumerator)
            {
                DbDataSourceEnumerator dsenum = factory.CreateDataSourceEnumerator();
                if (dsenum != null)
                {
                    DataTable dt   = dsenum.GetDataSources();
                    DataRow[] rows = dt.Select(null, "InstanceName", DataViewRowState.CurrentRows);

                    tnsNames = rows.Select(row => (string)row["InstanceName"]);
                }
            }

            return(tnsNames);
        }