GetRows 方法
將 Recordset 對(duì)象的多個(gè)記錄復(fù)制到數(shù)組中。
語(yǔ)法
array = recordset.GetRows( Rows, Start, Fields )
返回值
返回二維數(shù)組。
參數(shù)
Rows 可選,長(zhǎng)整型表達(dá)式,指定要檢索記錄數(shù)。默認(rèn)值為 adGetRowsRest (-1)。
Start 可選,字符串或長(zhǎng)整型,計(jì)算得到在 GetRows 操作開(kāi)始處的記錄的書簽。也可使用下列 BookmarkEnum 值。
常量 說(shuō)明
AdBookmarkCurrent 從當(dāng)前記錄開(kāi)始。
AdBookmarkFirst 從首記錄開(kāi)始。
AdBookmarkLast 從尾記錄開(kāi)始。
Fields 可選,變體型,代表單個(gè)字段名、順序位置、字段名數(shù)組或順序位置號(hào)。ADO 僅返回這些字段中的數(shù)據(jù)。
說(shuō)明
使用 GetRows 方法可將記錄從 Recordset 復(fù)制到二維數(shù)組中。第一個(gè)下標(biāo)標(biāo)識(shí)字段,第二個(gè)則標(biāo)識(shí)記錄號(hào)。當(dāng) GetRows 方法返回?cái)?shù)據(jù)時(shí)數(shù)組變量將自動(dòng)調(diào)整到正確大小。
如果不指定 Rows 參數(shù)的值,GetRows 方法將自動(dòng)檢索 Recordset 對(duì)象中的所有記錄。如果請(qǐng)求的記錄比可用記錄多,則 GetRows 僅返回可用記錄數(shù)。
如果 Recordset 對(duì)象支持書簽,則可以通過(guò)傳送該記錄的 Bookmark 屬性值,來(lái)指定 GetRows 方法將從哪個(gè)記錄開(kāi)始檢索數(shù)據(jù)。
如要限制 GetRows 調(diào)用返回的字段,則可以在 Fields 參數(shù)中傳送單個(gè)字段名/編號(hào)或者字段名/編號(hào)數(shù)組。
在調(diào)用 GetRows 后,下一個(gè)未讀取的記錄成為當(dāng)前記錄,或者如果沒(méi)有更多的記錄,則 EOF 屬性設(shè)置為 True。
GetString方法
查詢數(shù)據(jù)庫(kù)顯示表格時(shí),我們常用Do While()...Loop 或者是For...Next循環(huán)來(lái)顯示表格,這樣當(dāng)我們要查詢大量數(shù)據(jù)時(shí),勢(shì)必會(huì)比較慢。這時(shí),我們就可以用記錄集對(duì)象提供的GetString()方法(ADO必須升級(jí)到2.0)。
語(yǔ)法
Str=objRecordset.GetString(format,n,coldel,rowdel,nullexpr)
參數(shù)說(shuō)明:
objRecordset:已打開(kāi)的記錄集對(duì)象;
format:可選,一般取默認(rèn)值(默認(rèn)值為2)
n:可選,顯示記錄的數(shù)量,默認(rèn)值為全部顯示
coldel:可選,列界定符
rowdel:可選,行界定符
nullexpr:可選,該參數(shù)用于填充空字段!
有了GetString方法,我們就可以僅用一個(gè)Response.Write來(lái)顯示所有的輸出了,它就象是能判斷Recordset是否為EOF的DO ... LOOP循環(huán)。
用這個(gè)方法,可以自動(dòng)的循環(huán)輸出字符串,就不用再去while或for循環(huán)了,只要建立了RS對(duì)象,并且執(zhí)行了相應(yīng)操作,不管那是返回一條或者多條記錄,甚至是空記錄,getstring照樣工作。
要從Recordset的結(jié)果里生成HTML表格,我們只需關(guān)心GetString的5個(gè)參數(shù)中的3個(gè):coldel(分隔記錄集的列的HTML代碼),rowdel(分隔記錄集的行的HTML代碼),和nullexpr(當(dāng)前記錄為空時(shí)應(yīng)生成的HTML代碼)。
TABLE Border=1>
TR>TD>
% = Response.Write rs.GetString( , , "/TD>TD>", "/TD>/TR>TR>", ) %>
/TABLE>
這樣寫的HTML結(jié)果如下:
TABLE Border=1>
TR>
TD>row1, field1 value/TD>
TD>row1, field2 value/TD>
/TR>
TR>
TD>row2, field1 value/TD>
TD>row2, field2 value/TD>
/TR>
/TABLE>
這里有個(gè)BUG了,再看看生成下拉選單:
%
Set RS = conn.Execute("Select theValue,theText FROM selectOptionsTable orDER BY theText")
optSuffix = "/OPTION>" vbNewLine
valPrefix = "OPTION Value='"
valSuffix = "'>"
opts = RS.GetString( , , valSuffix, optSuffix valPrefix, "--error--" )
' Next line is the key to it!
opts = Left( opts, Len(opts)-Len(valPrefix) )
Response.Write "Select ...>" vbNewLine
Response.Write valPrefix opts
Response.Write "/Select>"
%>
如果想建立一個(gè)正確的表格的話,解決那個(gè)BUG,只要這樣做就可以了:
%
Set RS = conn.Execute("Select * FROM table")
tdSuffix = "/TD>" vbNewLine "TD>
trPrefix = "TR>" vbNewLine "TD>"
trSuffix = "/TD>" vbNewLine "/TR>" vbNewLine "TR>" vbNewLine
opts = RS.GetString( , , tdSuffix, trSuffix trPrefix, "--error--" )
' Next line is the key to it!
opts = Left( opts, Len(opts)-Len(trPrefix) )
Response.Write "TABLE Border=1 CellPadding=5>" vbNewLine
Response.Write trPrefix opts
Response.Write "/TABLE>" vbNewLine
%>
再介紹一個(gè)完全不同的辦法:
%
SQL = "Select 'OPTION Value=''',value,'''>',text,'/OPTION>' FROM table orDER BY text"
Set RS = conn.Execute(SQL)
Response.Write "Select>" vbNewLine RS.GetString(,,"",vbNewLine) "/Select>"
%>
你用過(guò)嗎。。。
看到了嗎?可以直接從查詢中返回結(jié)果。
再進(jìn)一步,您可以這樣做:
%
SQL = "Select 'OPTION Value=''' value '''>' text '/OPTION>' FROM table orDER BY text"
Set RS = conn.Execute(SQL)
Response.Write "Select>" vbNewLine RS.GetString(,,"",vbNewLine) "/Select>"
%>
下面是一份完整的示例:
Script Output:
711855 Wednesday 23 3/23/2005 1:33:37 AM
711856 Wednesday 23 3/23/2005 1:23:00 AM
711857 Wednesday 23 3/23/2005 1:26:34 AM
711858 Wednesday 23 3/23/2005 1:33:53 AM
711859 Wednesday 23 3/23/2005 1:30:36 AM
ASP完整代碼如下:
%
' Selected constants from adovbs.inc:
Const adClipString = 2
' Declare our variables... always good practice!
Dim cnnGetString ' ADO connection
Dim rstGetString ' ADO recordset
Dim strDBPath ' Path to our Access DB (*.mdb) file
Dim strDBData ' String that we dump all the data into
Dim strDBDataTable ' String that we dump all the data into
' only this time we build a table
' MapPath to our mdb file's physical path.
strDBPath = Server.MapPath("db_scratch.mdb")
' Create a Connection using OLE DB
Set cnnGetString = Server.CreateObject("ADODB.Connection")
' This line is for the Access sample database:
'cnnGetString.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" strDBPath ";"
' We're actually using SQL Server so we use this line instead.
' Comment this line out and uncomment the Access one above to
' play with the script on your own server.
cnnGetString.Open "Provider=SQLOLEDB;Data Source=10.2.1.214;" _
"Initial Catalog=samples;User Id=samples;Password=password;" _
"Connect Timeout=15;Network Library=dbmssocn;"
' Execute a simple query using the connection object.
' Store the resulting recordset in our variable.
Set rstGetString = cnnGetString.Execute("Select * FROM scratch")
' Now this is where it gets interesting... Normally we'd do
' a loop of some sort until we ran into the last record in
' in the recordset. This time we're going to get all the data
' in one fell swoop and dump it into a string so we can
' disconnect from the DB as quickly as possible.
strDBData = rstGetString.GetString()
' Since I'm doing this twice for illustration... I reposition
' at the beginning of the RS before the second call.
rstGetString.MoveFirst
' This time I ask for everything back in HTML table format:
strDBDataTable = rstGetString.GetString(adClipString, -1, _
"/td>td>", "/td>/tr>" vbCrLf "tr>td>", " ")
' Because of my insatiable desire for neat HTML, I actually
' truncate the string next. You see, GetString only has
' a parameter for what goes between rows and not a seperate
' one for what to place after the last row. Because of the
' way HTML tables are built, this leaves us with an extra
' tr>td> after the last record. GetString places the
' whole delimiter at the end since it doesn't have anything
' else to place there and in many situations this works fine.
' With HTML it's a little bit weird. Most developers simply
' close the row and move on, but I couldn't bring myself to'
leave the extra row... especially since it would have a
' different number of cells then all the others.
' What can I say... these things tend to bother me. ;)
strDBDataTable = Left(strDBDataTable, Len(strDBDataTable) - Len("tr>td>"))
' Some notes about .GetString:
' The Method actually takes up to 5 optional arguments:
' 1. StringFormat - The format in which to return the
' recordset text. adClipString is the only
' valid value.
' 2. NumRows - The number of rows to return. Defaults
' to -1 indicating all rows.
' 3. ColumnDelimiter - The text to place in between the columns.
' Defaults to a tab character
' 4. RowDelimiter - The text to place in between the rows
' Defaults to a carriage return
' 5. NullExpr - Expression to use if a NULL value is
' returned. Defaults to an empty string.
' Close our recordset and connection and dispose of the objects.
' Notice that I'm able to do this before we even worry about
' displaying any of the data!
rstGetString.Close
Set rstGetString = Nothing
cnnGetString.Close
Set cnnGetString = Nothing
' Display the table of the data. I really don't need to do
' any formatting since the GetString call did most everything
' for us in terms of building the table text.
Response.Write "table border=""1"">" vbCrLf
Response.Write "tr>td>"
Response.Write strDBDataTable
Response.Write "/table>" vbCrLf
' FYI: Here's the output format you get if you cann GetString
' without any parameters:
Response.Write vbCrLf "p>Here's the unformatted version:/p>" vbCrLf
Response.Write "pre>" vbCrLf
Response.Write strDBDataResponse.Write "/pre>" vbCrLf
' That's all folks!
%>
下面給大家補(bǔ)充一下
ASP里使用GetString來(lái)循環(huán)讀取數(shù)據(jù)的具體方法及實(shí)例
在ASP里,我們要循環(huán)讀取數(shù)據(jù)時(shí),我們通常用的是 while ... wend 或者 do while() .. loop,但這種方法非常的沒(méi)有效率,并且會(huì)影響網(wǎng)頁(yè)程序打開(kāi)的速度。
當(dāng)我們只是從數(shù)據(jù)庫(kù)里讀取并顯示數(shù)據(jù)的時(shí)候,我們可以用GetString來(lái)加速讀取數(shù)據(jù),并顯示到網(wǎng)頁(yè)上,使用GetString會(huì)大大增加我們從數(shù)據(jù)庫(kù)讀取數(shù)據(jù)的效率。
本文站長(zhǎng)就和大家來(lái)說(shuō)說(shuō)ASP里使用GetString的方法,先來(lái)看GetString的具體語(yǔ)法結(jié)構(gòu):
Str=objRecordset.GetString(format,n,coldel,rowdel,nullexpr)
'參數(shù):
objRecordset :為我們打開(kāi)的rs記錄集
format:可選,一般取默認(rèn)值(默認(rèn)值為2)
n:可選,顯示記錄的數(shù)量,默認(rèn)值為全部顯示
coldel:可選,列界定符
rowdel:可選,行界定符
nullexpr:可選,該參數(shù)用于填充空字段!
下面站長(zhǎng)給大家提供一個(gè)完整的實(shí)例:
%
set conn = server.CreateObject("adodb.connection")
conn.open "Driver={Microsoft Access Driver (*.mdb)};DBQ=" server.MapPath("study.mdb")
set rs = server.createobject("adodb.recordset")
sql = "sel ect * from users"
rs.open sql,conn,1,1
str=rs.GetString(,,"/td>td>","/td>/tr>tr>td>","該字段為空!")
Response.Write("table border=1>tr>td>"str"/td>/tr>/table>")
%>
以上就是asp中記錄集對(duì)象的getrows和getstring用法分析的詳細(xì)內(nèi)容,更多關(guān)于記錄集 getrows getstring的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
您可能感興趣的文章:- 用GetString提高ASP的速度
- asp ADO GetString函數(shù)與用GetString來(lái)提高ASP的速度
- asp中在JScript中使用RecordSet對(duì)象的GetRows
- 在asp中通過(guò)getrows實(shí)現(xiàn)數(shù)據(jù)庫(kù)記錄分頁(yè)的一段代碼
- asp中通過(guò)getrows實(shí)現(xiàn)數(shù)據(jù)庫(kù)記錄分頁(yè)的一段代碼