I am trying to fill a DataTable reading a view using SqliteDataReader. The following code works when I am reading a Table (not a view)
private DataTable ReadDataTable(SqliteCommand command)
{
DataTable result = new();
using (var connection = Connection)
{
connection.Open();
SqliteDataReader reader = command.ExecuteReader();
result.Load(reader);
}
return result;
}
However, when the table in question is a view, containing multiple rows, the resulting table only contains one row. To test I tried the following function, running the same command
private int ReadDataTableTest(SqliteCommand command)
{
int result = 0;
using (var connection = Connection)
{
connection.Open();
SqliteDataReader reader = command.ExecuteReader();
while(reader.Read())
result++;
}
return result;
}
This returned 6, the correct answer. Also, querying the table directly in SqliteStudio gives the right answer. The top function seems only to insert the last row of the reader in the DataTable. What is going on here?
Btw, I don't know for sure that the issue is that the table is a view, but I have a similar issue with many other views, and the "view/table" thing seems to be the common denominator.
I am using Windows, coding in .net6. using the following imports
using Microsoft.Data.Sqlite;
using System.Data;
EDIT
I am creating the command like this
private SqliteCommand CreateCommand(string tableName, DateTime reportDate)
{
var command = Connection.CreateCommand();
command.CommandText = @$"SELECT * FROM {tableName} WHERE ReportDate = $reportDate";
command.Parameters.AddWithValue("$reportDate", reportDate.ToSqliteDate());
return command;
}
And calling the code like this
var reportDate = new DateTime(2022, 7, 31);
var table = ReadDataTable(CreateCommand("MyTable", reportDate));
I am not doing anything to the table after the call.
EDIT 2
The following query generates the view
SELECT [of].ReportDate,
[of].Entity,
SUM(CASE WHEN [of].Item IN ('Equity') THEN [of].Value / fx.FxRate ELSE 0 END) AS EquityLocal,
SUM(CASE WHEN [of].Item IN ('AdjIFRS') THEN [of].Value / fx.FxRate ELSE 0 END) AS AdjIFRSLocal,
SUM(CASE WHEN [of].Item IN ('AdjIFRSGroup') THEN [of].Value / fx.FxRate ELSE 0 END) AS AdjIFRSGroup,
SUM(CASE WHEN Entity != 'Holding' AND
[of].Item IN ('Equity', 'AdjIFRS', 'AdjIFRSGroup') THEN -[of].Value / fx.FxRate ELSE 0 END) AS Elimination,
SUM(CASE WHEN Entity = 'Holding' AND
[of].Item IN ('Equity', 'AdjIFRS', 'AdjIFRSGroup') THEN [of].Value / fx.FxRate ELSE 0 END) AS EquityIFRS,
'EUR' AS ReportCurrency
FROM InputOwnFunds AS [of]
JOIN InputEurFxRates AS fx
ON [of].ReportDate = fx.ReportDate AND
[of].ReportCurrency = fx.ReportCurrency
WHERE of.Item in ('Equity', 'AdjIFRS', 'AdjIFRSGroup')
GROUP BY [of].ReportDate,
[of].Entity
UNION
SELECT [of].ReportDate,
'NLP Group' as Entity,
SUM(CASE WHEN [of].Item IN ('Equity') THEN [of].Value / fx.FxRate ELSE 0 END) AS EquityLocal,
SUM(CASE WHEN [of].Item IN ('AdjIFRS') THEN [of].Value / fx.FxRate ELSE 0 END) AS AdjIFRSLocal,
SUM(CASE WHEN [of].Item IN ('AdjIFRSGroup') THEN [of].Value / fx.FxRate ELSE 0 END) AS AdjIFRSGroup,
SUM(CASE WHEN Entity != 'Holding' AND
[of].Item IN ('Equity', 'AdjIFRS', 'AdjIFRSGroup') THEN -[of].Value / fx.FxRate ELSE 0 END) AS Elimination,
SUM(CASE WHEN Entity = 'Holding' AND
[of].Item IN ('Equity', 'AdjIFRS', 'AdjIFRSGroup') THEN [of].Value / fx.FxRate ELSE 0 END) AS EquityIFRS,
'EUR' AS ReportCurrency
FROM InputOwnFunds AS [of]
JOIN InputEurFxRates AS fx
ON [of].ReportDate = fx.ReportDate AND
[of].ReportCurrency = fx.ReportCurrency
WHERE of.Item in ('Equity', 'AdjIFRS', 'AdjIFRSGroup')
GROUP BY [of].ReportDate