0

我有一个带有以下代码的存储过程,它在 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 是总机器数。该代码不会产生错误,但不会检索图表中的数据。

dbo.Sieben:

Tag                  Aufträge_anzahl    Anlage
19.07.2013 00:00:00  2                  Siebraum 1
20.07.2013 00:00:00  1                  Siebraum 4
20.07.2013 00:00:00  3                  Siebraum 2
21.07.2013 00:00:00  4                  Siebraum 1
22.07.2013 00:00:00  5                  Siebraum 5

Tag 是当天的日期,Aufträge_anzahl 是该日期的订单数,Anlage 是机器

4

2 回答 2

0

您的命令 r = c.ExecuteReader() 返回一个 SqlDataReader 但您从不使用它。相反,您使用 Fill 再次执行存储过程。我不确定这是否是唯一的问题,但您不需要数据表、数据集或适配器。你可以像这样阅读你的阅读器:

while( r.Read() )
{
     var something = r["fieldname"];
}

另一件事是您将 DataRow 转换为 Int32,如果您使用 DataTable 而不是 DataReader,那么您应该包含一个字段名称,例如

foreach( DataRow row in DataTable.Rows)
{
    var something = row["field"];
}
于 2013-07-21T01:26:16.840 回答
0

在这一行中:

c.Parameters.Add("@raumn", SqlDbType.Int).Value= (int)dblOrders[i];

应该:

c.Parameters.Add(new SqlParameter("@id_beleg",SqlDbType.Int)).Value= (int)dblOrders[1];

即sql参数名应该和存储过程中使用的一样,@id_beleg

于 2013-07-22T08:59:58.327 回答