PDA

View Full Version : ساخت دکمه next & back با کد نویسی



sg.programmer
سه شنبه 06 دی 1390, 15:57 عصر
سلام
چطور میشه دکمه ای (Back - Next) ساخت که هر دفعه مثلا تعداد 10 فیلد از دیتابیس را نمایش بده - فابلیت Back هم داشته باشه.



Select top(10) from tbl

با هردفعه کلید next تعداد 10 فیلد بعدی نمایش داده گردد

sg.programmer
سه شنبه 06 دی 1390, 16:32 عصر
البته در sql 2005 دستور Row Number و در mysql دستور Limit هست ولی برای SQLServer 2000 من همنچین چیزی میخوام؟

sg.programmer
سه شنبه 06 دی 1390, 16:40 عصر
یک نمونه پیدا کردم کردم اگه دوستان روش بهتری دارن قرار بدند تا استفاده کنیم.
http://www.codeproject.com/KB/database/paging_various_databases.aspx



Here is the template of the SQL statement you should use:

Collapse | Copy Code
SELECT t.fields FROM (
SELECT TOP x id_field, sort_field FROM (
SELECT TOP y id_field, sort_field
FROM table
WHERE conditions
ORDER BY sort_field ASC, id_field ASC) AS foo
ORDER BY sort_field DESC, id_field DESC) AS bar
INNER JOIN table AS t ON bar.id_field = t.id_field
ORDER BY bar.sort_field ASC, bar.id_field ASC
Legend

SQL Server keywords: the words in uppercase should not be changed:

t = Alias of table/view
fields = Name of the field(s) you want to display in the Web Control. They should include the primary key field(s) of the table/view (whether it's displayed or not).
x = Rows per page (number of rows you want to have returned).
id_field = Primary key field(s) of the table/view.
sort_field = Fields on which you want to sort the table (optional). Notice however that the sort fields always include the primary key field(s)! This is to avoid ambiguous results.
y = The maximum number of records required. This requires a little calculation: y = pagenumber * records_per_page. So if you want to display the third page, and there are 10 records per page, then y = 3 x 10 = 30. (However, the SQL statement will only return 10 records, but y has to be the maximum number of records required.)
table = Name of the table/view from which to get the recordset.
conditions = Usual WHERE-clauses go here.
foo = Just a name, you can use it or choose another one.
bar = Just a name, idem.
Example

This will give you the first 10 products from the table Products of which the UnitsInStock is less than 200, ordered by ProductName. Fields you want to display in the GridView are ProductID, ProductName, UnitPrice, and UnitsInStock:

Collapse | Copy Code
SELECT p.ProductID, p.ProductName, p.UnitPrice, p.UnitsInStock FROM
(SELECT TOP 10 ProductID, ProductName FROM
(SELECT TOP 10 ProductID, ProductName
FROM Products
WHERE UnitsInStock < 200
ORDER BY ProductName ASC, ProductID ASC) AS foo
ORDER BY ProductName DESC, ProductID DESC) as bar
INNER JOIN Products p ON bar.ProductID = p.ProductID
ORDER BY bar.ProductName ASC, bar.ProductID ASC
The following will give the second page. Note that it is just the 'y' value that's changed!

Collapse | Copy Code
SELECT p.ProductID, p.ProductName, p.UnitPrice, p.UnitsInStock FROM
(SELECT TOP 10 ProductID, ProductName FROM
(SELECT TOP 20 ProductID, ProductName
FROM Products
WHERE UnitsInStock < 200
ORDER BY ProductName ASC, ProductID ASC) AS foo
ORDER BY ProductName DESC, ProductID DESC) as bar
INNER JOIN Products p ON bar.ProductID = p.ProductID
ORDER BY bar.ProductName ASC, bar.ProductID ASC