Click here to Skip to main content
15,867,141 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi
I want to know how the connectionstring is retrieved from this code

C#
Application[Constants.appSettingsDataFolderPath] = ConfigurationManager.AppSettings[Constants.appSettingsDataFolderPath]


Thanks
Akila
Posted
Updated 22-Jun-11 1:29am
v2

if you use .net 2.0 you can only declare connectionstring within <appsettings> in web.config.

but if you use .net 3.5 means you can directly specify connectionstring within <connectionstring> tags in webconfig.
---------------------------------------------------------------------------------
in .NET 3.5:

<connectionstrings>
  <add>
    name="ConnectionStringname";
    connectionString="Data Source=serverName;Initial
    Catalog=yourdb;Persist Security Info=True;User
    ID=userName;Password=password";>

</add></connectionstrings>

you can call it in program by using,
string connStr = ConfigurationManager.ConnectionStrings["ConnectionStringname"].ConnectionString;

-----------------------------------------------------------------------------------
in .NET 2.0:

<appsettings>
    <add key="ConnectionInfo" value="server=(local);database=yourdb;Integrated Security=SSPI">
  </add></appsettings>

you can call it in program by using,
string connectionInfo = ConfigurationSettings.AppSettings["ConnectionInfo"];
 
Share this answer
 
Comments
RaviRanjanKr 24-Jun-11 14:46pm    
Have a my 5 for nice answer :)
CS1401 24-Jun-11 23:57pm    
Thankx...

.!.
Put a debug point here trace using F10 put the value in quick watch window
 
Share this answer
 
Comments
Manfred Rudolf Bihy 24-Jun-11 12:20pm    
Have a 5 on me for humor!
RaviRanjanKr 24-Jun-11 14:46pm    
My 5 too :)
Hi,

The Connection String which you wrote in the Web.config or app.config in the Appsettings section is retrieved using Configuration Manager Class.

Thanks,
K.Vigneshwar.
 
Share this answer
 
If you need help on the AppSettings property - see here[^].
This property returns an AppSettingsSection object contains the contents of the configuration file's appSettings section.
 
Share this answer
 
Use Configuration Manager class to retrieve the connection string which you wrote in app (or)web.config.
 
Share this answer
 
Comments
RaviRanjanKr 24-Jun-11 14:47pm    
Nice Answer RK, My 5 :)
[no name] 27-Jun-11 0:11am    
Thanks Ravi.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900