SQL Connection: Connecting to Multiple Databases - Connection Using Dynamic Properties of the SQLConnection Control
(Page 4 of 4 )
The information stored in the web.config file can also be used by placing a 'SQLConnection' control from the 'Tools'. Since there are two server connections, two controls (see picture below) should be added to the user interface in the design plane. In the Properties window of the SQLConnection control, you need to configure the DynamicProperties in the Configuration node. By clicking the ConnectionString item of this node, a dialog box pops up asking for 'Mapping' instruction between the "key" in the configuration file and SQLConnection1.ConnectionString.

After adding the 'SQLConnection' control, the key in the web.config file can be associated with the connection as shown. For example, in the (Dynamic Properties) node,

the ConnectionString is SQLConnection2.ConnectionString, and its mapping to the web.config stored value is entered by clicking the empty space in the (Advanced) field as shown in the next picture, and picking up the relevant item. Similarly the SQLConnection1 is associated with the 'books.'

With these settings in place, the code gets much simpler, as shown below.
Private Sub Button1_Click(ByVal sender As Object, ByVal e As
System.EventArgs)_
Handles Button1.Click
If DropDownList1.SelectedItem.ToString Is "books" Then
SqlConnection1.Open()
TextBox1.Text = SqlConnection1.ConnectionString
TextBox2.Text = SqlConnection1.State
Else
If DropDownList1.SelectedItem.ToString Is "info" Then
SqlConnection2.Open()
TextBox1.Text = SqlConnection2.ConnectionString
TextBox2.Text = SqlConnection2.State
End If
End If
End Sub
The result for one of the choices is shown for completeness.

Summary
It is possible to connect any of the SQL 2000 servers whose key/value pairs are stored in the web.config file. It is also possible to use an external file with the same key/value information for the servers. One has to make sure the login for the various databases accessed are in place for successful connection. Using dynamic properties of the SQLConnection the amount of code can be reduced.
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |