存储过程返回到数据集在C#.NET [英] Stored procedure return into DataSet in C# .Net

查看:107
本文介绍了存储过程返回到数据集在C#.NET的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从存储过程返回虚表,我想在C#.net使用它的数据集。我的过程是一个有点复杂,找不到怎么回事表,并设置在一个数据集

下面是我的步骤修改

  ALTER PROCEDURE [DBO]。[PROCEDURE1]    @启动日期时间,
    @Finish日期时间,
    @TimeRange时间

开始    SET NOCOUNT ON;    声明@TimeRanges如表(SessionStart日期时间,日期时间SESSIONEND);     与TimeRanges为(
  选择@启动的开始时间,@启动+ @TimeRange的结束时间
  UNION ALL
  选择开始时间+ @TimeRange,结束时间+ @TimeRange
    从TimeRanges
    其中,开始时间< @Finish)
  选择开始时间,结束时间,计数(Test.ScenarioID)作为TotalPeaks
    从TimeRanges为TR左外连接
      dbo.Test作为测试上TR.StartTime< = Test.SessionStartTime和Test.SessionCloseTime< TR.EndTime
    通过TR.StartTime,TR.EndTime组
结束


解决方案

试试这个

 的DataSet DS =新的DataSet(TimeRanges);
    使用(SqlConnection的康恩=新的SqlConnection(的ConnectionString))
    {
            的SqlCommand sqlComm =新的SqlCommand(PROCEDURE1,康恩);
            sqlComm.Parameters.AddWithValue(@开始,开始时间);
            sqlComm.Parameters.AddWithValue(@完成,FinishTime);
            sqlComm.Parameters.AddWithValue(@ TIMERANGE,TIMERANGE);            sqlComm.CommandType = CommandType.StoredProcedure;            SqlDataAdapter的大=新的SqlDataAdapter();
            da.SelectCommand = sqlComm;            da.Fill(DS);
     }

I want to return virtual table from stored procedure and I want to use it in dataset in c# .net. My procedure is a little complex and can't find how to return a table and set it in a dataset

Here is my procedure to modify:

ALTER PROCEDURE [dbo].[Procedure1] 

    @Start datetime, 
    @Finish datetime,
    @TimeRange time
AS
BEGIN

    SET NOCOUNT ON;

    declare @TimeRanges as TABLE (SessionStart datetime, SessionEnd datetime);

     with TimeRanges as (
  select @Start as StartTime, @Start + @TimeRange as EndTime
  union all
  select StartTime + @TimeRange, EndTime + @TimeRange
    from TimeRanges
    where StartTime < @Finish )
  select StartTime, EndTime, Count( Test.ScenarioID ) as TotalPeaks
    from TimeRanges as TR left outer join
      dbo.Test as Test on TR.StartTime <= Test.SessionStartTime and Test.SessionCloseTime < TR.EndTime
    group by TR.StartTime, TR.EndTime   
END

解决方案

Try this

    DataSet ds = new DataSet("TimeRanges");
    using(SqlConnection conn = new SqlConnection("ConnectionString"))
    {               
            SqlCommand sqlComm = new SqlCommand("Procedure1", conn);               
            sqlComm.Parameters.AddWithValue("@Start", StartTime);
            sqlComm.Parameters.AddWithValue("@Finish", FinishTime);
            sqlComm.Parameters.AddWithValue("@TimeRange", TimeRange);

            sqlComm.CommandType = CommandType.StoredProcedure;

            SqlDataAdapter da = new SqlDataAdapter();
            da.SelectCommand = sqlComm;

            da.Fill(ds);
     }

这篇关于存储过程返回到数据集在C#.NET的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆