ASP.NET
  Home arrow ASP.NET arrow Page 9 - What is ADO?
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Mobile Linux 
App Generation ROI 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
ASP.NET

What is ADO?
By: Apress Publishing
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 16
    2004-08-11

    Table of Contents:
  • What is ADO?
  • About Universal Data Access
  • Existing Technologies
  • Why ADO?
  • ADO and ADO.NET
  • Providers and Drivers
  • New Features
  • ADO 2.8
  • Examples 1-3
  • Examples 4-6
  • Language Differences
  • Creating Objects in JScript, Visual C and .NET

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    What is ADO? - Examples 1-3


    (Page 9 of 12 )

    Because this is primarily a reference book, I’ll give you a few documented samples of code so that you can see what’s possible and how some of the components fit together. You can use this as a sort of “table of contents” to the rest of the book, where the various aspects are covered in more detail. A later section of this chapter provide more detail about language specifics, so I use Visual Basic here because it’s easy to read and understand.

    Example 1

    The following section of Visual Basic code creates a connection to SQL Server, opens a recordset, and then adds some details from the recordset to a listbox:

    ' Define two object variables
    ' The object model is discussed in Chapter 2
    Dim objConn As ADODB.Connection
    Dim objRs As ADODB.Recordset
    Set objConn = New ADODB.Connection
    Set objRs As New ADODB.Recordset

    ' Open a connection to the pubs database using the
    ' SQL Server OLE DB Provider
    ' Connection strings are discussed in Chapter 2
    ' The Connection object is discussed in Chapter 3
    objConn.Open "Provider=SQLOLE DB; Data Source=Tigger; " &_
                 "Initial Catalog=pubs; User Id=sa; Password="

    ' Open a recordset on the 'authors' table
    ' The Recordset is discussed in Chapter 5
    objRs.Open "authors", objConn, adOpenForwardOnly, _ 
                adLockReadOnly, adCmdTable

    ' Loop while we haven't reached the end of the recordset
    ' The EOF property is set to True when we reach the end
    While Not objRs.EOF
       ' Add the names to the listbox, using the default Fields collection
       ' The Fields collection is discussed in Chapter 6
       List1.AddItem objRs.Fields("au_fname") & " " & _
                     objRs.Fields("au_lname")
       ' Move to the next record
       objRs.MoveNext
    Wend

    ' Close the objects
    objRs.Close
    objConn.Close

    ' Release the memory associated with the objects
    Set objRs = Nothing
    Set objConn = Nothing 
     

    Example 2

    This example runs a SQL statement and doesn’t expect a set of records to be returned. It also uses an ODBC Data Source Name called pubs, which has been set up previously.

    ' Define the object variable
    Dim objConn As ADODB.Connection
    Set objConn = New ADODB.Connection

    ' Open a connection to the pubs database using the
    ' OLE DB Provider for ODBC
    objConn.Open "DSN=pubs; UID=sa; PWD="

    ' Run a SQL UPDATE statement to update book prices by 10%
    objConn.Execute "UPDATE titles SET price = price * 1.10" _
            , , adExecuteNoRecords + adCmdText

    ' Close the connection
    objConn.Close

    ' Release the memory associated with the objects
    Set objConn = Nothing

    Example 3

    This example runs a stored query in Access, passing in some parameters:

    ' Declare the object variables
    ' The Command object is discussed in Chapter 4
    ' The Parameter object is discussed in Chapter 4
    Dim objConn     As ADODB.Connection
    Dim objCmd      As ADODB.Command
    Dim objRs        As ADODB.Recordset
    Dim objParam    As ADODB.Parameter
    Set objConn = New ADODB.Connection
    Set objCmd = New ADODB.Command

    ' Open a connection to an Access pubs database,
    ' using the Access OLE DB provider
    objConn.Open "Provider=Microsoft.Jet.OLE DB.4.0; " & _
            "Data Source=C:\temp\pubs.mdb" 

    ' Create a new Parameter, called RequiredState
    Set objParam = objCmd.CreateParameter("RequiredState", _
                   adChar, _ adParamInput, 2, "CA")

    ' Add the Parameter to the Parameters collection of the Command object
    objCmd.Parameters.Append objParam

    ' Set the active connection of the command to the open Connection object
    Set objCmd.ActiveConnection = objConn

    ' Set the name of the stored query that is to be run
    objCmd.CommandText = "qryAuthorsBooksByState"

    ' Set the type of command
    objCmd.CommandType = adCmdStoredProc

    ' Run the stored query and set the recordset which it returns
    Set objRs = objCmd.Execute

    ' Loop through the recordset, adding the items to a listbox
    While Not objRs.EOF
         List1.AddItem objRs.Fields("au_fname") & " " & _ 
                          objRs.Fields("au_lname") & _ 
                          ": " & objRs.Fields("title")
         objRs.MoveNext
    Wend

    ' Close the recordset and connection
    objRs.Close
    objConn.Close

    ' Reclaim the memory from the objects
    Set objRs = Nothing
    Set objCmd = Nothing
    Set objParam = Nothing
    Set objConn = Nothing

     

    This is from ADO Programmer's Reference, by Dave Sussman (Apress, ISBN 1590593421). Check it out at your favorite bookstore today. Buy this book now.

    More ASP.NET Articles
    More By Apress Publishing


     

    ASP.NET ARTICLES

    - Advantages of the ASP.NET MVC Approach
    - ASP.NET Web Forms Weaknesses
    - ASP.NET Web Forms Meets ASP.NET MVC
    - Source Code for Saving and Retrieving Data w...
    - Using GridView to Save and Retrieve Data wit...
    - Handling Dynamic Images in ASP.NET 3.5 AJAX ...
    - Retrieving Data with AJAX and the GridView C...
    - Playing with Images in ASP.NET 3.5 AJAX Appl...
    - Saving and Retrieving Data with AJAX
    - Enhancing PHP Via the ASP.NET AJAX Framework...
    - Enhancing PHP Programming with the ASP.NET A...
    - Classes and ASP.NET AJAX
    - Using ASP.NET AJAX
    - Building a Simple Storefront with LINQ
    - Developing a Dice Game Using ASP.NET Futures...





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway
    Stay green...Green IT