<%
dim conn
dim strconn
dim rs
dim strsql
dim strsql2
dim strsql3
dim strsql4
dim strsql5
dim strsql6
dim strsql7
dim strsql8
'strconn = Driver={SQL
Server};Description=example;SERVER=222.222.1.2;UID=webexample;PWD=;DATABASE=webexample"
'Format Declare & EXEC statements that will be passed
'to the database with the output parameters
strsql = "DECLARE " & CHR(10) & "@Id_Req " & "INT" & CHR(10)
strsql2 ="exec " & "sp_EmpInfo" & " '" & request("txtFirstName") & "'," & "'" & request
("txtLastName") & "', " & "'" & request("txtaddress") & "', " & "'" & request("txtcity") &
"', "& "@Id_Req " & "OUTPUT" & chr(10)
'Formats one or more sql statements that will be passed to the
'database In this examples I use six different ways.
strsql3 ="SELECT * FROM AllData WHERE RecordId = @Id_Req" & Chr(10)
strsql4 ="SELECT AllData.fname, AllData.lname FROM Alldata WHERE RecordId = @Id_Req" & Chr
(10)
strsql5 ="SELECT AllData.fname FROM AllData WHERE RecordId = @Id_Req" & Chr(10)
strsql6 ="SELECT AllData.lname FROM AllData WHERE RecordId = @Id_Req" & Chr(10)
strsql7 ="SELECT AllData.Address FROM AllData WHERE RecordId = @Id_Req" & Chr(10)
strsql8 ="SELECT AllData.city FROM AllData WHERE RecordId = @Id_Req" & Chr(10)
'Puts together all of the local variables into one variable
'That will be used by the recordset object
strsql = strsql & strsql2 & strsql3 & strsql4 & strsql5 & strsql6 & strsql7 & strsql8
'This is optional this writes out the strsql local variable
'that will be passed to the database
response.write "<b>" & "Sql Statement that is passed to the database" & "</b>" & "<br>"
response.write strsql & "<br>" & "<br>"
'sets a connection & recordset objects and executes the strsql local variable
set conn = server.createobject("adodb.connection")
conn.open strconn
set rs = server.createobject("adodb.recordset")
rs.open strsql, conn
'Parses out the individual recordsets and places them
'into individual table rows
intcount = 1
Do Until rs Is Nothing
response.write "<table border='1' width='25%'>"
response.write "<b> Contents of recordset #" & intCount & "</b><br>"
'Parses out the individual recordsets and places them into table rows
Do While Not rs.EOF
response.write "<TR>"
For Each oField In RS.Fields
response.write "<TH>" & oField.Name & "</TH>"
Next
Response.write "</TR>" & "<TR>"
For Each oField In RS.Fields
response.write "<TD ALIGN=center>"
If IsNull(oField) Then
Response.Write " "
Else
Response.Write oField.Value
End If
response.write "</TD>"
Next
rs.MoveNext
Loop
'Uses the NEXTRECORDSET Method
Set rs = rs.NextRecordset
intCount = intCount + 1
response.write "</table>"
Loop
%>
NextRecordset 和 GetRows 大家可能用的很少!
最近使用使用,不错的好东东!
对提高批量查询,查询纪录集不是巨海量的情况很有效果
NextRecordset 和 GetRows 是Recordset的两个属性(属性还是方法我是常混淆是非#$#$,弄不清四下五
除一)
GetRows ---> 将recordset记录集提取到一个二维数组中,我们对recordset数据的行为就转移到该数组
,可以早早的断开纪录集,不用再使用元数据操作,rs.movnext, while not rs.eof等可以省掉
NextRecordset ----> 就是在一次提交多个查询,形成多个reordset结果集的情况下,提供一个离开当前
工作的recordset,转移到第二个recordset的方法!
主要是用在多个SELECT形成的结果集的情况
示例如下:
dim SQL,Rs,arrA,arrB,rowsA,rowsB
'======提取数据库库记录====
(adodb.connection 的连接部分省略,假定CONN.open CONNstr)
SQL=" select Ca1,Ca2,Ca3,Ca4 from TableA " '---------------SELECTa
SQL=SQL&" select Cb1,Cb2,Cb3,Cb4,Cb5 from TableB " '-------------SELECTb
Set Rs=conn.execute(SQL)
'执行结果将有两个select 的结果集,当前第一个select的recordset处于激活状态
arrA=rs.GetRows '----------取得SElECTa Recordset的二维数组
set rs=rs.NextRecordset
'------------最关键的一步,使用Nextrecordset激活下一个recordset
arrB=rs.GetRows '----------再次取得第二个SElECTb Recordset的二维数组
Rs.close
set rs=nothing '---------尽早释放数据库对象,关闭记录集
CONN.close
set CONN=Nothing
这样,我们所有关于数据库的数据干干净净的提取完成,用最早的时间释放数据库资源
'-----------//
'========用取得的arrA arrB进行页面处理,显示数据结果======
'注意,arrA=GetRows 后得到的数组,第一维是代表列,第二维代表行
rowsA=ubound(arrA,2) '----提取arrA的第二维下标,相当于取得recordset 的记录行数
rowsB=ubound(arrB,2) '-----同上,提取arrB的第二维下标
'做数据循环:
'第一个select表的循环
response.write "<table>"
For i=0 to rowsA
response.write "<tr>
response.write "<td>"&arrA(i,0)&"</td>" 'tableA.Ca1
response.write "<td>"&arrA(i,1)&"</td>" 'tableA.Ca2
response.write "<td>"&arrA(i,2)&"</td>" 'tableA.Ca3
response.write "<td>"&arrA(i,3)&"</td>" 'tableA.Ca4
response.write "</tr>"
Next
response.write "</table>
'第二个select表循环
response.write "<table>"
For i=0 to rowsB
response.write "<tr>
response.write "<td>"&arrB(i,0)&"</td>" 'tableB.Cb1
response.write "<td>"&arrB(i,1)&"</td>" 'tableB.Cb2
response.write "<td>"&arrB(i,2)&"</td>" 'tableB.Cb3
response.write "<td>"&arrB(i,3)&"</td>" 'tableB.Cb4
response.write "<td>"&arrB(i,4)&"</td>" 'tableB.Cb5
response.write "</tr>"
Next
response.write "</table>
'--------OVER
REM '============小结========
这样的结果,再清楚不过!
(1)使用Nextrecordset,可以处理多个select语句一次发送形成的结果集,减少网络流量,必定加快速
度!
不使用NextRecordset 则会这样操作:
SQL="select Ca1,Ca2,Ca3, Ca4 From TableA "
set Rs=CONN.execute (SQL)
SQL=" select Cb1,Cb2,Cb3,Cb4,Cb5 from TableB "
Set Rs=CONN.execute (SQL)
(2)使用GetRows将记录集提取到数组中(放到内存,所以要求记录集不要海大啦)
用内存的数组工作,而且省掉EOF,movenext等的判断,谁更快!自不必说!
(3)最最主要的,我们利用上二者,一次性将所有的数据提完,快速断开数据库连接和摧毁建立
recordset数据库对象,大大减少网络流量!性能自然要提高很多!
浙公网安备 33010602011771号