Webconfig Configuration:
Copy codeThe 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:
Copy codeThe 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>
MS web SQL <asp: GridView ID = "GridView1" runat = "server">
</Asp: GridView>
</Div>
</Form>
</Body>
</Html>
Background code:
Copy codeThe Code is 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 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;
}
}