Reference first
Using system.configuration;//configuration file
Using System.Data.SqlClient;
I'm using SQL Server. SA user password also SA, development tools for visual studio2012
Front Code:
1<connectionStrings>2<!--<add Name="defaultconnection"Providername="System.Data.SqlClient"connectionstring="data source= (LocalDb) \v11.0;initial catalog=aspnet-configuration file link database -20150623105832;integrated Security=sspi; attachdbfilename=| datadirectory|\aspnet-configuration file link Database -20150623105832.mdf"/>-->3<add name="myconn"connectionstring="Data Source=.;i Nitial Catalog=northwind; Persist Security info=true; User Id=sa; Password=sa; Database=test1"/>4</connectionStrings>
Background code:
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingsystem.web;usingSystem.Web.UI;usingSystem.Web.UI.WebControls;usingSystem.Data;//using MySql.Data.MySqlClient;//MysqlusingMySql.Data.MySqlClient;//SQLusingSystem.Configuration;//configuration fileusingSystem.Data.SqlClient;namespaceconfiguration file link Database { Public Partial classTest:System.Web.UI.Page {protected voidPage_Load (Objectsender, EventArgs e) { //get the connection string in the configuration file stringConstr = configurationmanager.connectionstrings["myconn"]. ToString (); using(SqlConnection con=NewSqlConnection (CONSTR)) { stringSelstr ="select name from user1 where 1=1"; SqlCommand com=NewSqlCommand (Selstr,con); Con. Open (); SqlDataReader SDR=com. ExecuteReader (); while(SDR). Read ()) {LISTBOX1.ITEMS.ADD (sdr[0]. ToString ()); }} response.write (Constr); } }}
Thanks to clerk's predecessors ' experience: http://www.cnblogs.com/yagzh2000/archive/2013/03/13/2957266.html
C # ASP. NET configuration file connect to SQL database