ASP.NET
  Home arrow ASP.NET arrow The Connection Object
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  
Silverlight  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
ASP Web Hosting  
ASP.NET Web Hosting 
Windows Web Hosting
 
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

The Connection Object
By: Apress Publishing
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 19
    2004-09-27

    Table of Contents:
  • The Connection Object
  • Connection State
  • The Cancel Method
  • The Execute Method
  • The Open Method
  • The OpenSchema Method
  • Properties of the Connection Object
  • The ConnectionTimeout Property and More
  • The Mode Property and Provider Property
  • The State Property and Version Property
  • Events of the Connection Object
  • The BeginTransComplete Event
  • The Disconnect Event
  • The RollbackTransComplete Event
  • Collections of the Connection Object

  • 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


    The Connection Object


    (Page 1 of 15 )

    ADO (ActivX Data Objects) gives us a way to manage data from all kinds of data stores, not just relational databases. This chapter looks at how to work with the Connection object and the methods, properties, collections and events that this object makes available to us. (From the book ADO Programmer's Reference by David Sussman, ISBN: 1590593421, Apress, 2004.)

    SussmanThe Connection object is what connects the consumer to the provider; it’s the link between the program and the data. As you’ve already seen, the flat model of ADO means that Connection objects don’t need to be created explicitly. Instead, you can pass a connection string directly to a Command or Recordset object, and ADO will create the Connection object for you. However, explicitly creating a Connection object is worthwhile if you are planning to retrieve data from the data source more than once, because you won’t have to establish a fresh connection each time.

    Connection Pooling

    In the newsgroups during the early days of ADO, there was a lot of talk about connection pooling and whether the benefits really are worthwhile. One of the most time-consuming operations you can perform is the act of connecting to a data store, so anything that speeds this up is defined as "a good thing."

    Connection pooling means that ADO will not actually destroy Connection objects unless it really needs to. The signal for destroying a Connection object is dictated by a timeout value; if the connection hasn’t been reused within the allotted time, it is destroyed. Here’s how it works. Suppose you open a connection, perform some data access, and close the connection. From your point of view, the connection is closed. But underneath, OLE DB keeps the connection in a pool, ready for it to be used again. If you decide that you need to open a connection to the same data store again, you will be given a Connection object from the pool of connections, and ADO doesn’t have to perform all of the expensive data store stuff again. You may not necessarily get the exact same connection object back, but you’ll get one that matches the same connection details that you previously used. In fact, existing objects will be given to anyone who requests them, so connection pooling is even more effective in a multi-user system.

    One important point to note about connection pooling is that connections will be reused only if they match the exact connection details. So on multi-user systems, if you specify the same data store but use different user names and passwords, you will create a new connection rather than having one reused from the pool. This may seem like a disadvantage, but pooling must be done this way to avoid breaking security; it just wouldn’t be right to reuse a connection based on different user details. You could, of course, create a generic user and perform all of your data access through this user, thus maximizing the use of pooling. Under version 2.8 an access token check is performed before the SID when creating pooled sessions. This ensures that delegated credentials are used correctly.

    You should also realize that connection pooling is not the same as connection sharing. An individual connection is not shared among multiple connection requests. Pooling means that closed connections are reused; open connections are not.

    You can test to see whether connection pooling really is working by using a tool to monitor the active connections to a data store. For example, with Microsoft SQL Server you can use SQL Trace. Executing the following code shows only one connection being opened:

    For iLoop = 1 To 5 
       objConn.Open strConn 
       objConn.Close
    Next

    However, if you turn off connection pooling, you’ll find that the five connections are opened and then immediately closed, one after another.

    Although you probably wouldn’t want to, you can turn off connection pooling by adding the attribute OLE DB Services=-2 to the end of the connection string:

    strConn = " . . .; OLE DB Services = -2"

    Alternatively, you can achieve the same result by setting the Connection object’s Properties entry equal to -2 as in this example:

    objConn.Properties("OLE DB Services") = -2

    This property takes values from the DBPROPVAL_OS constants (see Appendix B). If you’ve already flicked to the back of the book to find the constant with a value of -2, you’ll notice there isn’t one. There is, however, a constant to turn on connection pooling, so to turn it off you must use some binary arithmetic. Take the value for turning on connection pooling and perform a logical NOT operation on it.

    DBPROPVAL_OS_RESOURCEPOOLING has a value of 1, so:

    DBPROPVAL_OS_RESOURCEPOOLING = 00000001
    NOT DBPROPVAL_OS_RESOURCEPOOLING = 11111110

    and this equals -2.

    If you’re using an include file that contains these constants, you can use this format:

    objConn.Properties("OLE DB Services") = _
            
    DBPROPVAL_OS_ENABLEALL 
             AND _ (NOT DBPROPVAL_OS_RESOURCEPOOLING)

    This says we want all services enabled, apart from resource pooling.

    The Properties collection is discussed in more detail in Chapter 8.

    For ODBC connections, the ODBC Control Panel applet controls connection pooling. 

    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

    - Adding Content to a Static ASP.NET Website
    - Building a Static ASP.NET Website in a Basic...
    - Develop Your First ASP.NET Website with Visu...
    - Run ASP.NET in Windows XP Home with Cassini ...
    - How to Test a Web Application
    - How to Add Code and Validation Controls to a...
    - Working in Source and Split Views to Build a...
    - How to Build a Web Form for a One-Page Web A...
    - How to Develop a One-Page Web Application
    - An ASP.NET Web Application in Action
    - Developing ASP.NET Web Applications
    - An Introduction to ASP.NET Web Programming
    - Introduction to the ADO.NET Entity Framework...
    - Completing an In-Text Advertising System und...
    - Programming an In-Text Advertising System un...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 4 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek