SQLCommand.executepageReader [Repost] in ADO.NET 2.0

xiaoxiao2021-03-05  24

In .NET 2.0 PDC or Beta1, you can see that the SQLCOMMAND object adds an ExecutePageReader method, which implements the features of the paging read data. For page read data, in ADO.NET1.1 (Of course 2.0 is also suitable) generally frequently constructed SQL statement implementation:

SqlDataReader getPage (int Pagenumber, int PageSize) {// Pagenumber: Page number from 0 // PageSize: String command = string.format ("SELECT *" "Products ORDER BY ProductID) AS t1 WHERE ProductID NOT IN" "(SELECT TOP {1} ProductID FROM Products ORDER BY ProductID)", pageSize * (pageNumber 1), pageSize * pageNumber); SqlConnection conn = new SqlConnection ( " server = .; database = Northwind; Trusted_Connection = yes "); SqlCommand cmd = new SqlCommand (command, conn); conn.Open (); SqlDataReader dr = cmd.ExecuteReader (CommandBehavior.CloseConnection); return dr;}

Sometimes for better implementation of page performance, you can also use the stored procedure to establish a temporary table, but the principle is basically almost. In ADO.NET 2.0 PDC / Beta1, use SqlCommand.executePageReader to patch:

SqlDataReader GetPageReader (int pageNumber, int pageSize) {int startIndex = (pageNumber - 1) * pageSize; String command = "SELECT * FROM Products"; SqlConnection conn = new SqlConnection ( "server = .; database = Northwind; Trusted_Connection = yes" ); Sqlcommand cmd = new sqlcommand (command, conn); conn.open (); SqlDataReader DR = cmd.executepageReader (commandbehavior.closeconnection), startIndex, pagesize; Return DR;}

Finally Binding 5 lines of data on page 2 (ProductID from 6 to 10) to GridView1:

GridView1.datasource = getPage (1, 5); gridview1.database ();

转载请注明原文地址:https://www.9cbs.com/read-36307.html

New Post(0)