private DataTable GetCurrentRecords(int page) { DataTable dt = new DataTable(); using (SqlConnection con = new SqlConnection(ConnecString)) { SqlCommand cmd = new SqlCommand(); if (page == 1) { cmd = new SqlCommand("Select TOP " + PageSize + " [OrderID],[CustomerID],[ShipVia],[Freight] FROM [Northwind].[dbo].[Orders] ORDER BY [OrderID]", con); } else { //利用 SQL 語法來切換資料 int PreviousPageOffSet = (page - 1) * PageSize;
cmd = new SqlCommand("Select TOP " + PageSize + " [OrderID],[CustomerID],[ShipVia],[Freight] " + "FROM [Northwind].[dbo].[Orders] WHERE [OrderID] "+ "NOT IN " + "(Select TOP " + PreviousPageOffSet +" [OrderID] from [Northwind].[dbo].[Orders] ORDER BY [OrderID] ) " , con); } SqlDataAdapter da = new SqlDataAdapter(cmd); da.Fill(dt); lbCurrentPage.Text = "第 " + CurrentPageIndex + " 頁"; } return dt; }
///<summary> /// Gets the data. ///</summary> ///<returns></returns> private DataTable getData() { using (SqlConnection con = new SqlConnection(_connecString)) { con.Open(); using (SqlCommand cmd = new SqlCommand("SELECT [OrderID],[CustomerID],[ShipVia],[Freight] FROM [Northwind].[dbo].[Orders] ORDER BY [OrderID]", con)) { SqlDataAdapter da = new SqlDataAdapter(cmd); DataTable dt = new DataTable(); da.Fill(dt); return dt; } } }