数据整形
=============
数据整形或分层的记录集能显示一个树状结构或相关记录。
应用数据整形必须:
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高级编程》