Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase 12»»

Introduction to ADO Part 2 - Recordsets Expand / Collapse
Author
Message
Posted Sunday, November 11, 2001 12:00 AM
SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: Moderators
Last Login: Yesterday @ 2:30 PM
Points: 6,784, Visits: 1,899
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
Post #1608
Posted Wednesday, December 5, 2001 4:24 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Thursday, August 8, 2002 12:00 AM
Points: 4, Visits: 1
Good shot at basics of ADO.

Vidya




Post #23759
Posted Wednesday, December 5, 2001 4:54 AM
SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: Moderators
Last Login: Yesterday @ 2:30 PM
Points: 6,784, Visits: 1,899
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
Post #23760
Posted Friday, November 28, 2003 4:33 AM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Wednesday, February 2, 2005 4:46 AM
Points: 90, 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.




Post #23761
Posted Friday, November 28, 2003 7:47 AM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Tuesday, December 23, 2008 11:15 AM
Points: 77, 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!
Post #23762
Posted Friday, November 28, 2003 10:53 AM
SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: Moderators
Last Login: Yesterday @ 2:30 PM
Points: 6,784, Visits: 1,899
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
Post #23763
Posted Monday, December 1, 2003 7:40 AM
Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Monday, September 15, 2014 7:12 AM
Points: 320, Visits: 1,615
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




Post #23764
Posted Monday, December 1, 2003 2:04 PM
SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: Moderators
Last Login: Yesterday @ 2:30 PM
Points: 6,784, Visits: 1,899
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
Post #23765
Posted Monday, November 29, 2004 8:28 AM
SSChasing Mays

SSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing Mays

Group: General Forum Members
Last Login: Wednesday, December 14, 2011 12:03 PM
Points: 613, 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
Post #148376
Posted Monday, November 29, 2004 2:33 PM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Thursday, May 22, 2008 2:03 PM
Points: 180, 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




Post #148449
« Prev Topic | Next Topic »

Add to briefcase 12»»

Permissions Expand / Collapse