asp.net with MySQL stored procedure for paging code

Source: Internet
Author: User
Tags prepare

Recently used mysql+asp.net to write the site, since MySQL has supported the stored procedures, so like paging so common things, of course, to use the stored procedures

But find some on the internet, found that there is a feature-that is, the total number of records can not be outgoing, simply study it yourself. At last, the efficiency may not be very good, but I feel good too. Post code directly: It's also a record of learning MySQL for yourself.

CREATE PROCEDURE P_pagelist
(
M_pageno int,
m_perpagecnt int,
M_column varchar (1000),
M_table varchar (1000),
M_condition varchar (1000),
M_orderby varchar (200),
Out m_totalpagecnt int
)
BEGIN
SET @pageCnt = 1; --Total number of records
SET @limitStart = (m_pageno-1) *m_perpagecnt;
SET @limitEnd = m_perpagecnt;
SET @sqlCnt = CONCAT (' Select COUNT (1) into @pageCnt from ', m_table); --This statement is critical to get the total value
SET @sql = CONCAT (' Select ', M_column, ' from ', m_table);
IF M_condition is not NULL and m_condition <> ' THEN
SET @sql = CONCAT (@sql, ' where ', m_condition);
SET @sqlCnt = CONCAT (@sqlCnt, ' where ', m_condition);
End IF;
IF M_orderby is not NULL and M_orderby <> ' THEN
SET @sql = CONCAT (@sql, ' ORDER by ', M_orderby);
End IF;
SET @sql = CONCAT (@sql, ' limit ', @limitStart, ', ', @limitEnd);
PREPARE s_cnt from @sqlCnt;
EXECUTE s_cnt;
Deallocate PREPARE s_cnt;
SET m_totalpagecnt = @pageCnt;
PREPARE record from @sql;
EXECUTE record;
Deallocate PREPARE Record;
End

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.