从存储过程接收多行

时间:2013-07-19 10:36:20

标签: c# asp.net stored-procedures

我有一个带有以下代码的存储过程,它在sql server中工作正常。我需要使用返回的行来处理堆积的柱形图。我不知道如何接收这些行,尽管我已尝试使用存储过程代码后给出的代码。如果有人能找到我做错的地方,我真的很感激。谢谢:))

存储过程代码:

ALTER PROCEDURE [dbo].[weekly]

@id_beleg int           /* CONTAINS THE ID OF THE MACHINE FOR WHICH THE NUMBER OF ORDERS IS REQUIRED*/

AS

BEGIN

DECLARE @montag DATETIME            
DECLARE @weekend DATETIME

SET @montag = DATEADD(wk, DATEDIFF(wk,0,GETDATE()), 0)          
/* SET @MONTAG VARIABLE TO THE DATE ON MONDAY OF THE CURRENT WEEK*/

SET @weekend = dateadd(day, 6, @montag)                         
/* SET @WEEKEND VARIABLE TO THE DATE ON SUNDAY OF THE CURRENT WEEK*/

/* CONTAINS ALL RECORDS OF one PROCESS FOR THE CURRENT WEEK*/

CREATE TABLE #WeeklySieben
(
Tag datetime,

Aufträge_anzahl int,

Anlage varchar(30)
);

INSERT INTO #WeeklySieben (Tag, Aufträge_anzahl, Anlage)
SELECT * 
FROM 
    dbo.Sieben 

WHERE 
    Tag BETWEEN @montag AND GETDATE()

ORDER BY Tag


DECLARE @tempdate datetime

/* CONTAINS ONLY THE UNIQUE MACHINE NAMES*/

CREATE TABLE #Anlage_Sieben             
(   
Anl varchar(30)
)


/* CONTAINS DATE FOR ALL DAYS OF THE CURRENT WEEK*/


CREATE TABLE #alldays       
(   
days datetime
)



INSERT INTO #Anlage_Sieben (Anl)

SELECT      KURZTEXT    as Anl

FROM        dbo.tblBELEGUNGSEINHEIT

WHERE       ID  in (SELECT ID_BELEGUNGSEINHEIT FROM    dbo.tblPROZESS_BELEGUNGSEINHEIT WHERE ID_PROZESS = 5)    
/* SELECT MACHINE ID WHERE PROCESS IS number 5*/     



WHILE @montag <= @weekend

    BEGIN
        INSERT INTO #alldays (days)
        SELECT days = @montag
        SET @tempdate= @montag
        SET @montag = dateadd(day, 1,@tempdate) 
    END


/* MACHINE NAME * WEEKDAYS DATE*/

CREATE TABLE #weekdays      
(

    daydate datetime,

Anlage varchar(30)
)


INSERT INTO #weekdays (daydate, Anlage)
SELECT      d.days as daydate
        , a.Anl as Anlage
FROM        #Anlage_Sieben a
CROSS JOIN  #alldays d



/* SELECT NUMBER OF ORDERS ON A PARTICULAR MACHINE FOR ALL WEEK DAYS*/

Select      case
        when Aufträge_anzahl is null then '0' 
        else Aufträge_anzahl    
        end  
from        #WeeklySieben W right join #weekdays wd on W.Anlage = wd.Anlage and W.Tag = wd.daydate 
where       wd.Anlage in (select KURZTEXT from dbo.tblBELEGUNGSEINHEIT where ID = @id_beleg  )

END

C#代码:

try
{
    int a;
    string sql = "weekly";
    SqlDataAdapter dp = new SqlDataAdapter();
    SqlCommand c = null;
    SqlDataReader r = null;
    DataSet ds = new DataSet();
    DataTable dt = new DataTable();
    //For creating 'raum' number of stacked column bars
    for (int i = 0; i < raum; i++)
    {
        c = new SqlCommand(sql, conn);                    
        c.CommandType = CommandType.StoredProcedure;
        c.Parameters.Add("@raumn", SqlDbType.Int).Value= (int)dblOrders[i];
        r = c.ExecuteReader();
        dp.SelectCommand = c;
        dp.Fill(ds, "Aufträge_anzahl");
        dt = ds.Tables["Aufträge_anzahl"];
        //For adding points to a stacked column bar
        for (int j = 0; j < dt.Rows.Count; j++)
        {
            a = Convert.ToInt32(dt.Rows[j]);
            Chart1.Series[seriesArray[j]].Points.AddY(a);
        } 

        Chart1.DataBind();
        dtSr.Clear();
    }
}
catch (SqlException ex)
{
    Console.WriteLine("SQL Error" + ex.Message.ToString());
}

dblorders是双数组,它包含不同机器的ID,raum是总机器数。代码不会生成错误,但不会检索图表中的数据。

0 个答案:

没有答案