Implementing a Custom DataReader


Implementing a Custom DataReader

As you know, a DataReader is a class that provides a fast, forward-only method for traversing data returned from a command execution. In the case of SQL Server, this reader is live and connected to the server. In most cases, a DataReader is faster than filling a DataSet because it is a forward-only cursor.

The sample case simulates external data from the web service. That data is actually simulated using a DataTable. For that reason, the RDPDataReader isn't any faster than a DataSet. As mentioned earlier, this is only a sample implementation. In the real world, you would have some kind of proprietary data format for interfacing with the web service. In addition, you might be able to limit the number of rows returned on the first access, so that with subsequent calls to the DataReader.Read method, you could actually hit the web service for additional data in the background. If you feel really motivated, you can try adding that functionality to this provider.

The IDataReader Interface

The IDataReader interface defines the properties and methods described in Table 28.5.

Table 28.5. Properties and Methods of the IDataReader Interface

Property or Method

Description

Depth

Indicates the nesting depth for the current row

IsClosed

Indicates whether the DataReader is closed

RecordsAffected

Where applicable, returns the number of rows affected by the SQL statement that created the reader

Close()

Closes the reader

GetSchemaTable()

Returns a DataTable that describes the schema of the information on which the reader is operating

Nextresult()

Advances the reader to the next set of records when operating on SQL result batches

Read()

Moves the reader to the next record


The RDPDataReader Class

The code in Listing 28.5 shows the custom version of the DataReader developed for the example in this chapter.

Listing 28.5. The Remote Data Provider DataReader Class
 using System; using System.Data; namespace SAMS.CSharpUnleashed.RemoteDataProvider {   public class RDPDataReader : IDataReader   {     private bool open = true;     private DataTable resultSet;     private int currentPosition = 0;     private RDPConnection connection;     internal RDPDataReader( DataTable tbl )     {       resultSet = tbl;     }     internal RDPDataReader( DataTable tbl, RDPConnection connection )     {       this.connection = connection;       resultSet = tbl;     }     #region IDataReader Members     public int RecordsAffected     {       get       {         return -1;       }     }     public bool IsClosed     {       get       {         return !open;       }     }     public bool NextResult()     {       // TODO:  Add RDPDataReader.NextResult implementation       return false;     }     public void Close()     {       open = false;     }     public bool Read()     {       if (++currentPosition >= resultSet.Rows.Count)         return false;       else         return true;     }     public int Depth     {       get       {         return 0;       }     }     public DataTable GetSchemaTable()     {      throw new NotSupportedException();     }     #endregion     #region IDisposable Members     public void Dispose()     {     }     #endregion     #region IDataRecord Members     public int GetInt32(int i)     {       return (int)resultSet.Rows[currentPosition][i];     }     public object this[string name]     {       get       {         return resultSet.Rows[currentPosition][name];       }     }     object System.Data.IDataRecord.this[int i]     {      get      {        return resultSet.Rows[currentPosition][i];      }     }     public object GetValue(int i)     {       return resultSet.Rows[currentPosition][i];     }     public bool IsDBNull(int i)     {       return ( (resultSet.Rows[currentPosition][i] == DBNull.Value) ||         (resultSet.Rows[currentPosition][i] == null ) );     }     public long       GetBytes(int i, long fieldOffset, byte[] buffer, int bufferoffset, int length)     {       throw new NotSupportedException();     }     public byte GetByte(int i)     {       return (byte)resultSet.Rows[currentPosition][i];     }     public Type GetFieldType(int i)     {        return resultSet.Rows[currentPosition][i].GetType();     }     public decimal GetDecimal(int i)     {       return (decimal)resultSet.Rows[currentPosition][i];     }     public int GetValues(object[] values)     {       throw new NotSupportedException();     }     public string GetName(int i)     {       return resultSet.Columns[i].ColumnName;     }     public int FieldCount     {       get       {        return resultSet.Columns.Count;       }     }     public long GetInt64(int i)     {       return (long)resultSet.Rows[currentPosition][i];     }     public double GetDouble(int i)     {        return (double)resultSet.Rows[currentPosition][i];     }     public bool GetBoolean(int i)     {       return (bool)resultSet.Rows[currentPosition][i];     }     public Guid GetGuid(int i)     {       return (Guid)resultSet.Rows[currentPosition][i];     }     public DateTime GetDateTime(int i)     {       return (DateTime)resultSet.Rows[currentPosition][i];     }     public int GetOrdinal(string name)     {       return resultSet.Columns.IndexOf(name);     }     public string GetDataTypeName(int i)     {       return resultSet.Rows[currentPosition][i].GetType().ToString();     }     public float GetFloat(int i)     {       return (float)resultSet.Rows[currentPosition][i];     }     public IDataReader GetData(int i)     {       throw new NotSupportedException();     }     public long       GetChars(int i, long fieldoffset, char[] buffer, int bufferoffset, int length)     {       throw new NotSupportedException();     }     public string GetString(int i)     {       return (string)resultSet.Rows[currentPosition][i];     }     public char GetChar(int i)     {       return (char)resultSet.Rows[currentPosition][i];     }     public short GetInt16(int i)     {       return (short)resultSet.Rows[currentPosition][i];     }     #endregion   } } 



    Visual C#. NET 2003 Unleashed
    Visual C#. NET 2003 Unleashed
    ISBN: 672326760
    EAN: N/A
    Year: 2003
    Pages: 316

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