SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Introduction to ADO Part 2 - Recordsets


Introduction to ADO Part 2 - Recordsets

Author
Message
Andy Warren
Andy Warren
SSC-Forever
SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)

Group: Moderators
Points: 40390 Visits: 2770
Comments posted to this topic are about the content posted at http://www.sqlservercentral.com/columnists/awarren/introductiontoadopart2recordsets.asp>http://www.sqlservercentral.com/columnists/awarren/introductiontoadopart2recordsets.asp

Andy
SQLAndy - My Blog!
Connect with me on LinkedIn
Follow me on Twitter
VR
VR
SSC Rookie
SSC Rookie (48 reputation)SSC Rookie (48 reputation)SSC Rookie (48 reputation)SSC Rookie (48 reputation)SSC Rookie (48 reputation)SSC Rookie (48 reputation)SSC Rookie (48 reputation)SSC Rookie (48 reputation)

Group: General Forum Members
Points: 48 Visits: 1
Good shot at basics of ADO.

Vidya



Andy Warren
Andy Warren
SSC-Forever
SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)

Group: Moderators
Points: 40390 Visits: 2770
Thanks! We'll probably do another one or two articles on it, but really there are plenty of books on the subject - just wanted to get people interested, give them a place to start.


Andy

Andy
SQLAndy - My Blog!
Connect with me on LinkedIn
Follow me on Twitter
roberthopkins
roberthopkins
SSC Journeyman
SSC Journeyman (92 reputation)SSC Journeyman (92 reputation)SSC Journeyman (92 reputation)SSC Journeyman (92 reputation)SSC Journeyman (92 reputation)SSC Journeyman (92 reputation)SSC Journeyman (92 reputation)SSC Journeyman (92 reputation)

Group: General Forum Members
Points: 92 Visits: 1
Very well written. Makes what for me is a complicated topic surprisingly understandable. I will definitely check out other articles by this author. Thank you.



tombynum
tombynum
SSC-Enthusiastic
SSC-Enthusiastic (187 reputation)SSC-Enthusiastic (187 reputation)SSC-Enthusiastic (187 reputation)SSC-Enthusiastic (187 reputation)SSC-Enthusiastic (187 reputation)SSC-Enthusiastic (187 reputation)SSC-Enthusiastic (187 reputation)SSC-Enthusiastic (187 reputation)

Group: General Forum Members
Points: 187 Visits: 8
Great article. I've only been exposed to ADO since April of this year. It's a little difficult to get out of the DAO mindset, but once you break the dependence, it's wonderful.

You mentioned using "where 1=0" in a select statement when you just want to add records.
Can you elaborate?

Edited by - tombynum on 11/28/2003 07:48:51 AM



The ~BEST~ solution is always the simplest one!
Andy Warren
Andy Warren
SSC-Forever
SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)

Group: Moderators
Points: 40390 Visits: 2770
Sure. A lot of times you'll have a scenario where you just want to add records - say you're a user registering for a new sql web site! If you want to use ADO recordsets, you need a way to get the table schema into the recordset so you do the add. One way is to manually load the fields collection, perhaps by querying sysobjects/syscolumns, or just by hard coding the fields you need. A shortcut is to retrieve a recordset that has 0 matching records, that way you get the schema but no data, then you can add from there. Which technique you use depends on your goal. If you want to save round trips, you should embed the fields and add manually. If you want to keep it simple, use the zero record record set idea (and the way you get that is by putting in criteria that will never match 'where 1-0'). I never use the sysobjects method (better would be infoschema view), doesnt have any advantages really.

Andy
http://www.sqlservercentral.com/columnists/awarren/

Andy
SQLAndy - My Blog!
Connect with me on LinkedIn
Follow me on Twitter
Kevin MacCallum
Kevin MacCallum
SSC-Addicted
SSC-Addicted (415 reputation)SSC-Addicted (415 reputation)SSC-Addicted (415 reputation)SSC-Addicted (415 reputation)SSC-Addicted (415 reputation)SSC-Addicted (415 reputation)SSC-Addicted (415 reputation)SSC-Addicted (415 reputation)

Group: General Forum Members
Points: 415 Visits: 1887
Great topic Andy.
Thanks

There is another way to reference data in recordset fields that is potentially faster than using the ordinal field position.
Create explicit ADODB field objects & use these in your code.
Doing this eliminate the additional processing step required to resolve the field reference each time it is accessed.
In small loops the time saved will be minimal but in a large loop it could be quite a bit.

Example:
Dim fldCompany As ADODB.Field, fldContact As ADODB.Field
Set fldCompany = rs.Fields("CompanyName")
Set fldContact = rs.Fields("ContactName")
'or
'Set fldCompany = rs.Fields(0)
'Set fldContact = rs.Fields(1)

Do Until rs.EOF
List1.AddItem fldCompany.Value & "/" & fldContact.Value
rs.MoveNext
Loop

Set fldCompany = Nothing
Set fldContact = Nothing

Kevin MacCallum



Andy Warren
Andy Warren
SSC-Forever
SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)

Group: Moderators
Points: 40390 Visits: 2770
I've seen that. Not my favorite technique, but as you say, sometimes you need every bit of performance. Bill Vaughn's books cover this and quite a bit more, if you're a heavy ADO user worth buying.

Andy
http://www.sqlservercentral.com/columnists/awarren/

Andy
SQLAndy - My Blog!
Connect with me on LinkedIn
Follow me on Twitter
rschaeferhig
rschaeferhig
Hall of Fame
Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)

Group: General Forum Members
Points: 3259 Visits: 119

While a good tutorial on using native SQL with ADO, I would not recommend this technique for web site development. Rather most, if not all, SQL calls should be to SQL Stored Procedures. This removes the issue of cursor type and location from the equation, placing the onus for SQL performance on the database server, where it belongs. Using Stored Procedures also "parameterizes" the data passed to SQL clauses preventing SQL Injection attacks.

Better performance, more modular coding and better site/application security. If you're not coding in ASP.NET 2.0 (which provides a better SQL Provider than any other version of ASP) then native SQL should rarely be used in an ASP site (IMHO).





I reject your reality and substitute one of my own. - Adam Savage-Mythbuster
sunjiulu
sunjiulu
Mr or Mrs. 500
Mr or Mrs. 500 (532 reputation)Mr or Mrs. 500 (532 reputation)Mr or Mrs. 500 (532 reputation)Mr or Mrs. 500 (532 reputation)Mr or Mrs. 500 (532 reputation)Mr or Mrs. 500 (532 reputation)Mr or Mrs. 500 (532 reputation)Mr or Mrs. 500 (532 reputation)

Group: General Forum Members
Points: 532 Visits: 35

good article, as a beginner for ADO.net, I have one question.

how many new rows will insert by your script? one, number of rows, or the loop will run forever?

Do Until rs.EOF
rs.AddNew
rs.Fields("CustomerID") = "SSC"
rs.Fields("CompanyName") = "SQLServerCentral.Com"
rs.update
Loop





Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum







































































































































































SQLServerCentral


Search