rs.getstring的方法GetString的作用是:以字符串的形式返回指定的記錄集??梢允褂眠@個方法向ASP文件中添加HTML表格。
getstring 方法語法
Set str=objRecordset.GetString(format,n,coldel,rowdel,nullexpr)
Parameter參數 |
Description描述 |
---|---|
format |
Optional. A StringFormatEnum value that specifies the format when retrieving a Recordset as a string |
n |
Optional. The number of rows to be converted in the Recordset |
coldel |
Optional. If format is set to adClipString it is a column delimiter. Otherwise it is the tab character |
rowdel |
Optional. If format is set to adClipString it is a row delimiter. Otherwise it is the carriage return character |
nullexpr |
Optional. If format is set to adClipString it is an expression used instead of a null value. Otherwise it is an empty string |
案例
To create an HTML table with data from a recordset, we only need to use three of the parameters above:
我們只要通過上述三個參數中的一個就可以創建HTML格式的記錄集數據表:
coldel - the HTML to use as a column-separator
coldel – 使用HTML格式作為列分隔符
rowdel - the HTML to use as a row-separator
rowdel – 使用HTML格式行分隔符
NullExpr - the HTML to use if a column is NULL
NullExpr – 如果列為空,則使用HTML
Note: The GetString() method is an ADO 2.0 feature.
在下面的案例中,我們將使用GetString()方法將記錄集以一個字符串的形式保留:
復制代碼代碼如下:
<html>
<body><%
set conn=Server.CreateObject("ADODB.Connection")
conn.Provider="Microsoft.Jet.OLEDB.4.0"
conn.Open "c:/webdata/northwind.mdb"set rs = Server.CreateObject("ADODB.recordset")
rs.Open "SELECT Companyname, Contactname FROM Customers", connstr=rs.GetString(,,"</td><td>","</td></tr><tr><td>"," ")
%><table border="1" width="100%">
<tr>
<td><%Response.Write(str)%></td>
</tr>
</table><%
rs.close
conn.close
set rs = Nothingset conn = Nothing%></body>
</html>
Constant |
Value |
Description |
---|---|---|
adClipString |
2 |
Delimits rows by the rowdel parameter, columns by the coldel parameter, and null values by the nullexpr parameter |
許多asp程序員都有過執行數據庫查詢,然后將查詢結果用html表格的形式顯示出來的經歷吧. 通常我們是這么做的:
<%
"create connection / recordset
"populate data into recordset object
%>
<table>
<% do while not rs.eof %>
<tr>
<td><%=rs("field1")%></td>
<td><%=rs("field2")%></td>
...
</tr>
<% rs.movenext
loop %>
</table>
如果查詢結果很多,服務器解釋你的asp script將花費大量的時間,因為有許多的response.write語句要處理. 如果你將輸出的全部結果放在一個很長的字符串里(從<table>到</table>),那么服務器只需解釋一遍response.write語句,速度就會快得多. 微軟公司里的一些能干的家伙已經將想法變成了現實. (注意,這是一個ado 2.0才有的特性. 如果你還在使用ado 1.5話,可以在http://www.microsoft.com/data/download.htm免費下載ado 2.0)
有了getstring方法,我們就可以僅用一個response.write來顯示所有的輸出了,它就象是能判斷recordset是否為eof的do ... loop循環.
getstring的用法如下(所有的參數都是可選的):
string = recordset.getstring(stringformat, numrows, columndelimiter, rowdelimiter, nullexpr)
GetString rs.getstring getstring 方法要從recordset的結果里生成html表格,我們只需關心getstring的5個參數中的3個: columndelimiter(分隔記錄集的列的html代碼),rowdelimiter(分隔記錄集的行的html代碼),和nullexpr(當前記錄為空時應生成的html代碼). 就象你在下面生成html表格的例子里所看到的那樣,每列用<td>...</td>分隔,每行用<tr>...</tr>分隔. 來看看例子的代碼吧.
<%@ language="vbscript" %>
<% option explicit "good coding technique
"establish connection to db
dim conn
set conn = server.createobject("adodb.connection")
conn.open "dsn=northwind;"
"create a recordset
dim rs
set rs = server.createobject("adodb.recordset")
rs.open "select * from table1", conn
"store our one big string
dim strtable
strtable = rs.getstring(,,"</td><td>","</td></tr><tr><td>"," ") %>
<html>
<body>
<table>
<tr><td>
<% response.write(strtable) %>
</tr></td>
</table>
</body>
</html>
<%
"cleanup!
rs.close
set rs = nothing
conn.close
set conn = nothing
%>
strtable字符串用于存放我們從"select * from table1"結果生成的html表格的代碼. html表格的每列之間都將有</td><td>的html代碼,每行之間的html代碼是</td></td><tr><td>. getstring方法將輸出正確的html代碼并存放在strtable中,這樣我們只需一行response.write便可以輸出數據集中的所有記錄. 讓我們來看個簡單的例子,假設我們的查詢結果返回了以下的行和列:
col1 col2 col3
row1 bob smith 40
row1 ed frank 43
row1 sue void 42
那么getstring語句返回的字符串將是:
bob</td><td>smith</td><td>40</td><td></td></tr><tr><td>ed ...
說實話,這個字符串看上去冗長而雜亂,但它就是我們想要的html代碼. (注意看,我們在手工書寫的html代碼中,將<table><tr><td>放在response.write的前面,將</td></tr></table>放在它的后面. 這是因為我們的格式化字符串中并不含有這些表格頭尾所需的字符串.)
charles carroll的文章:http://www.learnasp.com/learn/dbgetstring.asp講述了如何用getstring來生成一個select box. 我想對你們也是很有幫助的。