For example:
App. config
<? XML version = "1.0" encoding = "UTF-8"?>
<Configuration>
<Configsections>
</Configsections>
<Connectionstrings>
<Add name = "emsconnectionstring" connectionstring = "Data Source =. \ sqlexpress; initial catalog = db_cms; Integrated Security = true"
Providername = "system. Data. sqlclient"/>
</Connectionstrings>
</Configuration>
Web. config
<? XML version = "1.0"?>
<Configuration>
<Appsettings/>
<Connectionstrings>
<Add name = "emsconnectionstring" connectionstring = "Data Source =. \ sqlexpress; initial catalog = db_cms; Integrated Security = true"/>
</Connectionstrings>
<System. Web>
<! --
Set compilation DEBUG = "true" to insert debugging symbols
Compiled pages. However, this
Performance is affected, so this value is only available during development.
Set to true.
-->
<Compilation DEBUG = "true">
</Compilation>
<! --
You can use the <authentication> section to configure ASP. NET
Identifies
Security Authentication mode.
-->
<Authentication mode = "Windows"/>
<! --
If an unprocessed error occurs during request execution,
You can configure the corresponding processing steps in the <mermerrors> section. Specifically,
This section allows developers to configure
HTML error page 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 = "filenotfound.htm"/>
</Customerrors>
-->
</System. Web>
</Configuration>
C # Read connectionstring
1. String STR = system. configuration. configurationmanager. connectionstrings ["emsconnectionstring"]. connectionstring. tostring (); or
String Str
= System. configuration. configurationsettings. receivettings ["emsconnectionstring"]. tostring ();
Note: connectionstrings ["emsconnectionstring"] here the brackets contain the value corresponding to name = "emsconnectionstring,
If name = "emsconnectionstring", it should be: connectionstrings ["emsconnectionstring"].
2. If you use settings. settings in the Properties column, you can use the following methods to read the settings:
String STR = properties. settings. Default. emsconnectionstring;
This statement is the same as in the preceding example. Strings can be obtained.
3. In another way, specify a specific file
Private Static string getconnectionstringsconfig (string connectionname)
{
Execonfigurationfilemap filemap = new execonfigurationfilemap ();
Filemap. execonfigfilename = application. startuppath + @ "\ sss.exe. config ";
Configuration Config = configurationmanager. openmappedexeconfiguration (filemap, configurationuserlevel. None );
Return config. connectionstrings. connectionstrings [connectionname]. connectionstring. tostring ();
}
Vbnet reading
1. Dim Str
As string = system. configuration. configurationsettings. receivettings ("emsconnectionstring ")
2. My. settings. emsconnectionstring
C # dynamically read the config file
Public String getconfigvalue (string path, string appkey)
{
Xmldocument xdoc = new xmldocument ();
Try
{
Xdoc. Load (PATH );
// Xdoc. Load (system. Windows. Forms. application. executablepath + ". config ");
Xmlnode xnode;
Xmlelement xelem;
Xnode = xdoc. selectsinglenode ("// appsettings ");
Xelem = (xmlelement) xnode. selectsinglenode ("// Add [@ key = '" + appkey + "']");
If (xelem! = NULL) then
Return xelem. getattribute ("value ");
Else
Return "";
}
Catch (exception)
{
Return "";
}
}