I execute the following query in a table containing about 600 columns:
SELECT *
FROM Table
WHERE [Owner] = 1234
I run this query using EF code and Dapper and I am facing the same issue with both.
In particular, many rows that DO matter are returned as DBNullfrom a query (I checked using SQL Server Management Studio that the column data has data). Curiously, this only happens when querying all columns (using *or pulling them explicitly).
For example, if a column Statushas a value "A", the query returns DBNullas its value. But if instead of the above code (which pulls out 600 columns), I use this query:
SELECT [Status]
FROM Table
WHERE [Owner] = 1234
Status .
Dapper, :
public IList<Dictionary<string, string>> GetData() {
var sql = "SELECT * FROM Table WHERE [Owner] = 1234";
var cn = new SqlConnection(serverConnectionString);
var rows = new List<Dictionary<string, string>>();
using (var reader = cn.ExecuteReader(sql))
{
while (reader.Read())
{
var dict = new Dictionary<string, string>();
for (var i = 0; i < reader.FieldCount; i++)
{
var propName = reader.GetName(i).ToLowerInvariant();
var propValue = reader.GetValue(i);
dict[propName] = propValue?.ToString();
}
rows.Add(dict);
}
}
return rows;
}
. .