|
|
|
SSCertifiable
       
Group: Moderators
Last Login: Thursday, May 09, 2013 12:38 PM
Points: 6,462,
Visits: 1,384
|
|
|
|
|
|
Forum Newbie
      
Group: General Forum Members
Last Login: Thursday, August 08, 2002 12:00 AM
Points: 4,
Visits: 1
|
|
Good shot at basics of ADO.
Vidya
|
|
|
|
|
SSCertifiable
       
Group: Moderators
Last Login: Thursday, May 09, 2013 12:38 PM
Points: 6,462,
Visits: 1,384
|
|
|
|
|
|
SSC Journeyman
      
Group: General Forum Members
Last Login: Wednesday, February 02, 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.
|
|
|
|
|
SSC 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!
|
|
|
|
|
SSCertifiable
       
Group: Moderators
Last Login: Thursday, May 09, 2013 12:38 PM
Points: 6,462,
Visits: 1,384
|
|
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 SQLShare - Learn One New Thing Each Day SQLAndy - My Professional Blog Connect with me on LinkedIn Follow me on Twitter
|
|
|
|
|
SSC Veteran
      
Group: General Forum Members
Last Login: Yesterday @ 10:49 AM
Points: 293,
Visits: 1,367
|
|
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
|
|
|
|
|
SSCertifiable
       
Group: Moderators
Last Login: Thursday, May 09, 2013 12:38 PM
Points: 6,462,
Visits: 1,384
|
|
|
|
|
|
SSChasing 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
|
|
|
|
|
SSC-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
|
|
|
|