What is it?
Control used in a sample application
You have to give a username and password to retrieve all hosted databases. Note: SQL_Detect_dll.zip is the assembly containing the
SQLServersDetector control. SQL_Detect_source.zip contains the source code of the control, along with a sample application using the control.
It's a simple window control that can load all of the SQL Server instances in your local network and then load all of the databases on the selected server. I used
SqlDataSourceEnumerator to load servers from the network.
I was writing code in C# that was related to the manipulation of data with more than one database. I found that it was difficult for a user to type the address of a server and database into the text input fields and then connect to them for further manipulation because there is always the chance of the sever name or path being misspelled. If a user by mistake inputs the wrong spelling of the server or database name and then clicks connect, it takes a long time to decide that the path is not valid and throw an exception. Then the user has to to check the spelling again and give the input again, which I think is always irritating. So, I decided to detect the server instances automatically and load databases from the server and so on...
Using the Control
The control is easy to use, as is any other Windows control in .NET. You just have to add a reference of the DLL that is provided with this article. Alternatively, add the project to your solution and add a reference of the project
SqlServersDetector to your project. Then the control will appear in your Toolbox just like this.
Now you can simply drag and drop it onto your form.
Properties of the Control
You can use the following properties of the control if you need them.
lblConnectionString.Text = sqlServersDetector1.DBConnectionString;
sqlServersDetector1.MainLabel = "All Servers";
sqlServersDetector1.ServersLabel = "Servers";
sqlServersDetector1.DatabasesLabel = "Database";
string server = sqlServersDetector1.SelectedServer;
string database = sqlServersDetector1.SelectedDatabase;
Using the Example Solution Provided with the Article
If any beginner doesn't know how to use the solution, just download the ZIP file SQL_Detect_source.zip and unzip it. Then open the SQLServerDetectionControl.sln solution file and run the project named
TestAppForSQLServerControl. If you are still facing problems using this control, then feel free to ask.
- 7 November, 2007 -- Original version posted