Webconfig configuration:
Copy Code code as follows:
<connectionStrings>
<add name= "MSSQL" connectionstring= "Data source=localhost;initial catalog=test; User id=sa;password=sa; "providername=" System.Data.SqlClient "/>
</connectionStrings>
Front-Office aspx:
Copy Code code 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 ">
<title>DB</title>
<body>
<form id= "Form1" runat= "Server" >
<div>
MS sql<asp:gridview id= "GridView1" runat= "Server" >
</asp:GridView>
</div>
</form>
</body>
Background code:
Copy 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)
{
DataSet dsmssql = GetDataSet ("select * from [Table]", "MSSQL");
This. Gridview1.datasource = Dsmssql;
This. Gridview1.databind ();
}
Protected DataSet GetDataSet (string sqlcommand,string DB)
{
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;
}
}