Difficulty: 3
Prerequisites: You have some knowledge about configuring database connections in the app. config and web. config files.
Conclusion
Vs2003: String connstr = system. configuration. configurationsettings. etettings ["sqlconnectionstring"];
Vs2005: String connstr = system. configuration. configurationmanager. connectionstrings ["sqlconnectionstring"]. tostring ();
System. configuration. dll must be referenced.
Definition
ConfigurationsettingsProvides support for reading configuration sections and public configuration settings during runtime.
ConfigurationmanagerProvides client applicationsProgramConfiguration File Access. This class cannot be inherited.
Configurationsettings. deleettings attributes
Obtain the read-only namevaluecollection in the configuration file application settings section. (Expired)
[Obsoleteattribute ("this method is obsolete, it has been replaced by system. Configuration! System. configuration. configurationmanager. configurettings ")]
Namespace: system. Configuration
Assembly: system (in system. dll)
--------------------------------------------
Configurationmanager class
Namespace: system. Configuration
Assembly: system. configuration (inSystem. configuration. dll) Using configurationmanager,This reference must be added !!
You can use the configurationmanager class to access the configuration information of computers and applications. Configurationmanager is the preferred method for processing client application configuration files. Other methods are not recommended. We recommend that you use the webconfigurationmanager class for Web applications.
Configurationmanager. deleettings attributes
Obtains the appsettingssection data configured by default for the current application.
Configurationmanager. connectionstrings attributes
Obtain the connectionstringssection data configured by default for the current application.
Example
<? XML version = "1.0" encoding = "UTF-8"?>
<Configuration>
<Configsections>
<Section name = "customsection"
Type = "samples. config. customsection,
Configurationmanager,
Version = 1.0.0.0,
Culture = neutralpublickeytoken = NULL"
Allowdefinition = "everywhere"
Allowexedefinition = "machinetoapplication"
Restartonexternalchanges = "true"/>
</Configsections>
<Connectionstrings>
<Add name = "connstr1"
Connectionstring = "localsqlserver: Data Source = 127.0.0.1; Integrated Security = sspi; initial catalog = aspnetdb"
Providername = "system. Data. sqlclient"/>
<Add name = "connstr2"
Connectionstring = "localsqlserver: Data Source = 127.0.0.1; Integrated Security = sspi; initial catalog = aspnetdb"
Providername = "system. Data. sqlclient"/>
</Connectionstrings>
<Deleetask>
<Add key = "etetting0"
Value = "Monday, February 28,200 5 1:27:59"/>
<Add key = "appsetting1"
Value = "Monday, February 28,200 5 1:36:40"/>
</Appsettings>
<Customsection filename = "default.txt" maxusers = "1000"
Maxidletime = "00:10:00"/>
</Configuration>
If you use deleetting in the web. config file
The configuration file's ettings section element contains a seriesName (key)/value (value)Yes. applications can be used for any purpose.
<Configuration xmlns = "http://schemas.microsoft.com/.NetConfiguration/v2.0">
<Deleetask>
<Add key = "connectionstring" value = ""/>
</Appsettings>
<System. Web>
...
</System. Web>
</Configuration>
Use connectionstrings in the web. config file
<Appsettings/>
<Connectionstrings>
<! -- Link string of the Access database -->
<Add name = "dbconnectionstring" connectionstring = "provider = Microsoft. Jet. oledb.4.0; Data Source = E: epaperoa. mdb" providername = "system. Data. oledb"/>
<! -- Link the connection string of the SQL2000 database -->
<Add name = "sqlconnectionstring" connectionstring = "Data Source = N4; initial catalog = epaperoa; persist Security info = true; user id = sa; Password = 123456" providername = "system. data. sqlclient "/>
</Connectionstrings>