Ways to read database connection strings in Web.config during web development

Source: Internet
Author: User
Tags connectionstrings

Although the new ado.net in VS 2K5 enables us to develop faster, we can even use the database code at all without handwriting, but sometimes we have to get ConnectionString. Write it here ...

The

========web.config file is ===========
<?xml version= "1.0"
<configuration xmlns= "http://" schemas.microsoft.com/. netconfiguration/v2.0 "
 <configsections>
  <section name=" DataConfiguration " Type= "Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data "/>
 </configsections>
 < Connectionstrings>
  <remove name= "LocalSqlServer"/>
  <add name= " LocalSqlServer "connectionstring=" Data source=.;i ntegrated security=true;initial catalog=morning2 "Providername= System.Data.SqlClient"/>
 </ Connectionstrings>
 <dataconfiguration defaultdatabase= "LocalSqlServer"/>
 <!-- This sentence gives Enterpriselibrary.data a connection string with the
 <system.web>
 //
 </system.web>
</configuration>
==================================

Use System.Configuration when used:

Connectionstringsettingscollection CSSC = System.Web.Configuration.WebConfigurationManager.ConnectionStrings;
String constr = cssc["LocalSqlServer"]. ConnectionString;

----------------or-----------------
String constr = configurationmanager.connectionstrings["LocalSqlServer"]. ConnectionString;

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.