Write the database connection information in the web. config file.

Source: Internet
Author: User

If we write the database connection information, that is, the database connection string, in web. config, it not only enhances program security, but also greatly facilitates program modification.

In the web. the keyword key is added to the config file through the <ettings> mark, but the ettings mark is usually placed in the <system. web> ..... </system. web> mark the outside. Example:

<Configration>

<Deleetask>

<Add key = "connString1"

Value = "server = localhost; user id = sa; pwd =; database = database name"/>

<Add key = "connString2"

Value = "provider = Microsoft. Jet. OleDb.4.0; Data Source = database path"/>

</AppSettings>

<System. web>

To reference these database connection strings in the code, you must first add. conFiguration namespace reference, which contains the ConfigurationSettings class, its static method ConfigurationSettings. the deleetaskproperty can be used to obtain the web. in the config file, set the value to string. For example:

Using System. Configuration;

String conn1 = ConfigurationSettings. receivettings ["connString1"];

String conn2 = ConfigurationSettings. receivettings ["connString2"];

SQLConnection myConn1 = new SQLConnection (conn1 );

OleDbConnection myConn2 = new OleDbConnection (conn2 );

.

.

.

</System. web>

</Configration>


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.