Asp.net: use classes to bind data sources to the backend, and asp.net backend

Source: Internet
Author: User

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 ();
}
}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.