SQL instance Listing in ComboBox

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Does anyone know how to search for Instances of SQL Server running on your network through code to populate a drop down like is done in the SQL Server Query Analyzer, and then once an instance is selected, list the databases on that Instance?
 
John,

In order to do this, you have a number of options.

To get the list of SQL server instances on the network, you can call the
NetServerEnum function through the P/Invoke layer. It will allow you to
return a list of SQL server instances on your network. Also, you can use
the SQLDMO library through COM interop. There is a COM class that has a
method that will do this (although the name escapes me).

Once you have the names of your SQL servers, you can use the classes in
the System.Data.SqlClient namespace to connect to the database and then
issue queries against the system tables to get the databases on the
instance. Also, you can set a reference to the ADOX library to do the same
thing through an object-oriented model.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

John Barr said:
Does anyone know how to search for Instances of SQL Server running on your
network through code to populate a drop down like is done in the SQL Server
Query Analyzer, and then once an instance is selected, list the databases on
that Instance?
 
John Barr said:
Does anyone know how to search for Instances of SQL Server running on your
network through code to populate a drop down like is done in the SQL Server
Query Analyzer, and then once an instance is selected, list the databases on
that Instance?

RegistryKey RK;
RK =
egistry.LocalMachine.OpenSubKey( "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\M
icrosoft SQL Server" );
Console.WriteLine(Convert.ToString(RK.GetValue("InstalledInstances")));

You do need to parse the value return before populating your list box.
 
Back
Top