Asp.net: use classes to bind data sources to the backend, and asp.net backend
// Encapsulate it into one
Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Web;
Using System. Web. UI;
Using System. Web. UI. WebControls;
Using System. Data. SqlClient;
Using System. Data;
Using System. Configuration;
/// <Summary>
/// BindData Summary
/// </Summary>
Public class bindData
{
// Declare the parameters of a query statement
Private string _ selectSQL;
// Parameter attributes
Public string SelectSQL
{
Get {return _ selectSQL ;}
Set {_ selectSQL = value ;}
}
// Rewrite Method
Public bindData (string selsql)
{
This. _ selectSQL = selsql;
}
// BindDate () method with return value DataView ()
Public DataView BindDate (){
String s = "Data Source = (LocalDB) \ v11.0; AttachDbFilename = | DataDirectory | \ xlgameguide. mdf; Integrated Security = True; Connect Timeout = 30 ";
SqlConnection sqlConnection = new SqlConnection (s );
SqlDataAdapter sqlDataAdapter = new SqlDataAdapter (this. _ selectSQL, s );
DataSet ds = new DataSet ();
SqlDataAdapter. Fill (ds, "table ");
DataView dv = ds. Tables ["table"]. DefaultView;
Return dv;
}
}
// Webpage background code
Protected void Page_Load (object sender, EventArgs e)
{
// It must be in! IsPostBack, otherwise it will be fixed multiple times after each refresh
If (! IsPostBack)
{
// Create an object bd
BindData bd = new bindData ("SELECT * FROM [BBSgame]");
// Create a new gv to receive the corresponding DataView
DataView gv = bd. BindDate ();
// Data Binding
ListView1.DataSource = gv;
ListView1.DataBind ();
}
}