Detailed explanation of NET AppSettings and ConnectionStrings use cases

  • 2021-12-04 09:49:10
  • OfStack

Directory 1. Use of ConnectionStrings 2. < appSettings > Use of 3. Difference 4. Test

AppSettings was used in ASP. NET 1.1, and ConnectionStrings was added in NET Framework 2.0.

1. Use of ConnectionStrings


<connectionStrings>   
<add name="ConnectionStringName" connectionString="Data Source= Server name ;Initial Catalog= Database 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= Users ;Password= Password "   
providerName="System.Data.SqlClient" />   
</connectionStrings>

You can also refer to this on the page < %$ ConnectionString:Name% > .

2. < appSettings > Use of


<add key="connectionstringName" value="data source= Server name or IP;initial catalog= Database name ;persist security info=False;user id= Users ;password= Password ;packet size=4096">   
</add>

3. Difference

1) AppSettings is commonly used in 2003 and ConnectionStrins is commonly used in 2005.

2) Benefits of using ConnectionString:

1: The connection string can be encrypted by using one encryption tool of MS.   2: A data source control that can be bonded directly, without having to write code to read it and assign it to the control.   3: The database platform can be easily replaced, such as Oracle database, only need to modify providerName.

3) Written in < appSettings > Retrieve values with System. Configuration. ConfigurationManager. AppSettings ["name"] in.  
Written in < ConnectionStrings > Retrieve values with System. Configuration. ConfigurationManager. ConnectionStrings ["name"] in.

STEP 4 Test

Create a new website in VS 2005, and then 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 code for web. config is as follows:


<?xml version="1.0"?>  
<!--   
     Attention :  In addition to manually editing this file, you can also use the    
    Web  Management tools to configure the settings of the application. You can use the  Visual Studio  In   
      "Website" -> " Asp<a href="http://lib.csdn.net/base/dotnet" rel="external nofollow"  class='replace_word' title=".NET Knowledge base " target='_blank' style='color:#df3434; font-weight:bold;'>.NET</a>  Configure option.   
     A complete list of settings and comments is found in the    
    machine.config.comments  This file is usually located in the    
    /Windows/Microsoft.Net/Framework/v2.x/Config  Medium   
-->  
<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>  
        <!--   
             Settings  compilation debug="true"  Insert debug symbols into   
             In the compiled page. But because this will    
             Impact performance, so this value is only used during development    
             Set to  true .   
        -->  
        <compilation debug="true"/>  
        <!--  
             Pass  <authentication>  Section can be configured  ASP.NET  Used    
             Secure authentication mode,   
             To identify the incoming user.    
        -->  
        <authentication mode="Windows"/>  
        <!--  
             If an unhandled error occurs during the execution of the request,   
             Then pass the  <customErrors>  Section can configure the corresponding processing steps. Specifically,   
             This section allows developers to configure   
             To display  html  Error page   
             To replace the error stack trace.   
  
        <customErrors mode="RemoteOnly" defaultRedirect="GenericErrorPage.htm">  
            <error statusCode="403" redirect="NoAccess.htm" />  
            <error statusCode="404" redirect="FileNotFound.htm" />  
        </customErrors>  
        -->  
    </system.web>  
</configuration>

Of course, the premise is that two lable are added to the editing page, which are labConn and labApp respectively.

Read Web. Config file connection string

string conString = ConfigurationManager.ConnectionStrings["SQLCONNECTIONSTRING"].ConnectionString;

Related articles: