using System;
using System.Collections.Specialized;
namespace web
{
/// <summary>
/// MultiOrderPagerSQL 的摘要說(shuō)明
/// </summary>
public class MultiOrderPagerSQL
{
private NameValueCollection orders = new NameValueCollection();
private string table_;
private string where_="";//1=1 and 2=2 的格式
private string outfields_;
private int nowPageIndex_=0;
private int pagesize_=0;
private string sql_;//要返回的SQL
public MultiOrderPagerSQL()
{
}
/****************方法*******************/
public void addOrderField(string field, string direction)
{
orders.Add(field, direction);
}
public string getSQL()
{
//排序字段
string orderList="";//用戶(hù)期望的排序
string orderList2 = "";//對(duì)用戶(hù)期望的排序的反排序
string orderList3 = "";//用戶(hù)期望的排序,去掉了前綴.復(fù)合查詢(xún)里的外層的排序不能是類(lèi)似這樣的table1.id,要去掉table1.。
if (orders.Count > 0)
{
string[] str = orders.AllKeys;
foreach (string s in str)
{
string direction="asc";//默認(rèn)一個(gè)方向
if (orders[s].ToString() == "asc")
direction = "desc";
//去掉前綴的字段名稱(chēng)
string s2 = "";
int index = s.IndexOf(".") + 1;
s2 = s.Substring(index);
orderList =orderList + s +" "+ orders[s] +",";
orderList2 = orderList2 + s2 + " " + direction + ",";
orderList3 = orderList3 + s2 + " " + orders[s] + ",";
}
//去掉最后的,號(hào)
orderList = orderList.Substring(0,orderList.Length-1);
orderList2 = orderList2.Substring(0, orderList2.Length - 1);
orderList3 = orderList3.Substring(0, orderList3.Length - 1);
}
//return orderList2;
//形成SQL
string strTemp;
strTemp = "select * from \n ( select top {7} * from ( select top {6} {0} from {1} \n";
if (where_ != "")
strTemp = strTemp + " where {2} \n";
if(orderList!="")
strTemp = strTemp + " order by {3} ) as tmp order by {4} \n ) \n as tmp2 \n order by {5} \n";
strTemp = string.Format(strTemp, outfields_, table_, where_, orderList, orderList2, orderList3, nowPageIndex_ * pagesize_, pagesize_);
return strTemp;
}
/****************屬性*******************/
public string table
{
set { table_ = value; }
}
public string where
{
set { where_ = value; }
}
public string outfields
{
set { outfields_ = value; }
}
public int nowPageIndex
{
set { nowPageIndex_ = value; }
}
public int pagesize
{
set { pagesize_ = value; }
}
}
}
說(shuō)一下原理先:其實(shí)很簡(jiǎn)單,由于AC和MS SQL 2000 沒(méi)有象MS SQL 2005的row_number函數(shù),我們就不能從這里下手了,比如你取第二頁(yè),那就是序號(hào)從10-20,我們先按照某一排序規(guī)則 把 前 20條的數(shù)據(jù)取出來(lái),然后再按照先前的排序規(guī)則的反規(guī)則把這個(gè)數(shù)據(jù)反排序,再取前10條,那么這個(gè)時(shí)候就是要取的數(shù)據(jù)了,這個(gè)時(shí)候還沒(méi)有結(jié)束,再把結(jié)果按照先前的排序規(guī)則排序即可。我覺(jué)得效率瓶頸會(huì)出現(xiàn)在排序上。看看是怎么來(lái)使用的:
代碼如下:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class MultiOrderPagerSQLTest : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
web.MultiOrderPagerSQL sql = new web.MultiOrderPagerSQL();
//sql.addOrderField("t1.id", "desc");//第一排序字段
sql.addOrderField("t1.hits", "desc");//第二排序字段
sql.table = "joke t1,type t2";
sql.outfields = "t1.*,t2.type";
sql.nowPageIndex = 5;
sql.pagesize = 10;
sql.where = "t1.typeid=t2.typeid";
Response.Write(sql.getSQL());
}
}
以上在AC和MS SQL 2000(5)上測(cè)試通過(guò)。
暫時(shí)做出這樣一個(gè)類(lèi),沒(méi)有做成存儲(chǔ)過(guò)程,要做的話(huà),還有一點(diǎn)難度呢 ,呵呵。
聲明:本網(wǎng)頁(yè)內(nèi)容旨在傳播知識(shí),若有侵權(quán)等問(wèn)題請(qǐng)及時(shí)與本網(wǎng)聯(lián)系,我們將在第一時(shí)間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com