Introduction to ADO Part 2 - Recordsets

  • Good shot at basics of ADO.

    Vidya

  • 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

  • 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.

  • 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!

  • 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/

  • 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

  • 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/

  • 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).

     

  • 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

  • Hi Andy

    We use ForwardOnly, Readonly, Serverside recordsets for populating combos & lists etc just as you suggest but we are increasingly having problems with values wrongly being returned as nulls. We have moved away from this method when memo fields are involved but now we are having the same problem with numerics. (nb the problem goes away if we use Keyset) Any ideas about what is going on?

    thanks

    Kath

Viewing 11 posts - 1 through 10 (of 10 total)

You must be logged in to reply to this topic. Login to reply