How to get list of available SQL Servers using C# Code?

后端 未结 3 1969
盖世英雄少女心
盖世英雄少女心 2021-02-02 01:01

I have created a desktop application. On application launch I want to display the list of all available SQL Server instances on the local PC, and allow to choose a SQL Server na

3条回答
  •  自闭症患者
    2021-02-02 01:54

            //// Retrieve the enumerator instance, and then retrieve the data sources.
            SqlDataSourceEnumerator instance = SqlDataSourceEnumerator.Instance;
            DataTable dtDatabaseSources = instance.GetDataSources();
    
            //// Populate the data sources into DropDownList.            
            foreach (DataRow row in dtDatabaseSources.Rows)
                if (!string.IsNullOrWhiteSpace(row["InstanceName"].ToString()))
                    Model.DatabaseDataSourceNameList.Add(new ExportWizardChooseDestinationModel
                    {
                        DatabaseDataSourceListItem = row["ServerName"].ToString()
                            + "\\" + row["InstanceName"].ToString()
                    });
    

提交回复
热议问题