Project--asp.net setting and reading of global variables (web. config and Gloab)

Source: Internet
Author: User

Method one: Web. config file
--Settings:
Adding keywords to the Web. config file key is implemented by the <appSettings> tag, but the appSettings tag is usually placed in the <system.web>.....</system.web > Mark out. 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 your code, You need to first add a reference to the System.Configuration namespace that contains the ConfigurationSettings class, whose static method ConfigurationSettings.AppSettings property gets the Web. config text Set in the <appSettings> section of the piece, the value read is a 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:
In the global file, 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);

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.