Use of appsettings and connectionstrings.

Source: Internet
Author: User

Reference: http://www.cnblogs.com/mikemao/archive/2009/02/02/1382694.html

 

Appsettings is used in the ASP. net1.1 period. In. NET Framework 2.0, connectionstrings is added.

1. Use of connectionstrings

 

<Connectionstrings> <AddName= "Connectionstringname"Connectionstring= "Data Source = server name; initial catalog = database name; user id = user; Password = password"Providername= "System. Data. sqlclient" /> </Connectionstrings> 

Or:

<Connectionstrings> <AddName= "Connectionstringname"Connectionstring= "Sever = server name; database = database name; user id = user; Password = password"Providername= "System. Data. sqlclient" /> </Connectionstrings> 

You can also reference <% $ connectionstring: name %>.

2. Use of <deleettings>

 

<AddKey= "Connectionstringname"Value= "Data Source = server name or IP address; initial catalog = database name; persist Security info = false; user id = user; Password = password; packet size = 4096"> </Add> 

3. Differences

1) appsettings are commonly used in 2003 and connectionstrins are commonly used in 2005.
2) advantages of using connectionstring:
First, you can encrypt the connection string by using an encryption tool of Ms.
Second, data source controls that can be directly attached without having to writeCodeRead it and assign it to the control.
Third, you can easily change the database platform. For example, if you change to an Oracle database, you only need to modify the providername.
3) Use System. configuration. configurationmanager. deleetask[ "name"] to retrieve the value in <deleetask>.
In <connectionstrings>, use system. configuration. configurationmanager. connectionstrings ["name"] to retrieve the value.

4. Test

Create a new website in vs2005 and add the following code to the defaul page:

 Using  System. Web;  Using  System. Web. Security;  Using  System. Web. UI; Using  System. Web. UI. webcontrols;  Using  System. Web. UI. webcontrols. webparts;  Using  System. Web. UI. htmlcontrols;  Public   Partial   Class  _ Default: system. Web. UI. Page {  Protected   Void Page_load ( Object  Sender, eventargs e) {labconn. Text = Configurationmanager. connectionstrings [ "  Sitesqlserver  "  ]. Tostring (); labapp. Text = Configurationmanager. appsettings [ "  Sitesqlserver  "  ]. Tostring ();}} 

The Web. config code is as follows:

 <?  XML version = "1.0"  ?>  <! -- Note: In addition to manually editing this file, you can also use web management tools to configure applications.Program. You can use the "website"> "ASP. NET configuration" option in Visual Studio. The complete list of settings and comments is in machine. config. Comments. This file is usually located in/Windows/Microsoft. NET/framework/v2.x/config  -->  <  Configuration  >    <  Connectionstrings  >      <  Add  Name  = "Sitesqlserver"  Connectionstring = "Data Source = xuwei/sqlexpress; initial catalog = store; user id = dnndemo; Password = dnndemo"  Providername  = "System. Data. sqlclient"   />    </  Connectionstrings  >      <  Appsettings  >      <  Add  Key  = "Sitesqlserver"  Value = "Data Source = xuwei/sqlexpress; initial catalog = store; user id = dnndemo; Password = dnndemo"   />      </  Appsettings  >      <  System. Web  >          <! --  Set compilation DEBUG = "true" to insert the debugging symbol into the compiled page. However, this affects performance, so this value is set to true only during development.  -->          <  Compilation Debug  = "True"  />          <! --  In the <authentication> section, you can configure the Security Authentication mode used by ASP. NET to identify the passed-in user.  -->          <  Authentication  Mode  = "Windows"  />          <! -- If an error is not processed during request execution, you can configure the corresponding processing steps in the <mermerrors> section. Specifically, developers can use this section to configure HTML error pages to be displayed to replace the error stack trace. <Customerrors mode = "remoteonly" defaultredirect = "genericerrorpage.htm"> <error statuscode = "403" Redirect = "noaccess.htm"/> <error statuscode = "404" Redirect = "deny "/> </customerrors>  -->      </  System. Web  >  </  Configuration  > 

Of course, the premise is that two labels are added to the editing page, labconn and labapp.

Read the Web. config file connection string

StringConstring = configurationmanager. connectionstrings ["Sqlconnectionstring"]. Connectionstring;

 

 

 

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.