I have an sql pivot query which results in dynamic sql columns. How do I read these values into a C# object?
I haven't had much success while I am able to read values from the datareader. I am unable to pack it into an object.
I need to use datareader and pass an object through the service layer to the UI.
sql code similar to below,
DECLARE @cols AS NVARCHAR(MAX),
@query AS NVARCHAR(MAX)
SELECT @cols = STUFF((SELECT ',' + QUOTENAME([MONTH])
FROM #REVENUE
FOR XML PATH(''), TYPE
).value('.', 'NVARCHAR(MAX)')
,1,1,'')
SELECT @query =
'SELECT * FROM
(SELECT
[MONTH],
SALES
FROM #REVENUE)X
PIVOT
(
AVG(SALES)
for [MONTH] in (' + @cols + ')
) P
EXEC SP_EXECUTESQL @query