The IDbCommand Interface


The IDbCommand Interface

The Command class is another core component for any .NET managed data provider. The Command object implements the IDbCommand interface and is responsible for formulating a database statement or command and passing it on to the underlying data source. This is the main class that is leveraged by a concrete DBCommandWrapper class. The Command object is responsible for returning results as a DataReader, a scalar value, or as parameters.

Classes that implement the IDbCommand interface must often define additional functions to add provider-specific functionality. For example, like the SqlCommand, the XmlFileCommand class defines the ExecuteXmlReader method in addition to providing an implementation for all the other methods defined by the IDbCommand interface. Listing B.4 illustrates how the XmlFileCommand implements the IDbCommand interface.

Listing B.4. XmlFileCommand Implements IDbCommand

[C#] public class XmlFileCommand : IDbCommand {     ...     public CommandType CommandType     {          get          {               return commandType;          }          set          {               // Only need to support text per SDK.               commandType = value;          }     }     public IDataReader ExecuteReader(CommandBehavior behavior)     {          // Must have a valid and open connection.          if (connection == null ||                    connection.State != ConnectionState.Open)               throw new                    InvalidOperationException                    (SR.ExceptionConnectionOpen);          return ExecuteReader();     }     public IDataReader ExecuteReader()     {          // Must have a valid and open connection.          if (connection == null ||              connection.State != ConnectionState.Open)                   throw new                         InvalidOperationException                         (SR.ExceptionConnectionOpen);          XmlFileDataReader reader =               new XmlFileDataReader(connection.Database);          return reader;     }     public XmlReader ExecuteXmlReader()     {          // Must have a valid and open connection.          if (connection == null ||                    connection.State != ConnectionState.Open)               throw new                    InvalidOperationException(                    SR.ExceptionConnectionOpen);          XmlFileDataReader reader =               new XmlFileDataReader(connection.Database);          return reader.XmlReader;     }     //Updates file and returns number of records.     public int ExecuteNonQuery()     {          // Must have a valid and open connection.          if (connection == null ||               connection.State != ConnectionState.Open)               throw new                    InvalidOperationException(                    SR.ExceptionConnectionOpen);          int recs;          XmlFileDataReader reader =                (XmlFileDataReader)ExecuteReader();          recs = reader.RecordsAffected;          reader.Close();          return recs;     }     ... } [Visual Basic] Public Class XmlFileCommand : Implements IDbCommand     ...     Public Property CommandType() As CommandType _               Implements IDbCommand.CommandType          Get               Return commandType          End Get          Set               ' Only need to support text per SDK.               commandType = Value          End Set     End Property     Public Function ExecuteReader _           (ByVal behavior As CommandBehavior) _               As IDataReader Implements IDbCommand.ExecuteReader          ' Must have a valid and open connection.          If connection Is Nothing OrElse _                   connection.State <> ConnectionState.Open Then               Throw New _                    InvalidOperationException _                    (SR.ExceptionConnectionOpen)          End If          Return ExecuteReader()     End Function     Public Function ExecuteReader() As IDataReader _                    Implements IDbCommand.ExecuteReader          ' Must have a valid and open connection.          If connection Is Nothing OrElse  _                    connection.State <> ConnectionState.Open Then               Throw New _                    InvalidOperationException _                    (SR.ExceptionConnectionOpen)          End If          Dim reader As XmlFileDataReader = _               New XmlFileDataReader(connection.Database)          Return reader     End Function     Public Function ExecuteXmlReader() As XmlReader          ' Must have a valid and open connection.          If connection Is Nothing OrElse _                    connection.State <> ConnectionState.Open Then               Throw New _                    InvalidOperationException _                    (SR.ExceptionConnectionOpen)          End If          Dim reader As XmlFileDataReader = _                    New XmlFileDataReader(connection.Database)          Return reader.XmlReader     End Function     'Updates file and returns number of records.     Public Function ExecuteNonQuery() As Integer _               Implements IDbCommand.ExecuteNonQuery          ' Must have a valid and open connection.          If connection Is Nothing OrElse _                    connection.State <> ConnectionState.Open Then               Throw New _                    InvalidOperationException _                    (SR.ExceptionConnectionOpen)          End If          Dim recs As Integer          Dim reader As XmlFileDataReader = _                    CType(ExecuteReader(), XmlFileDataReader)          recs = reader.RecordsAffected          reader.Close()          Return recs     End Function     ... End Class




Fenster Effective Use of Microsoft Enterprise Library(c) Building Blocks for Creating Enterprise Applications and Services 2006
Effective Use of Microsoft Enterprise Library: Building Blocks for Creating Enterprise Applications and Services
ISBN: 0321334213
EAN: 2147483647
Year: 2004
Pages: 103
Authors: Len Fenster

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net