• While developing
any application that has to interact with the data source in order to
retrieve,insert,update and delete the data from it, there are possibilities
that a developer can make mistakes while establishing connection to the data
source by hard coding the ConnectionString in the source code.
• Hard coding such
lines of code for an application consumes time and requires man power to make
an application work perfectly if the application has many web pages or windows
forms.
• The developer
has to connect the application to different data source in order to test it. In
such situation, hard coding may lead to errors in the source code because we
need to make changes for every ConnectionStrings in all web pages/windows forms
for a single data source which is definitely error prone and time consuming.
• So to overcome
such situation, we have ConnectionStrings class where we place the code of the
connection and give that connection; a meaningful name and use that name in
all web pages/windows form in order to establish connection to the data source
specified in the ConnectionStrings.
• To store the
connection string at a place we have got configuration file. We use web.config
file for web application and app.config file for windows application.
• These
configuration files contain the name of the connection string which we can use
in the web pages/ windows form to establish connection to the data source.
•
In configurationfile code section we have configuration section under which we use:-
> ConnectionString property to specify the server or system name, data source(database) name and the integrated security.
We can even use the userid and password if we use the SQL Server authentication to establish connection.
> We have provider name element to provide the name of the provider like SqlClient, Oracle etc.
> We have provider name element to provide the name of the provider like SqlClient, Oracle etc.
- Following lines of codes shows ConnectionStrings for both web.config and app.config configuration files:-
<configuration>
<connectionStrings>
<addname="MyDemo"
connectionString="Data
Source=testServer;Initial Catalog=demo;
Integrated
Security=SSPI;"
providerName="System.Data.SqlClient"/>
</connectionStrings>
</configuration>
• After setting
configuration files with the connection codes we need to make an application to
read that code from the configuration file.
• For this
purpose, we first need to import the Configuration
namespace which provides the necessary class and properties. We have ConfigurationManager class in this namespace which reads the
connection code from the configuration file using its property called ConnectionStrings which we use to
specify the name or index number of the connectionStrings associated with the
required data source.
• We can have
multiple connectionStrings inside the configuration file to manage connection
to different data sources. ConfigurationManager class returns the
connectionString as a string so we need to store it in a string variable as
shown below:-
String cs =
ConfigurationManager.ConnectionStrings[“MyDemo”].ConnectionString;
• So in the future
if we want that application to point to the different data source then we need
to change only the name of the server and the data source.
• The web.config file in web application gets added by
default and the app.config file needs to be added explicitly.
That's it....Thanks for reading this post...
Comments
Post a Comment