數據整形 ============= 數據整形或分層的記錄集能顯示一個樹狀結構或相關記錄。 應用數據整形必須:
1。使用MSDataShape OLEDB提供者
Provider=MSDataShape;Data Provider=SQLOLEDB;DATA Source=..... 比較簡潔的方式如下: 連接字符串: strCon="Provider=SQLOLEDB;Data source=servername;"&_ "Initial Catalog=defaultsql;User Id=sa;Password=" 構建MSDataShape OLEDB連接 strCon="Provider=MSDataShape;Data="&strCon
2。使用一種特殊的整形語言,它是SQL的一種擴充,允許構造層次。
(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
在該例中,第一行是父記錄集,第二行是子記錄集,第三行指明關聯父、子記錄集 的兩個字段,兩個標中都有一個名為Pub_ID的字段。該命令返回一個包含出版社的 記錄集,通過AS子句了一個含有子記錄集的新列(rsTitles)。
3。在ASP里使用數據整形
如何訪問上面例子中的Titles記錄集呢? Set rsTitles=rsPublishers("rsTitles").value 一個完整的遍歷記錄集的例子:
<% 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")
'創建連接字符串 strShapeConn = "Provider=MSDataShape; Data " & strConn
' 創建一個包含出版社的父記錄集和一個含有書名的子記錄集 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>" '設置變量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整理 轉自:《ASP3高級編程》
|