AppSettings and connectionstrings application and difference explanation

Source: Internet
Author: User
Tags oracle database connectionstrings

The Apps Tutorial ettings is an ASP tutorial. net1.1 period, in. NET Framework 2.0, new connectionstrings specifically used to get configuration files in the ConfigurationManager class

The appsettings and connectionstrings sections are used in the following ways:

Using System.Configuration;
public partial class _default:system.web.ui.page
{
protected void Page_Load (object sender, EventArgs e)
{
Label1.Text = configurationmanager.connectionstrings["Connectionstringsname"].tostring ();
Label2.Text = configurationmanager.appsettings["Appsettingsname"].tostring ();
}
}

AppSettings is used during the ASP.net tutorial 1.1, and in the. NET Framework 2.0, connectionstrings is added.

1.<connectionstrings>
<connectionstrings>
<add name= "connectionStringName" connectionstring= "data source= server name; Initial catalog= database tutorial name; user id= users; password= Password
Providername= "System.Data.SqlClient"/>
</connectionstrings>

Or

<connectionstrings>
<add name= "connectionStringName" connectionstring= sever= server name; database= database name; user id= username; password= password "
Providername= "System.Data.SqlClient"/>
</connectionstrings>

In <connectionstrings>, in the page can also quote <%$ Connectionstring:name%>.

2.<appsettings>

<add key= "connectionStringName" value= "data source= server name or ip;initial catalog= database name; Persist Security Info=false;user id= user; password= password; packet size=4096 ">
</add>

1) appsettings is commonly used in 2003, and Connectionstrins is commonly used in 2005.
2 The benefits of using ConnectionString:
First: The connection string can be encrypted, using one of MS's encryption tools.
Second: Directly state the data source control, but do not have to write code read out and then assign to the control.
Third: Easy to replace the database platform, such as the Oracle database, just modify the providername.
3 write in <appsettings > retrieve the value with system.configuration.configurationmanager.appsettings["name".
Write in <connectionstrings> retrieve values with system.configuration.configurationmanager.connectionstrings["name".

In the. NET Framework 2.0, The ConfigurationManager class has added two properties appsettings and connectionstrings specifically to get the data for the profiles appsettings and connectionstrings sections, using the following methods:

Using System.Configuration;
public partial class _default:system.web.ui.page
{
protected void Page_Load (object sender, EventArgs e)
{
Label1.Text = configurationmanager.connectionstrings["Connectionstringsname"].tostring ();
Label2.Text = configurationmanager.appsettings["Appsettingsname"].tostring ();
}
}

The difference between <connectionstrings> and <appsettings>
1.<connectionstrings>
<connectionstrings>
<add name= "syxtconnectionstring" connectionstring= "Data source=leike;initial catalog=syxt;user Id=sa;password=sa "
Providername= "System.Data.SqlClient"/>
</connectionstrings>


2.<appsettings>:

<add key= "connectionstring1" value= "Data source=192.168.123.201;initial catalog=webmedlink;persist Security Info=false;user id=sa;password=ksdchagd2004;packet size=4096 ">
</add>

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.