|
|
|
Implementing a Custom DataReaderAs 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
The IDataReader Interface
The
IDataReader
interface defines the
properties and
Table 28.5. Properties and Methods of the IDataReader Interface
The RDPDataReader ClassThe 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
}
}
|
|
|
|