问题
I'm trying to make a generic reporting system so that I can add reports to my program on the fly, rather than issuing a new version of the program every time I have to add in a report.
Currently, I have it partly working;
My custom report SQL is stored in a table, along with the report name. The report name is used for the buttons for the user to click, when they click the button, I want the SQL to execute and be bound to a gridview, so it's as generic as possible.
This seems to be where the troubles come though. Getting the SQL and executing it is fine, but binding it to a gridview seems impossible, I only get one column, called Column, that displays 1 row with a value of System.Object.
I think the problem is because I'm returning data from multiple tables, i.e.
SELECT c.CertificateName, e.EntityName,
ce.CertificateDate, ce.CertificateExpiry, ce.CertificateNumber
FROM FCERTSTest.dbo.CertificateEntries ce
INNER JOIN FCERTSTest.dbo.Certificates c
ON c.CertificateID = ce.Certificate_ID
INNER JOIN FCERTSTest.dbo.Entities e
ON e.EntityID = ce.Entity_ID
WHERE FirstNotificationSent = '1'
I'm currently using
using (DBContainer db = new DBContainer())
{
grid.DataSource = db.Database.SqlQuery<dynamic>(SQLCommand).ToList();
}
I've tried replacing dynamic
with object
and get the same problem...
I understand that the best solution would be to have a custom class and replace dynamic
with that custom class, but then I lose the benefits of it being generic as a different report may not return the same results and I would need a new custom class...
回答1:
Use SqlDataAdapter
and DataSet
. EF doesn't work with "dynamic queries" where you don't have special type for result set.
来源:https://stackoverflow.com/questions/14743290/entity-framework-custom-sql-query-returning-generic-type