ASP.NET
  Home arrow ASP.NET arrow Page 14 - 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  
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

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 - The RollbackTransComplete Event


    (Page 14 of 15 )

    This event fires after a RollbackTrans method call has finished executing.

    RollbackTransComplete(pError, adStatus, pConnection)

    Parameter Type Description
    pError Error An Error object that describes the error that occurred if adStatus is adStatusErrorsOccurred (not set otherwise)
    adStatus EventStatusEnum
    (Long)
    Identifies the status of the event
    pConnection Connection The Connection object upon which the RollbackTrans method was called

    You can use this event to trigger other operations that depend upon the transaction having failed, such as writing to log files. For example:

    Private Sub objConn_RollbackTransComplete( _ 
                 ByVal pError As ADODB.Error, _
                 adStatus As ADODB.EventStatusEnum, _
                 ByVal pConnection As ADODB.Connection)

    Print "Transaction was rolled back. " & _ 
          "Changes have not been saved."

    End Sub

    This could be extremely useful in nightly batch jobs.

    The WillConnect Event

    This event fires before a connection is opened, indicating that the connection is about to be established.

    WillConnect(ConnectionString, UserID, Password, _ 
                Options, adStatus, pConnection)

    Parameter

    Type

    Description
    Connection

    String

    Connection information
    String
    UserID

    String

    User name to use when connecting
    Password

    String

    User password to use when connecting
    Options

    Long

    Extra connection options, as passed into the

    Options parameter of the Connection object’s

    Open method
    adStatus

    EventStatus
    Enum (Long)

    Identifies the status of the event
     
    pConnection Connection

    Connection object for which this event applies

    The parameters supplied can be changed before the method returns-for instance if the user has specified certain connection attributes, but you wish to change them. As an example, imagine an application that allowed the user to specify connection details. You could prevent them from connecting as a certain user, but allow the connection to be established as another:

    Private Sub objConn_WillConnect(ConnectionString As _
              String, UserID As String, Password As String, _
              Options As Long, _
              adStatus As ADODB.EventStatusEnum, _ 
              ByVal pConnection As ADODB.Connection)

       If adStatus = adStatusOK Then
          Select Case UserID
          Case "sa"
             Print "Connection as system " & _
                    "administrator not allowed."
             adStatus = adStatusCancel 
          Case "Guest"
             UserID = "GuestUser"
             Password = "GuestPassword"
          End Select
       End If

    End Sub

    This stops the user trying to connect as sa and cancels the connection attempt. If a user tries to connect as Guest, then the user ID is changed to GuestUser and the connection proceeds. This allows you to have a set of real user details that are hidden, while exposing a viewable set of user details.

    The WillExecute Event

    This event fires before a pending command executes on the connection.

    WillExecute(Source, CursorType, LockType, _ 
           Options, adStatus, pCommand, _
           pRecordset, pConnection)
     

    Name

    Type

    Description
    Source

    String

    The SQL command or stored procedure name
    CursorType

    CursorTypeEnum(Long)

    Type of cursor for the recordset that will be opened (if adOpenUnspecified, cursor type cannot change) 
     
    LockType

    LockTypeEnum(Long) 

    Lock type for the recordset that will be opened (if adLockUnspecified, lock type cannot be changed)
    Options

    Long

    Options that can be used to execute the command or open the recordset, as passed into the Options argument

     

     

     

     

     

    adStatus

    EventStatusEnum(Long) 

    Identifies the status of the event
    pCommand

    Command

    Command object for which this event applies (may be empty if a Command object was not being used)

     

     

     

     

     

     

    pRecordset

    Recordset

    Recordset object for which this event applies

     

     

    (will be empty Recordset object if no recordset returned by Execute method)

     

     

     

    pConnection

    Connection

    Connection object for which this event applies

    The execution parameters can be modified in this procedure, because it is called before the command executes.

    This is particularly useful when building user-query type applications where the user has the ability to set details of the connection, because it allows you to examine the parameters and modify them if necessary. For example:

    Private Sub objConn_WillExecute(Source As String, _ 
         CursorType As ADODB.CursorTypeEnum, _ 
         LockType As ADODB.LockTypeEnum, _ 
         Options As Long, adStatus As ADODB.EventStatusEnum, _
         ByVal pCommand As ADODB.Command, _
         ByVal pRecordset As ADODB.Recordset, _
         ByVal pConnection As ADODB.Connection)

       If Source = "SalaryDetails" Then
          Print " Nice try, but you're not " & _
                "allowed to look at these"
          adStatus = adStatusCancel
       End If

    End Sub

    The preceding code cancels the event if someone tries to connect to the SalaryDetails table.

    You can also use this technique to protect against ad hoc insertions and deletions, and it is an easy way to build business logic into the connection. A better way to protect against this sort of amendment to data or tables is to implement proper security and to use a three-tier business model, where data access is only possible through controlled operations. 

    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 3 hosted by Hostway
    Stay green...Green IT