SQL Connection: Connecting to Multiple Databases
(Page 1 of 4 )
As businesses may have more than one SQL Server holding their data, it is of interest to know the technique of connecting to mulitple servers through saving connection information in the configuration of the application. In fact, it is a recurring question in several forums. This tutorial addresses this question in detail.
Using application settings in Web.Config
In this tutorial, the storing of a connection string in a web.config file and its usage for connecting to multiple databases will be considered. One advantage of storing information in a web.config file is that all testing can be done with a development database, and when going into production all that needs to be changed is the web.config file information. The web.config file is secure because its accessibility is limited. The manner in which the web.config file is set up to store database information, assuming a connection to the pubs database on the local server accessed with SQL authentication, is as follows:
<Configuration>
<appSettings>
<add key="ConnectionString"
value="DATABASE=pubs;Server=localhost;UID=sa:"/>
</appSettings>
</Configuration>
It is also possible to store the same database information in an external file data.config and link to it. In this case the key/value pair information is carried in the external file. Once the web.config carries this information it can be retrieved by using code as follows:
Code in C#:
string strConn=ConfigurationSettings.AppSettings["ConnectionString"];
Code in VB.NET
DIM conn as String conn=ConfigurationSettings.AppSettings("ConnectionString")
Next: Saving multiple connections in Web.config >>
More MS SQL Server Articles
More By Jayaram Krishnaswamy