Overview
The configuration file is a file installed on the computer along with the installer, which stores the parameters required when the installed application is running. You can open the configuration file and modify the parameters, in this way, you can achieve a certain purpose without modifying the code. For example, we can change the character used to connect the configuration file to the SQL Server database to the character used to connect to the Oracle database. In this way, if the application has compiled the Oracle database code during development, then the program can run normally when the Oracle database is running.
Configuration File addition 1. To which layer?
In the overview, it is explained that the configuration file and the installed application must exist on the computer that is installed at the same time to make full use of it. Therefore, you must add the configuration file to the interface layer when adding the configuration file, if it is added elsewhere, an error occurs.
2. How to add?
Configuration File Usage 1. How to modify the configuration file
The configuration file has its corresponding writing format. As long as we write the file in the corresponding format, the key name is connstr, and the key value is the equal sign content after the value, you only need to use connstr for use.
<? XML version = "1.0" encoding = "UTF-8"?> <Configuration> <! -- Start of the configuration file --> <! -- The added node is used to modify the database connection --> <appsettings> <! -- The IP address, username, and password are your own. For example, <add key = "connstr" value = "Data Source = 192.168.27.27; initial catalog = charge; user id = admin; Pwd = abcedef "> </Add> </appsetdef> <! -- Other nodes can be added below --> </configuration> <! -- End of configuration file -->
2. How to Use the content in the configuration file
Add this reference to the project using the configuration file: system. configuration, as shown in figure
3. Retrieve the configuration file code
Dim strconnection as string = configurationmanager. receivettings ("connstr") 'can be used in the following method, but the 'dim connstr as string = configurationsettings. receivettings ("connstr ")