The application configuration file is a standard XML file, and XML tags and attributes are case-sensitive. It can be changed as needed, and developers can use the configuration file to change the settings without having to recompile the application. In the app. config file, the root node is the configuration, and the node under the root node that can create the connection string, which is connectionstrings.
I. Configuration method for app. Config
1. Add an application configuration file (App. config) to the project, add the connection string node under the root node <connectionStrings></connectionStrings>
2. In the connection string node connectionstrings, you can add a connection string with the following code syntax:
Note: You can add multiple connection strings here, and you can choose to use different database connection strings as needed in your project
Differentiate with the name attribute
<add name= "Constr" connectionstring= "server=local;database=teaching;uid=sa;pwd=123456"/>
<add name= "CONSTR2" connectionstring= "server=locak;database=entityframework;uid=sa;pwd=123"/>
Two. About the use of App. Config in C #
The detailed code is as follows:
Call the database connection string in app. Config to summarize a sentence convenient to remember, do not laugh ~ ~ ~
(ConfigurationManager) The profile administrator selects the connectionstrings (connection string) node under the name XXX database and obtains the connection word assigned to the defined Constr.
String constr = configurationmanager.connectionstrings["Constr"]. ConnectionString;
Instantiates a connection object and puts the obtained string constr into the constructor
using (SqlConnection conn = new SqlConnection (CONSTRR))
{
Open connection
Conn. Open ();
}
Clumsy birds have essay: ADO about C # using the database connection string from app. Config