views:

37

answers:

1

Hello everyone,

I am using VSTS 2010 + C# + .Net 4.0 to develop an ASP.Net application using SQL Server 2008 Enterprise as database. I am learning someone else's code. I notice code like this, I am not sure whether the usage is correct and so I come here to ask for advice.

In the code, I see some code like this, I want to know whether using such method to read connection string from web.config is correct?

ConfigurationManager.ConnectionStrings["DBConnectinString"].ConnectionString

and such code is used to read connection string from web.config like below, please notice that connection string is defined outside of system.web section.

<?xml version="1.0"?>
<configuration>
  <configSections>
       <!--this section is empty-->
  </configSections>
  <appSettings>
    ...... content of appSettings
  </appSettings>
  <connectionStrings>
    <add name="DBConnectinString" connectionString="data Source=.;uid=foo;pwd=foo;database=FOODB" providerName="System.Data.SqlClient"/>
  </connectionStrings>
<system.web>
... ... content of system.web
</system.web>
</configuration>

thanks in advance, George

+4  A: 

That is the standard place to define connection strings in web.config, yes.

See this for more info: http://msdn.microsoft.com/en-us/library/ms178411.aspx

Andrew Barber
Thanks, question answered!
George2