App.config "Application configuration file", it is actually a standard XML file, but. NET class library has encapsulated the method of reading this file. can be very convenient to use. Look at the use process.
1. Right-click your project name in Solution Explorer, add > New Item, select application configuration file in the list, and the default file is app.config. Are you sure.
2. Open app.config, the initial XML code is:
<xml version= "1.0" encoding= "Utf-8" >
<configuration>
</configuration>
Set your connection string in the following format
<xml version= "1.0" encoding= "Utf-8" >
<configuration>
<connectionStrings>
<!-- The server here can write (local) or write a dot .-->
<add name= "sqlconnstring connectionstring=" server=.; database= database name; uid= user name; pwd= password " />
</connectionStrings>
</configuration>
Where name is the identity name of the connection string (similar to an alias). ConnectionString is your connection string. You can write multiple add nodes.
Note: The format is fixed
3. Right-click "Reference" in your project in Solution Explorer, add System.Configuration Reference. In your code, you need both:
Using System.Configuration;
4. began to acquire the
String connstr= configurationmanager.connectionstrings["MyConn"]. ConnectionString;
SqlConnection cn = new SqlConnection (CONNSTR);
cn. Open ();
At this point the database is open, you can write the appropriate code