.net Appsettings and connectionstrings use case explanation

Time:2022-7-24

Appsettings is used in asp.net1.1, and is used in Net framework 2.0, new connectionstrings

1. Use of connectionstrings

<connectionStrings>   
<add name= "connectionstringname" connectionstring= "data source= server name; initial catalog= database name; user id= user; password= password"   
providerName="System.Data.SqlClient" />   
</connectionStrings>

Or:

<connectionStrings>   
<add name= "connectionstringname" connectionstring= "sever= server name; database= database name; user id= user; password= password"   
providerName="System.Data.SqlClient" />   
</connectionStrings>

You can also refer to <% $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>

3. Differences

1) Appsettings is commonly used in 2003 and connectionstrins is commonly used in 2005

2) Benefits of using connectionstring:

  • First: you can encrypt the connection string by using an encryption tool of MS.
  • Second: the data source control can be directly defined without writing code to read it and assign it to the control.
  • Third: you can easily change the database platform. If you change to Oracle database, just modify the providername.

3) Write in <appsettings > and use system.configuration.configurationmanager.appsettings[“name”] to retrieve values.  
Write in <connectionstrings> and use system.configuration.configurationmanager.connectionstrings[“name”] to retrieve values.

4. Test

Create a new website in VS2005, 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 of web.config is as follows:

<?xml version="1.0"?>  
<!--   
    Note: in addition to manually editing this file, you can also use   
    Web management tools to configure application settings. You can use the  
     "Website" - > "asp<a href=“ http://lib.csdn.net/base/dotnet " rel="external nofollow"  class='replace_word' title=". Net knowledge base "target='\u blank'style='color:\df3434; font weight: bold; >.Net</a> configuration" option.  
    The complete list of settings and comments is in   
    machine.config. In comments, this file is usually located in   
    /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 debugging symbols  
            In the compiled page. But because this will   
            Affect performance, so only use this value during development   
            Set to true.  
        -->  
        <compilation debug="true"/>  
        <!--  
            ASP can be configured through the <authentication> section Net   
            Secure authentication mode,  
            To identify the incoming user.   
        -->  
        <authentication mode="Windows"/>  
        <!--  
            If an unhandled error occurs during the execution of the request,  
            Then the corresponding processing steps can be configured through the <customerrors> section. Specifically,  
            This section allows developers to configure  
            HTML error page to display  
            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 to add two labels in the editing page, labconn and labapp respectively.

Read web.config file connection string

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

This is about Net Appsettings and connectionstrings use cases detailed article introduced here, more relevant Net Appsettings and connectionstrings. Please search for previous articles of developeppaer or continue to browse the relevant articles below. I hope you will support developeppaer in the future!

Recommended Today

JS generate guid method

JS generate guid method https://blog.csdn.net/Alive_tree/article/details/87942348 Globally unique identification(GUID) is an algorithm generatedBinaryCount Reg128 bitsNumber ofidentifier , GUID is mainly used in networks or systems with multiple nodes and computers. Ideally, any computational geometry computer cluster will not generate two identical guids, and the total number of guids is2^128In theory, it is difficult to make two […]