A very simple webconfig.xml configuration database connection, which is also reproduced online. I was making some notes. Welcome pupils to attend to, the great God directly ignore it.
The Webconfig.xml configuration reads as follows:
<configuration>
<connectionStrings>
<add name= "ApplicationServices"
connectionstring= "Data source=. Sqlexpress;integrated Security=sspi; attachdbfilename=| Datadirectory|aspnetdb.mdf; User Instance=true "
Providername= "System.Data.SqlClient"/>
<add name= "tesr_db" connectionstring= "User Id=sa;data source=127.1.1.1;password=12345678; Persist security info=true; "Providername=" System.Data.SqlClient "/>
</connectionStrings>
......
</configuration>
Persist security Info=true: Whether confidential information is kept, do does not save password information when the database connection succeeds. True to save, false means not saved.
Mainly is Name= "tesr_db", will be the backstage corresponding configurationmanager.connectionstrings to take to
The above is SQL Sever, Oracle is <add name= "tesr_db" connectionstring= "DATA source=127.1.1.1; password=12345678; USER Id=sa "providername=" Oracle.DataAccess.Client "/>
Test whether to connect code:
(in the background to refer to Webconfig.xml configuration information, you need to introduce a using System.Configuration library to call to Webconfig.xml configuration information)
String myvar1 = configurationmanager.connectionstrings["tesr_db"]. ToString ();
SqlConnection conn = new SqlConnection (MYVAR1);
Try
{
Conn. Open ();
Response.Write ("connected successfully");
}
catch (Exception ex)
{
Response.Write ("Connection failed");
}
Here and obviously tell you, may have noticed the name= "tesr_db" in the background is configurationmanager.connectionstrings["tesr_db"]. ToString (); is consistent. A project that corresponds to multiple databases or other information is set up using a different name.