Configure database connections within Webconfig.xml

Source: Internet
Author: User

A very simple webconfig.xml configuration database connection, which is also reproduced online. I was making some notes. Welcome pupils to attend to, the great God directly ignore it.

The Webconfig.xml configuration reads as follows:

<configuration>

<connectionStrings>

<add name= "ApplicationServices"

connectionstring= "Data source=. Sqlexpress;integrated Security=sspi; attachdbfilename=| Datadirectory|aspnetdb.mdf; User Instance=true "

Providername= "System.Data.SqlClient"/>

<add name= "tesr_db" connectionstring= "User Id=sa;data source=127.1.1.1;password=12345678; Persist security info=true; "Providername=" System.Data.SqlClient "/>

</connectionStrings>

......

</configuration>

Persist security Info=true: Whether confidential information is kept, do does not save password information when the database connection succeeds. True to save, false means not saved.

Mainly is Name= "tesr_db", will be the backstage corresponding configurationmanager.connectionstrings to take to

The above is SQL Sever, Oracle is <add name= "tesr_db" connectionstring= "DATA source=127.1.1.1; password=12345678; USER Id=sa "providername=" Oracle.DataAccess.Client "/>

Test whether to connect code:

(in the background to refer to Webconfig.xml configuration information, you need to introduce a using System.Configuration library to call to Webconfig.xml configuration information)

String myvar1 = configurationmanager.connectionstrings["tesr_db"]. ToString ();

SqlConnection conn = new SqlConnection (MYVAR1);

Try

{

Conn. Open ();

Response.Write ("connected successfully");

}

catch (Exception ex)

{

Response.Write ("Connection failed");

}

Here and obviously tell you, may have noticed the name= "tesr_db" in the background is configurationmanager.connectionstrings["tesr_db"]. ToString (); is consistent. A project that corresponds to multiple databases or other information is set up using a different name.

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.