You are writing a tool for SQL programmers by using C#, SQL Management Objects etc. You need to connect to a SQL Server instance and so you need to code a way to ask for the Server name, user credentials and so on. Why rewrite it each and every time? Simply add boB ‘The Tool Man’ Taylor’s Connect To SQL class, override one method and set your application icon and voila! You have a reusable resource that will allow you to connect to any SQL Server instance along with any SQL Azure Database server instance.
Here is an example of the Connect To SQL dialog being used in another project of mine.
Let’s examine the solution…
First we must add references to the libraries we will be using to connect to SQL Server instances.
There are two standard resources included in the ConnectToSQLBase class which you can replace in the constructor to customize the look for your application.
1. A default icon for use by the dialog
2. A default banner for use by the dialog
The key task in order to use ConnectToSQLBase in your project is to override the one virtual function
protected virtual void DoWork(SqlConnection con, SqlConnectionInfo sci)
In this method you have access to the open connection and any Connection Info you might need to perform your tasks.
In the next post we will examine the code that performs the authentication and connection features as well as builds the server list when the user selects <Browse for more…> option in the Server Name combo box.