使用c#从存储过程中检索多行

时间:2013-07-20 20:51:20

标签: c# asp.net sql-server 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是总机器数。代码不会生成错误,但它不会检索图表中的数据。

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

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

2 个答案:

答案 0 :(得分: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"];
}

答案 1 :(得分: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