This article mainly introduces the ASP.net connection database reading data samples, we refer to the use of the bar
Webconfig configuration: code is as follows: <connectionStrings> <add name= "MSSQL" connectionstring= "Data source= Localhost;initial catalog=test; User Id=sa;password=sa "providername=" System.Data.SqlClient "/> </connectionStrings> foreground aspx: The code is as follows: <%@ Page language= "C #" autoeventwireup= "true" codefile= "DB.aspx.cs" inherits= "DB"%> <! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd "> <html xmlns=" http://www.w3.org/1999/xhtml "> <head runat=" Server "> <title>DB< /title> </head> <body> <form id= "Form1" runat= "Server" > <div> &nbs P MS sql<asp:gridview id= "GridView1" runat= "Server" > &L t;/asp:gridview> </div> </form> </body> </html> Background code : Code as follows: Using System; Using System.Collections.Generic; Using System.Web; Using System.Web.UI; Using System.Web.UI.WebControls; Using System.Data; Using system.data.common;//remember to have a using using System.Configuration; public partial class DB:System.Web.UI.Page { protected void Page_Load (object sender, EventArgs e) & nbsp { DataSet dsmssql = GetDataSet ("select * from [Table]", "MSSQL"); this. Gridview1.datasource = Dsmssql; this. Gridview1.databind (); } protected DataSet getdataset (String sqlcommand,string DB) { &NBS P DbProviderFactory dbproviderfactory = Dbproviderfactories.getfactory ( CONFIGURATIONMANAGER.CONNECTIONSTRINGS[DB]. ProviderName); DbConnection dbconnection = Dbproviderfactory.createconnection (); dbconnection.connectionstring = Configurationmanager.connectionstrings[dB]. ConnectionString; DataSet ds = new DataSet (); DbCommand DbCommand = Dbproviderfactory.createcommand (); dbcommand.connection = dbconnection; DbDataAdapter dbdataadapter = Dbproviderfactory.createdataadapter (); Dbcommand.commandtext = SqlCommand; Dbdataadapter.selectcommand = DbCommand; Dbdataadapter.fill (DS); return DS; }}