public DataRecordCollection List(PartQuery query)
{
using (IDbCommand cmd = base.DataContext.CreateCommand(ListPart, true))
{
base.AddQueryParameters(cmd, query);
DataContext.Open();
IDataReader reader = cmd.ExecuteReader();
return new DataRecordCollection(typeof(PartRecord), reader);
}
}
返回值的类型是转换的吗?
//这是另一个类
public class DataRecordCollection : IEnumerator, IEnumerable, IDisposable
{
private Type _type;
private IDataReader _reader;
private DataRecord _currentRecord;
private int _pageCount;
private int _pageSize;
private int _recordCount;
private int _startRecord;
public DataRecordCollection(Type type, IDataReader reader, int pageSize, int pageCount)
{
_type = type;
_reader = reader;
_startRecord = 0;
this.PageCount = pageCount;
this.PageSize = pageSize;
}
public DataRecordCollection(Type type, IDataReader reader)
: this(type, reader, 0, 0)
{
_type = type;
_reader = reader;
_startRecord = 0;
this.PageCount = this.PageSize = 0;
}
IEnumerator IEnumerable.GetEnumerator()
{
return this;
}
object IEnumerator.Current
{
get { return _currentRecord; }
}
void IEnumerator.Reset()
{
throw new NotSupportedException();
}
public virtual bool MoveNext()
{
if ((_recordCount<_pageSize || _pageSize==0) && _reader.Read())
{
_recordCount++;
_currentRecord = Activator.CreateInstance(_type, new object[] { (IDataRecord)_reader }) as DataRecord;
return true;
}
else
{
_currentRecord = null;
((IDisposable)this).Dispose();
return false;
}
}
public virtual void Dispose()
{
_reader.Close();
}
private void UpdateStartLocation()
{
if (_pageSize>0)
{
if (_startRecord>_pageCount*_pageSize)
throw new ArgumentException("Cannot move backwards");
for (; _startRecord<_pageCount*_pageSize; _startRecord++)
_reader.Read();
}
}
public int PageSize
{
get { return _pageSize; }
set { _pageSize = value; this.UpdateStartLocation(); }
}
public int PageCount
{
get { return _pageCount; }
set { _pageCount = value; this.UpdateStartLocation(); }
}
public DataTable ToDataTable()
{
DataTable dt = new DataTable(); //this._reader.GetSchemaTable();
for (int i=0; i<this._reader.FieldCount; i++)
{
dt.Columns.Add(this._reader.GetName(i), this._reader.GetFieldType(i));
}
object[] values = new object[_reader.FieldCount];
//while(_reader.Read())
while(((IEnumerator)this).MoveNext())
{
_reader.GetValues(values);
dt.Rows.Add(values);
}
return dt;
}
}
DataRecordCollection这个的作用呢?
------解决方案--------------------------------------------------------
DataRecordCollection
------解决方案--------------------------------------------------------
返回值的类型肯定是DataRecordCollection
------解决方案--------------------------------------------------------
------解决方案--------------------------------------------------------
是的
------解决方案--------------------------------------------------------
数据类型:DataRecordCollection