Asp.net| Object | data | Show in learning DataSet objects, we encounter a concept: DataView. This object represents a way to view the data tables in the dataset by default by arranging the data in tabular form, and the data is sorted according to the order in which the data is read from the datasheet, usually and sort when the database is involved order or the arrangement basis (ascending or descending, etc.) defined in the SQL statement.
When using DataView, you can use different sorting criteria (sort), filter conditions (filter), or Lookup method (search) depending on the actual situation.
For the following example, we will use the DataView object to display different data in the same database.
In the Adodotnet Web project, add a Web Form named dataview.aspx and add three button controls, respectively Button1 (text= "Show All Data"), Button2 (text= "Show Boy Data Only") and Button3 (text= "Show Girl Data Only"), add a DataGrid control, named Dataview.aspx, the design window as shown in Figure 8.11.
Figure 8.11 dataview.aspx Design window
Switch to dataview.aspx HTML window, the main HTML code is as follows:
<form id= "Form1" method= "POST" runat= "Server" >
<font face= "Song body" > Filtering Data Using DataView
<asp:button id= "Button1" style= "Z-INDEX:101; left:8px; Position:absolute; top:40px "runat=" Server "
Text= "Show All Data" width= "105px" height= "32px" ></asp:Button>
<asp:button id= "Button2" style= "z-index:102; left:112px; Position:absolute; top:40px "runat=" Server "
Text= "Show boy data Only" width= "105px" height= "32px" ></asp:Button>
<asp:button id= "Button3" style= "z-index:103; left:216px; Position:absolute; top:40px "runat=" Server "
Text= "Show girl data Only" width= "height=" ></asp:Button>
<asp:datagrid id= "DataGrid1" style= "z-index:104; left:8px; Position:absolute; top:80px "runat=" Server "
Width= "328px" height= "128px" font-size= "X-small" ></asp:DataGrid></FONT>
</form>
The logical code for dataview.aspx is as follows:
'--------Code begin-------
Imports system.web
Imports System.Data
Public Class DataView
Inherits System.Web.UI.Page
#Region the code generated by the Web Forms Designer
' The code generated by the form designer is omitted here to save space
#End Region
Public RowFilter as String = "NULL"
' Declares the global variable for the Web form to hold the filter condition of the DataView object,
' Initialize defaults to NULL, i.e. does not filter
Private Sub Page_Load (ByVal sender as System.Object, ByVal e as System.EventArgs) Handles MyBase.Load
' The user code to place the initialization page here
' The process of calling or going data
GetData ()
End Sub
' Write a generic process getdata that uses or goes to data and generates views based on filter conditions
Sub GetData ()
' The user code to place the initialization page here
Dim connstr as String ' declares database connection characters
Dim Mycon as Oledb.oledbconnection
' Because the AEECSS database is used, the Oledconnention object is declared
Dim myCMD as Oledb.oledbdataadapter
' Declare DataAdapter Object
Dim MySQL as String
' declares the SQL string of command commands
' Try
ConnStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data source=" + Server.MapPath (".") + "\studentinfor.mdb"
' Assign a value to a connection string
Mycon = New oledb.oledbconnection (connstr)
' Instantiate the Connection object
MySQL = "SELECT * FROM Student"
' Set up an SQL statement that queries all content in the database
myCMD = New oledb.oledbdataadapter (mysql, mycon)
' Instantiate the DataAdapter object
Dim myDataSet as Data.dataset
' Declare a DataSet object
myDataSet = New Data.dataset
' Instantiate a DataSet object
myCMD. Fill (myDataSet, "student")
' Fills the myDataSet object with the Fill method of the DataAdapter object,
' The data table name that is populated in myDataSet is student
Dim DV as Data.dataview
DV = New Data.dataview (mydataset. Tables ("student"))
' myDataSet. Tables (0) indicate which data table The view is based on.
' Table (0) indicates the index entry of the datasheet in the dataset, starting from 0,
' This statement equates to DV = New Data.dataview (mydataset. Tables ("student"))
' Determine the filter criteria and then generate the view based on the filter criteria
Select Case RowFilter
Case "NULL"
' If it is ' null ', that is to consider
Case "Male"
Dv. RowFilter = "sex= ' Man"
' Show boy data only
Case "female"
Dv. RowFilter = "sex= ' Woman"
' Show girl data only
End Select
DataGrid1.DataSource = DV
' DataGrid1 control to add a data source, that is, a myDataSet object
' DATAGRID1 control specifies data table
Datagrid1.databind ()
' Catch ex as Exception
' If an error occurs, the output error message
' Response.Write ("The program has an error, the error message is as follows: <br>" & Ex. Message)
' Finally
Mycon. Close () Closes data connection
' End Try
End Sub
Private Sub button1_click (ByVal sender as System.Object, ByVal e as System.EventArgs) Handles Button1.Click
RowFilter = "NULL"
GetData ()
End Sub
Private Sub button2_click (ByVal sender as System.Object, ByVal e as System.EventArgs) Handles Button2.click
RowFilter = "Male"
GetData ()
End Sub
Private Sub button3_click (ByVal sender as System.Object, ByVal e as System.EventArgs) Handles Button3.click
RowFilter = "female"
GetData ()
End Sub
End Class
'-------Code end-------------
After you save the compilation, preview it through the browser, as shown in Figure 8.12.
Figure 8.12 Dataview.aspx Running effect
Of course, if just to filter data, you can add "where sex=" in the SQL statement. DataView is rich in functionality and can be bound to the same data table using a variety of composite controls. Interested readers can write code to verify that space is limited and there is no longer an example to illustrate.
8.4 Summary of this chapter
This chapter introduces the Ado.net data access technology in detail. Ado. NET database access technology is a new generation of data access technology, it uses off-line way to access data from the database, improve the efficiency and scalability of distributed applications. Ado. NET's key technical dataset data model, in Ado.net, all programs are data-centric, through the support of XML, make ado.net have good expansibility and compatibility. With the memory database technology of dataset class, the overhead of server resources is minimized. DataSet object through the transformation of data format to XML, can easily penetrate the company firewall, or carry out the data transmission of heterogeneous nodes.
In ASP.net, the Ado.net database access technology is the core and fundamental of data manipulation, and the application environment of enterprise database can be built efficiently in combination with ASP.net data-bound control. In the next chapter, we'll learn more about data-bound controls.