數(shù)據(jù)整形 ============= 數(shù)據(jù)整形或分層的記錄集能顯示一個(gè)樹狀結(jié)構(gòu)或相關(guān)記錄。 應(yīng)用數(shù)據(jù)整形必須:
1。使用MSDataShape OLEDB提供者
Provider=MSDataShape;Data Provider=SQLOLEDB;DATA Source=..... 比較簡潔的方式如下: 連接字符串: strCon="Provider=SQLOLEDB;Data source=servername;"&_ "Initial Catalog=defaultsql;User Id=sa;Password=" 構(gòu)建MSDataShape OLEDB連接 strCon="Provider=MSDataShape;Data="&strCon
2。使用一種特殊的整形語言,它是SQL的一種擴(kuò)充,允許構(gòu)造層次。
(1)整形語言的語法: SHAPE{parent command} [AS parent alias] APPEND({child command} [AS child alias] RELATE parent_column TO child_column) [AS parent_column_name]
EXAMPLE: 以PUBS庫的Publishers和Titles為例
SHAPE{SELECT * FROM Publishers} APPEND({SELECT * FROM Titles} RELATE Pub_ID TO Pub_ID) AS rsTitles
在該例中,第一行是父記錄集,第二行是子記錄集,第三行指明關(guān)聯(lián)父、子記錄集 的兩個(gè)字段,兩個(gè)標(biāo)中都有一個(gè)名為Pub_ID的字段。該命令返回一個(gè)包含出版社的 記錄集,通過AS子句了一個(gè)含有子記錄集的新列(rsTitles)。
3。在ASP里使用數(shù)據(jù)整形
如何訪問上面例子中的Titles記錄集呢? Set rsTitles=rsPublishers("rsTitles").value 一個(gè)完整的遍歷記錄集的例子:
<% dim rsPublishers dim rsTitles dim strShapeConn dim strShape Dim strConn
strConn = "Provider=SQLOLEDB; Data Source=" & _ Request.ServerVariables("SERVER_NAME") & _ "; Initial Catalog=pubs; User ID=sa; Password=" set rsPublishers=server.CreatObject("ADODB.Recordset")
'創(chuàng)建連接字符串 strShapeConn = "Provider=MSDataShape; Data " & strConn
' 創(chuàng)建一個(gè)包含出版社的父記錄集和一個(gè)含有書名的子記錄集 strShape = "SHAPE {select * from publishers}" & _ " APPEND ({select * from titles}" & _ " RELATE pub_id TO pub_id) AS rsTitles"
' 打開記錄集 rsPublishers.Open strShape, strShapeConn
' 遍歷記錄集 Response.Write "<UL>" While Not rsPublishers.EOF Response.Write "<LI>" & rsPublishers("pub_name")
' now the titles Response.Write "<UL>" '設(shè)置變量rsTitles只想記錄的value值 Set rsTitles = rsPublishers("rsTitles").Value
' loop through the titles While Not rsTitles.EOF Response.Write "<LI>" & rsTitles("title") rsTitles.MoveNext Wend Response.Write "</UL>"
' move to the next publisher rsPublishers.MoveNext Wend Response.Write "</UL>"
rsPublishers.Close Set rsPublishers = Nothing Set rsTitles = Nothing %> -------------------------------------------------------------- china&boy整理 轉(zhuǎn)自:《ASP3高級(jí)編程》
|