If you want a DataTable
popular, you should not be using DataReader
, but DataAdapter
:
var adapter = new SqlDataAdapter(minhaConsulta, customerConnection);
var dataSet = new DataSet();
adapter.Fill(dataSet, "Tabela");
var dataTable = customerOrders.Tables["Tabela"];
EDIT
PivotGrid - DevExpress
As for PivotGrid
of DevExpress
, I can not help you much, but according to them, the ideal for your scenario is to use Database Server Mode
", either using EntityServerModeDataSource
or LinqServerModeDataSource
as DataSource
. The DevExpress website has this guide on Entity Framework 4.0+ Server Mode
Sql Server Paging
As for paging, you have two options, use the new OFFSET FETCH
, which is available from SQL Server 2012
, or use ROW_NUMBER
, it is available from SQL Server 2005
OFFSET FETCH
DECLARE @page as int
DECLARE @pageSize as int
SET @page = 10
SET @pageSize = 20
SELECT TabelaId, Coluna1, Coluna2, ..., ColunaN
FROM Tabela
ORDER BY TabelaId -- Ou Qual quer outro critério de ordenação
OFFSET (@page * @pageSize) ROWS FETCH NEXT @pageSize ROWS ONLY
In the above example, the OFFSET
will ignore the first 200 records, while the FETCH
will read only the next 20 records. This will return only the records from 201 to 220.
ROW_NUMBER
DECLARE @page as int
DECLARE @pageSize as int
SET @page = 10
SET @pageSize = 20
WITH CTE_Page AS (
SELECT
ROW_NUMBER() OVER (ORDER BY TabelaId) AS Ordem, -- Ou Qual quer outro critério de ordenação
*
FROM Tabela
)
SELECT TabelaId, Coluna1, Coluna2, ..., ColunaN
FROM CTE_Page
WHERE Ordem BETWEEN (@page * @pageSize + 1) AND (@page * @pageSize + @pageSize)
The ROW_NUMBER
will create a numbering of the records sequentially based on the sorting criteria. The WHERE
will filter the records to return only those of order between 201 and 220.
File Read
In this case it is interesting to create a file with a fixed position, so it is possible to search for a record by a specific line.
In the example below I'm using the FlatFile.FixedLength.Attributes
package.
MyClass
using FlatFile.FixedLength;
using FlatFile.FixedLength.Attributes;
[FixedLengthFile]
public class MyClass
{
[FixedLengthField(1, 5, PaddingChar = ' ', Padding = Padding.Right)]
public int Indice { get; set; }
[FixedLengthField(1, 50, PaddingChar = ' ', Padding = Padding.Left)]
public Guid Campo01 { get; set; }
[FixedLengthField(2, 50, PaddingChar = ' ', Padding = Padding.Left)]
public Guid Campo02 { get; set; }
[FixedLengthField(3, 50, PaddingChar = ' ', Padding = Padding.Left)]
public Guid Campo03 { get; set; }
[FixedLengthField(4, 50, PaddingChar = ' ', Padding = Padding.Left)]
public Guid Campo04 { get; set; }
[FixedLengthField(5, 50, PaddingChar = ' ', Padding = Padding.Left)]
public Guid Campo05 { get; set; }
}
Writing a File
var factory = new FixedLengthFileEngineFactory();
var flatFile = factory.GetEngine<MyClass>();
using (var stream = new FileStream(@"D:\Temp\Registros.txt", FileMode.Create))
{
for (var i = 1; i <= 500; i++) {
var registro = new MyClass();
registro.Indice = i;
registro.Campo01 = Guid.NewGuid();
registro.Campo02 = Guid.NewGuid();
registro.Campo03 = Guid.NewGuid();
registro.Campo04 = Guid.NewGuid();
registro.Campo05 = Guid.NewGuid();
flatFile.Write<MyClass>(stream, new List<MyClass> { registro });
}
}
In the above example I am trying to simulate the behavior of DataReader
, you will write register by registry in the file.
Reading the File
var factory = new FixedLengthFileEngineFactory();
var flatFile = factory.GetEngine<MyClass>();
var lineSize = 257;
var page = 10;
var pageSize = 20;
using (var stream = new FileStream(@"D:\Temp\Registros.txt", FileMode.Open))
{
var binary = new byte[pageSize * lineSize];
var inicio = lineSize * (page * pageSize);
stream.Seek((long)inicio, SeekOrigin.Begin);
stream.Read(binary, 0, binary.Length);
using (var memory = new MemoryStream(binary))
{
var registros = flatFile.Read<MyClass>(memory).ToList();
}
}
In the example above, only records from 201 to 220 were read.