Asp. NET global variable settings and read methods _ Practical Tips

Source: Internet
Author: User
This article describes how global variables are used in two asp.net projects. web.config files and gloab files. The following are described separately:

Method One: Web.config file
--Settings:
Adding the keyword key to the web.config file is done through the <appSettings> tag, but the appSettings tag is usually placed in <system.web>.....</system.web > tag outside. Cases:
<configration>
<appSettings>
<add key= "connString1" value= "Server=localhost;user id=sa;pwd=;d atabase= 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 code, You need to add a reference to the System.Configuration namespace that contains the ConfigurationSettings class in the namespace, and its static method ConfigurationSettings.AppSettings property to get the web.config text The set of <appSettings> section in the piece, the Read value is string type. For example:

Using System.Configuration;
String conn1 = configurationsettings.appsettings["ConnString1"];
String conn2 = configurationsettings.appsettings["ConnString2"];
SqlConnection myConn1 = new SqlConnection (CONN1);
OleDbConnection myConn2 = new OleDbConnection (CONN2);

In VS2005, configurationsettings.appsettings can be replaced by configurationmanager.appsettings.

Method Two: Gloab file
--Settings:
Adding in the global file
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);

Recommended to use the first way! More 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.