如何使用DataReader或DataSet类从数据透视表列中读取值

时间:2014-01-30 04:38:20

标签: c# sql-server-2008 ado.net

这是我的商店程序,我的问题是如何读取DataReader或DataSet中的列值。

ALTER PROCEDURE StudentProgressReport   
BEGIN   
    SET NOCOUNT ON;  

DECLARE @cols AS NVARCHAR(MAX),
    @query  AS NVARCHAR(MAX)

SET @cols = STUFF((SELECT distinct ',' + QUOTENAME(r.Exam_Date) 
    FROM Institute_Student_Results r
    FOR XML PATH(''), TYPE).value('.', 'NVARCHAR(MAX)'),1,1,'')

set @query = 'SELECT Student_Roll_No,Student_First_Name,Student_Last_Name,' + @cols + ' from 
        (
            select distinct  u.Student_Roll_No,
            s.Student_First_Name,s.Student_Last_Name,               
            u.Marks,e.Examination_Total_Marks,
            u.Exam_Date

            from Institute_Student_Results u

            inner join Institute_Examinations e
            on u.Exam_ID=e.Institute_Examination_ID

            inner join Student_Master s
            on u.Student_ID=s.Student_ID

            where e.Institute_Course_Batch_ID=110
            and u.Exam_ID=105
            and u.Marks is not null             

       ) x
        pivot 
        (
            MAX(Marks)
            for Exam_Date in (' + @cols + ')
        ) p '

   execute(@query)     

END

1 个答案:

答案 0 :(得分:0)

您可以使用SqlDataAdapter执行存储过程并使用结果填充DataSet。有关填充DataSet的示例,请参阅此SO post;如果要填充DataTable,请参阅此one