Define global variables for strings in Web. config

Source: Internet
Author: User

This document describes how to use global variables in two ASP. NET projects. The Web. config file and the gloab file. Method 1: In the Web. config file -- settings: Add a keyword key to the Web. config file using the <etettings> tag?
......

-----------------------------------------------------------------------------

This document describes how to use global variables in two ASP. NET projects. The Web. config file and the gloab file. Method 1: Web. config file

-- Settings:

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>

</System. Web>

</Configration>

-- Read:

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 );

In vs2005, configurationsettings. configurettings can be changed to configurationmanager. configurettings.

Method 2: gloab File

-- Settings:

Add

Protected void session_start (Object sender, eventargs E)

{

Session ["sqlconnectionstring"] = "uid = username; Pwd = password; database = mytest;

Server = localhost; Connect timeout = 300 ";

}

-- Read:

Application in code:

String strconnection = session ["sqlconnectionstring"]. tostring ();

Sqlconnection_1 = new sqlconnection (strconnection );

The first method is recommended! Flexible

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.