If you have ever wanted to create an application in which you can configure a custom SQL connection string, one of the key features of this configuration module in your application, should be a combo box in which you can show a list of all visible SQL Servers on your network.
To do this:
- Drag a combo box onto your application from the ToolBox.
- Right-Click on your Project Name in the Solution Explorer, and select Add Reference.
- In the Add Reference dialog box that appears, go to the COM tab.
- Scroll down and search for "Microsoft SQLDMO Object Library". Select it, and click OK.
- Open a Form.Load subroutine for the form on which the combo box resides, and add the following code:
Compile and run. If you have at least one SQL Server on your network (at least SQL Server 2000) it will be listed in the combo box's drop down list.Code:'List all available SQL Servers in the combo box Dim SQList As SQLDMO.NameList Dim SQLApp As New SQLDMO.Application Dim x As Integer SQList = SQLApp.ListAvailableSQLServers For x = 1 To SQList.Count ComboBoxServers.Items.Add(SQList.Item(x)) Next
This will work just as well in C# as it does in VB. Just change the code standard as necessary.
Enjoy.