本文介绍了从数据集MM格式:获得总HH的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

CREATE procedure St_Proc_GetUserReportforCurrentDayTask
@userID int
as
    Begin
        set NoCount on;
        DECLARE @TODAY DATE
        SET @TODAY = CONVERT(VARCHAR(10), GETDATE(), 111)
        select  CONVERT(VARCHAR,production.CalendarDate,101) + RIGHT (CONVERT(VARCHAR,production.CalendarDate , 100 ) ,7) as Date,
        RegionAndProjectInfo.RegionProjectName as Region ,
        County.CountyName as County,
        WorkType.WorkTypeName as WorkType,
        Task.TaskName as Task,
        Production.VolumeProcessed as 'Volumes Processed',
        Production.TimeSpent as 'Duration'
        from Production
        inner join RegionAndProjectInfo
        on
        RegionAndProjectInfo.RegionProjectID=Production.RegionProjectID
        inner join County
        on
        County.CountyID=Production.CountyID
        inner join WorkType
        on
        WorkType.WorkTypeID=Production.WorkTypeID
        inner join Task
        on
        Task.TaskID=Production.TaskID
        where Production.UserID=@userID and CalendarDate >= @TODAY
    End

从上面的存储过程我正在填充Dataset.After说我是这个数据集绑定到一个网格视图。
在数据集中,列包含时间中的数据HH:MM格式(例如,01:00,12:45,02:59等)。是有办法,我可以得到HH总持续时间:从MM格式数据itself.I不用想从数据库中再次查询来获取时间的总和。

From the above Stored Procedure i am filling a Dataset.After that i am binding this Dataset to a grid view.In the dataset ,the Column Duration contains Data in HH:MM Format(example-01:00,12:45,02:59 etc).Is there a way that i can get the total of Duration in HH:MM format from the dataset itself.I dont want to query again from the Database to get the Sum of the Duration.

推荐答案

如果你使用的至少是SQL-Server 2008中,你应该使用时间数据类型重新present一个.NET 时间跨度

If you're using at least SQL-Server 2008, you should use Time datatype to represent a .NET TimeSpan.

要让它有一个VARCHAR列的工作,我会用 LINQ到数据集,例如:

To get it to work with a varchar-column, i would use Linq-To-DataSet, for example:

var timeSpans = dataSet.Tables[0].AsEnumerable()
    .Select(r => new {
        DurHour   = r.Field<String>("Duration").Split(':')[0],
        DurMinute = r.Field<String>("Duration").Split(':')[1]
    })
    .Select(x => new TimeSpan(int.Parse(x.DurHour), int.Parse(x.DurMinute), 0));

Console.WriteLine("Total time in minutes: {0}", timeSpans.Sum(ts=>ts.TotalMinutes));

不过,这只是一种变通方法,其实你真的应该避免存放在varchar列一个时间跨度,使用VARCHAR只有文本,没有别的。

But that is only a workaround, actually you should really avoid to store a timespan in a varchar column, use varchar only for text and nothing else.

在SQL-Server 2005中(或其他DBMS),你也可以使用两列,一为启动的DateTime 和一个结束的DateTime

In SQL-Server 2005 (or other dbms) you could also use two columns, one for the start DateTime and one for the end DateTime.

这篇关于从数据集MM格式:获得总HH的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 09:31