Previous Article: CYQ. The path to the lightweight Data layer)
Preface:
This article continues with the previous article, which describes the use of all related queries.
Summary:
1: Perform the Fill operation on a single row of data to perform the GetCount operation.
2: Select Operation for multi-row data operations
3: List-bound controls work with paging controls
4: Multi-Table query and view binding and custom SQL
Single Row Data Operations
I. Fill filling method, single row Query
Original method: public bool Fill (object where)
Example 1: direct upload ID
MAction action = new MAction (TableNames. Users );
If (action. Fill (888) // query single row data with ID = 888
{
Action. SetTo (lblUserName );
Action. Close ();
}
Example 2: Pass the where Condition
MAction action = new MAction (TableNames. Users );
If (action. Fill ("id = 888 or UserName = ' '") // query the data of a single row whose ID = 888 or UserName is ""
{
Action. SetTo (lblUserName );
Action. Close ();
}
Example 3: where condition with order
MAction action = new MAction (TableNames. Users );
If (action. Fill ("id> 888 order by id desc") // query the single row data with the largest ID in the result with ID> 888
{
Action. SetTo (lblUserName );
Action. Close ();
}
2. GetCount: Count the total number of statistics.
Original method: public int GetCount (string where)
Example 1:
MAction action = new MAction (TableNames. Users );
Int count = action. GetCount ("id> 10 ");
Action. Close ();
Multi-row data operations
Iii. Select multi-Data Query
Original method:
1: public MDataTable Select ()
2: public MDataTable Select (int PageIndex, int PageSize, string Where, out int RowCount)
Example 1:
MAction action = new MAction (TableNames. Users );
MDataTable tabme = action. Select (); // query all data
Action. Close ();
Example 2:
Int count; // This is the total number of returned records.
MAction action = new MAction (TableNames. Users );
MDataTable tabme = action. select (1st, "id> 10 order by username desc", out count); // query 10 records with id> 10 [pages, 10 data records per page, results are sorted by usename]
Action. Close ();
Additional instructions:
Select All data. The internal principle of the method is:
Public MDataTable Select ()
{
Int count;
Return Select (0, 0, "", out count );
}
List binding
4. Bind The GridView/DataList/Repeater
Example 1: Query all direct bindings
MAction action = new MAction (TableNames. Users );
MDataTable table = action. Select ();
Action. Close ();
GvUsers. DataSource = table;
GvUsers. DataBind ();
Example 2: bind the pagination control with the pagination control in the Post article [: CYQ. Data lightweight Data layer-based bug feedback, optimization suggestions, and latest framework download]
Public void BindData ()
{
Int count;
MAction action = new MAction (TableNames. Users );
MDataTable table = action. Select (Pager1.PageIndex, Pager1.PageSize, "id> 10", out count );
Action. Close ();
GvUsers. DataSource = table;
GvUsers. DataBind ();
Pager1.Count = count; // you can specify the total number of records.
Pager1.BindName = "BindData"; // method name
}
Example 3: bind the page control with other Get Methods
Public void BindData ()
{
Int count;
MAction action = new MAction (TableNames. Users );
MDataTable table = action. Select (Pager1.PageIndex, Pager1.PageSize, "id> 10", out count );
Action. Close ();
GvUsers. DataSource = table;
GvUsers. DataBind ();
Pager1.Count = count;
}
Note:
If the page control you are using is more complex than the preceding usage, You can optimize or discard the original page control.
Multi-Table query and binding
V. View Mode
Example 1: Just like a table operation, the only difference is that the table name is changed to the view name.
Public void BindData ()
{
Int count;
MAction action = new MAction (ViewNames. V_Users );
MDataTable table = action. Select (Pager1.PageIndex, Pager1.PageSize, "id> 10", out count );
Action. Close ();
GvUsers. DataSource = table;
GvUsers. DataBind ();
Pager1.Count = count;
Pager1.BindName = "BindData ";
}
Vi. Custom multi-Table SQL statements
Example 1:
Public void BindData ()
{
String customTable = "(select u. *, m. Body from Users u left join Message m on u. ID = m. UserID) v ";
Int count;
MAction action = new MAction (customTable );
MDataTable table = action. Select (Pager1.PageIndex, Pager1.PageSize, "id> 10", out count );
Action. Close ();
GvUsers. DataSource = table;
GvUsers. DataBind ();
Pager1.Count = count;
Pager1.BindName = "BindData ";
}
Note:
To facilitate management, custom SQL statements are not directly written on the interface. You can create a new project to manage custom SQL statements in a unified manner.
Statement:
After reading this example, you should understand the query. It is easy to implement the paging control.
For other usage, see the next article: The name is not fixed.