สร้างขั้นตอน GoalerPageSp
@IntPageSize อินท์
@IntCurrPage อินท์
@strFields nvarchar (2000)
@strTable วาชาร์ (200)
@strWhere varchar(800)
@strOrderType วาชาร์ (200)
@strKeyField วาชาร์ (50)
เช่น
ตั้งหมายเลขเป็น
ประกาศ @tmpSQL nvarchar(4000)--คำสั่ง SQL แบบไดนามิกสำหรับการจัดเก็บ
ประกาศ @tmpWhere varchar (800)
DECLARE @tmpAndWhere varchar(800)--ใช้สำหรับเงื่อนไขการสืบค้นในหน้า N (>1)
ประกาศ @tmpOrder varchar (200)
ประกาศ @tmpD_X varchar (2)
ประกาศ @tmpMin_MAX varchar (3)
--กำหนดเงื่อนไข--
ถ้า @strWhere เป็นโมฆะหรือ RTRIM(@strWhere)=''
เริ่มต้น--ไม่มีเงื่อนไขในการสืบค้น
SET @tmpWhere=''
SET @tmpAndWhere=''
จบ
อื่น
เริ่มต้น--มีเงื่อนไขการค้นหา
SET @tmpWhere=' WHERE '+@strWhere
ตั้งค่า @tmpAndWhere=' และ '+@strWhere
จบ
--ตั้งค่าการเรียงลำดับ--
ถ้า @strOrderType != 0
เริ่มต้น--ลำดับย้อนกลับ
SET @tmpD_X = '<'
SET @tmpMin_MAX = 'ขั้นต่ำ'
SET @tmpOrder=' เรียงตาม ' +@strKeyField+ ' DESC'
จบ
อื่น
เริ่ม
SET @tmpD_X = '>'
SET @tmpMin_MAX = 'สูงสุด'
SET @tmpOrder=' เรียงตาม ' +@strKeyField+ ' ASC'
จบ
--แบบสอบถาม SQL--
ถ้า @IntCurrPage=1
ตั้งค่า @tmpSQL='SELECT TOP '+CAST(@IntPageSize AS VARCHAR)+' '+@strFields+' จาก '+@strTable+' '+@tmpWhere+' '+@tmpOrder
อื่น
SET @tmpSQL='เลือกด้านบน '+CAST(@IntPageSize AS VARCHAR)+' '+@strFields+' จาก '+@strTable+' WHERE ('+@strKeyField+' '+@tmpD_X+' (SELECT '+@tmpMin_MAX+'(' +@strKeyField+') จาก (เลือกด้านบน '+นักแสดง(@IntPageSize*(@IntCurrPage-1) AS VARCHAR)+' '+@strKeyField+' จาก '+@strTable+' '+@tmpWhere+' '+@tmpOrder+') AS T))'+@tmpAndWhere+' ' +@tmpOrder
EXEC(@tmpSQL)
ไป
วิธีการโทร:
IntPageSize=20
strTable= [TableName] 'ชื่อตารางข้อมูล
strFields= Field1,Field2,Field3,Field4 'ชื่อคอลัมน์ที่จะอ่าน
strKeyField=Field1'primary key: ในที่นี้จะถือว่า Field1 เป็นคีย์หลัก
strWhere='เงื่อนไข: FieldA='b'
strOrderType=1'วิธีการเรียงลำดับ: 1 คือลำดับย้อนกลับ, 0 คือลำดับตามลำดับ
CurrPage=Request.QueryString(หน้า)
IF(CurrPage<> และ Isnumeric(CurrPage))แล้ว
CurrPage=CLNG(CurrPage)
ถ้า(CurrPage<1)แล้ว CurrPage=1
อื่น
CurrPage=1
สิ้นสุด ถ้า
ถ้า strWhere<> แล้ว
tmpWhere= WHERE &strWhere
อื่น
tmpที่ไหน=
สิ้นสุด ถ้า
ถ้า(SESSION(RecCount)<>)แล้ว
ถ้า(SESSION(strWhere)<>strWhere)แล้ว
RecCount=Conn.Execute(SELECT COUNT(&strKeyField&) จาก &strTable&tmpWhere)(0)
SESSION(RecCount)=จำนวนครั้ง
SESSION(strWhere)=strWhere
อื่น
RecCount=เซสชัน(RecCount)
สิ้นสุด ถ้า
อื่น
RecCount=Conn.Execute(เลือก COUNT(*) จาก &strTable&tmpWhere)(0)
SESSION(RecCount)=จำนวนครั้ง
SESSION(strWhere)=strWhere
สิ้นสุด ถ้า
ถ้า(RecCount MOD IntPageSize <>0)แล้ว
IntPageCount=INT(RecCount/IntPageSize)+1
อื่น
IntPageCount=RecCount/IntPageSize
สิ้นสุด ถ้า
SET Cmd=Server.CreateObject(Adodb.Command)
Cmd.CommandType=4
SET Cmd.ActiveConnection=เชื่อมต่อ
Cmd.CommandText=GoalerPageSp
Cmd.Parameters.Append Cmd.CreateParameter(@IntPageSize,4,1,4,IntPageSize)
Cmd.Parameters.Append Cmd.CreateParameter(@IntCurrPage,4,1,4,CurrPage)
Cmd.Parameters.Append Cmd.CreateParameter(@strFields,200,1,2000,strFields)
Cmd.Parameters.Append Cmd.CreateParameter(@strTable,200,1,200,strTable)
Cmd.Parameters.Append Cmd.CreateParameter(@strWhere,200,1,800,strWhere)
Cmd.Parameters.Append Cmd.CreateParameter(@strOrderType,4,1,4,strOrderType)
Cmd.Parameters.Append Cmd.CreateParameter(@strKeyField,200,1,50,strKeyField)
SET RS=Cmd.ดำเนินการ()
ถ้า RecCount<1 แล้ว
ตอบกลับเขียน(ไม่มีบันทึก)
อื่น
GetRecord=RS.GetRows(IntPageSize)
สำหรับ i=0 ถึง Ubound(GetRecord,2)
Response.Write(GetRecord(0,i),GetRecord(1,i),GetRecord(2,i))'...เนื้อหาเอาต์พุต
ต่อไป
GetRecord=ค่าว่าง
สิ้นสุด ถ้า
ตั้งค่า RS=ไม่มีอะไร
เพื่อนๆ ที่เป็นประโยชน์ โปรดทำการดีบั๊กด้วยตนเอง บันทึกทั้งหมดจะถูกดึงออกมาโดยใช้ ASP และจัดเก็บไว้ใน SESSION หากคุณนับบันทึกทั้งหมดทุกครั้ง จะใช้เวลานานมาก ขั้นตอนการจัดเก็บ นอกจากนี้ยังสามารถบันทึกและส่งกลับจำนวนหน้าทั้งหมดได้ ต่อไปนี้คือรหัส:
--รับจำนวนบันทึกทั้งหมด--
SET @tmpSQL='SELECT @getRecordCounts=COUNT('+@strKeyField+') จาก '+@strTable+@tmpWhere
EXEC sp_executesql @tmpSQL,N'@getRecordCounts เอาต์พุต int',@getRecordCounts เอาต์พุต
--รับจำนวนหน้าทั้งหมด--
SET @tempFolatNumber=@getRecordCounts%@IntPageSize
หาก @getRecordCounts<=@IntPageSize
SET @getPageCounts=1
อื่น
เริ่ม
ถ้า @tempFolatNumber != 0
SET @getPageCounts=(@getRecordCounts/@IntPageSize)+1
อื่น
SET @getPageCounts=(@getRecordCounts/@IntPageSize)
จบ
อย่าลืมส่งคืนพารามิเตอร์คำจำกัดความ:
@getRecordCounts int ผลลัพธ์ -- ส่งคืนบันทึกทั้งหมด
@getPageCounts int output ส่งคืนจำนวนหน้าทั้งหมด