日期时间1天长度

时间:2012-10-22 07:17:45

标签: c# sql datetime

我想在天之间创建报告,我每天选择生成报告。当我将时间间隔设置为24:00:00.000时,它会出错: 日历System.Globalization.GregorianCalendar中不支持字符串表示的DateTime。

23:59:59导致我的报告延迟。

日期时间格式的1天的长度是多少?

这是我在MSSQL Server中的程序

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 EndTime < @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

并将此程序称为

private DataSet GetStoredProcedure1(DateTime StartTime, DateTime FinishTime, DateTime TimeRange)
        {
            DataSet ds = new DataSet();
            SqlCommand cmd = new SqlCommand("Procedure1", connection);
            cmd.CommandType = CommandType.StoredProcedure;

            cmd.Parameters.AddWithValue("@Start", SqlDbType.DateTime).Value = StartTime;
            cmd.Parameters.AddWithValue("@Finish", SqlDbType.DateTime).Value = FinishTime;
            cmd.Parameters.AddWithValue("@TimeRange", SqlDbType.DateTime).Value = TimeRange;

            SqlDataAdapter da = new SqlDataAdapter();
            da.SelectCommand = cmd;
            da.Fill(ds);
            return ds;
        }

当我给出时间范围00:05:00.00持续5分钟01:00:00.000持续1小时我的代码没有任何问题,但是当我想要每日报告时24:00:00.00不起作用。我该如何解决这个问题?

1 个答案:

答案 0 :(得分:2)

range of the time data type is 00:00:00 to 23:59:59.999...。因此,即使您使用C#为您创建正确的DateTime对象,也无法使用您在存储过程中选择的数据类型执行所需的操作。

由于SQL Server没有像.NET那样的TimeSpan数据类型(这是正确的选择),一个简单的替代方法是将@TimeRange作为整数传递,用于指定秒数/分钟数/小时(取决于您要提供的粒度)。在T-SQL中,使用DATEADD function将值添加到@Start