How to use the nextrecordset to write out multiple recordsets in one call to the database. Very Hand

Contributed by
Rating: 4 stars4 stars4 stars4 stars4 stars / 7
September 01, 1999
Rate this Article:
MEH MEH++


SEARCH ASP FREE
TOOLS YOU CAN USE

advertisement

Using the NEXTRECORDSET method to write out multiple SELECT statements from one connection.


A formatted string gets passed to the database via a Recordset object.
     (below is this examples formatted string.)

Sql Statement that is passed to the database

DECLARE @Id_Req INT exec sp_EmpInfo 'John ','Smith', '123 main Street', 'Hollywood', @Id_Req OUTPUT SELECT * FROM AllData WHERE RecordId = @Id_Req SELECT AllData.fname, AllData.lname FROM Alldata WHERE RecordId = @Id_Req SELECT AllData.fname FROM AllData WHERE RecordId = @Id_Req SELECT AllData.lname FROM AllData WHERE RecordId = @Id_Req SELECT AllData.Address FROM AllData WHERE RecordId = @Id_Req SELECT AllData.city FROM AllData WHERE RecordId = @Id_Req

Output of the multiple recordsets formatted on an ASP Page. 

Contents of recordset #1
RecordIDfnamelnameaddresscity
58JohnSmith123 main StreetHollywood
Contents of recordset #2
fnamelname
JohnSmith
Contents of recordset #3
fname
John
Contents of recordset #4
lname
Smith
Contents of recordset #5
Address
123 main Street
Contents of recordset #6
city
Hollywood

4. Code of the asp page.

<%
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 "&nbsp;"
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
%>

 

blog comments powered by Disqus
ASP CODE ARTICLES

- ASP Forms
- ASP: The Beginning
- Getting Remote Files With ASP Continued
- Inbox and Outbox Manipulation in ASP
- Relational DropDownList Using VB.NET
- Ad Tracking URL Hits
- Use ViewState to display one record per page...
- Send Email using ASP.NET formatted in HTML
- ASP File Explorer
- ASP/XML Interview questions by Srivatsan Sri...
- Pressing RETURN won't submit the form
- This shows how you get the TEXT of a combo r...
- Group Data by Adrian Forbes
- Multiple checkbox select sample
- Multiple checkbox select with all values sam...

ASP Web Hosting ASP.Net Web Hosting Windows Web Hosting
 
 
 

ASP Free Forums 
 RSS  Tutorials RSS
 RSS  Forums RSS
 RSS  All Feeds
Site Map 
Request Media Kit
Write For Us Get Paid 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Privacy Policy 
Support 


© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 4 - Follow our Sitemap
Most Popular Topics
All ASP.Net Tutorials