if you want to popular a DataTable
, should not be using a DataReader
, but rather a DataAdapter
.:
var adapter = new SqlDataAdapter(minhaConsulta, customerConnection);
var dataSet = new DataSet();
adapter.Fill(dataSet, "Tabela");
var dataTable = customerOrders.Tables["Tabela"];
EDIT
Pivotgrid - Devexpress
As to the PivotGrid
of DevExpress
, I can’t help you much, but according to them, the ideal scenario for you is to use Database Server Mode
, either using a EntityServerModeDataSource
or LinqServerModeDataSource
as DataSource
. On the Devexpress website is 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 the SQL Server 2012
, or use the ROW_NUMBER
, this is available from the 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 example above, the OFFSET
will ignore the first 200 records, while the FETCH
will read only the next 20 records. This way will return only the records from number 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 number of records sequentially based on the sort criteria. The WHERE
will filter the records to return only those of order between 201 and 220.
File reading
In this case it is interesting to create a file with fixed position, so if possible to search a record by a specific line.
In the example below I am using the package FlatFile.FixedLength.Attributes
.
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 an Archive
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 example above I am trying to simulate the behavior of DataReader
, you will write record by record in the file.
Reading from the Archive
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 above example, only records from 201 to 220 were read.
Try to see this, I think you need it: https://stackoverflow.com/questions/20860101/how-to-read-text-file-to-datatable?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa
– Joana Brandão
Hi Joana, thanks really, helped me a lot to find a solution !
– Nardier Barbosa