0

在 Sql Server 中工作的查询。

Declare @from datetime, @to datetime, @status int, @SupervisorID uniqueidentifier
SELECT @to = '09-12-2012', @from = '02-02-2012', @status = 0, @SupervisorID = '2EB087F0-A419-4421-861A-6EBDFED9449A'

select nums.date as DayOff, [RequestID], [EmployeeName], [AbsenceType], [AbsenceBundle], [DateRequested], [FirstDay], [LastDay], [ReturnDay], [SupervisorName], [DaysHoursDesc] as DaysAbsent, [Description], Hours from
            (select dateadd(day, number, @from) as date
            from 
                (select distinct number from Numbers
                ) num
            where dateadd(day, number, @from) < @to) nums right outer join [AbsenceRequests].[dbo].[AbsenceRequestWithDescriptions] a on (DateDiff(Day, a.FirstDay, nums.date) >= 0 and DateDiff(Day, a.LastDay, nums.date) <=0)
            where
                 (([SupervisorID] = @SupervisorID) or ([EmployeeID] = @SupervisorID)
                            or 
                 ([SupervisorID] in  (select SupervisorID from Viewers where [viewerID] = @SupervisorID)))
and((@status = StatusID) or (@status = '0'))
            order by SupervisorName, EmployeeName, FirstDay, Hours 

我从这个查询中得到的结果

2012-06-06 00:00:00.000 515 apptest1    Vacation    55 - Vacation   2012-06-28 00:00:00.000 2012-06-06 00:00:00.000 2012-06-07 00:00:00.000 2012-06-08 00:00:00.000 apptest3    16 Hour(s)  Pending 16

SqlDatasource 定义

  <asp:SqlDataSource ID="EmployeeAbsenceRequestsSql" runat="server"  
        ConnectionString="<%$ ConnectionStrings:AbsenceRequestsConnectionString %>" 

            SelectCommand="
            select nums.date as DayOff, [RequestID], [EmployeeName], [AbsenceType], [AbsenceBundle], [DateRequested], [FirstDay], [LastDay], [ReturnDay], [SupervisorName], [DaysHoursDesc] as DaysAbsent, [Description], Hours from
            (select dateadd(day, number, @from) as date
            from 
                (select distinct number from Numbers
                ) num
            where dateadd(day, number, @from) &lt; @to) nums right outer join [AbsenceRequests].[dbo].[AbsenceRequestWithDescriptions] a on (DateDiff(Day, a.FirstDay, nums.date) &gt;= 0 and DateDiff(Day, a.LastDay, nums.date) &lt;=0)
            where
                 (([SupervisorID] = @SupervisorID) or ([EmployeeID] = @SupervisorID)
                            or 
                 ([SupervisorID] in  (select SupervisorID from Viewers where [viewerID] = @SupervisorID)))
                 and ((@status = StatusID) or (@status = '0'))
            order by SupervisorName, EmployeeName, FirstDay, Hours " 
            onselecting="EmployeeAbsenceRequestsSql_Selecting" >
            <SelectParameters>
                <asp:FormParameter DefaultValue=" " FormField="Fromtxt" Name="from" />
                <asp:FormParameter DefaultValue=" " FormField="Totxt" Name="to" />
                <asp:SessionParameter DefaultValue="&quot;&quot;" Name="SupervisorID" 
                    SessionField="EmployeeID" Type="Object" />
                <asp:ControlParameter ControlID="Statusddl" DefaultValue="0" PropertyName="SelectedValue"  Name="status" />
            </SelectParameters>
        </asp:SqlDataSource>

当我调用数据源的 select 方法时,我得到了结果,但第一列 DayOff 返回空白(我通过设置手表和检查 DataView 验证了这一点)。下面是我用来执行和读取选择查询结果的代码。

   protected void RefreshTreeview()
    {
        this.EmployeeAbsenceRequestsSql.SelectParameters["from"].DefaultValue = SafeDateString(this.Fromtxt.Text);
        this.EmployeeAbsenceRequestsSql.SelectParameters["to"].DefaultValue = SafeDateString(this.Totxt.Text);
        DataView dv = (DataView) this.EmployeeAbsenceRequestsSql.Select(new DataSourceSelectArguments());

        FillTreeView(dv);
    }

private void FillTreeView(DataView dv)
{
    TreeNode CurrentSupervisorNode =  null;
    TreeNode CurrentEmployeeNode = null;
    foreach (DataRow row in dv.Table.Rows)
    {
        String SupervisorName = SafeString(row["SupervisorName"]);
        if ((CurrentSupervisorNode == null) || !CurrentSupervisorNode.Text.Equals(SupervisorName))
        {
            CurrentSupervisorNode = new TreeNode(SupervisorName);
            this.AbsencesTV.Nodes.Add(CurrentSupervisorNode);
            CurrentEmployeeNode = null;
        }
        String EmployeeName = SafeString(row["EmployeeName"]);
        if (CurrentEmployeeNode == null || !CurrentEmployeeNode.Text.Equals(EmployeeName))
        {
            CurrentEmployeeNode = new TreeNode(EmployeeName);
            CurrentSupervisorNode.ChildNodes.Add(CurrentEmployeeNode);
        }

        String DayOffSummary = SafeString(row["DayOff"]) + " " + SafeString(row["AbsenceBundle"]) +" " + SafeString(row["Description"]);
        CurrentEmployeeNode.ChildNodes.Add(new TreeNode(DayOffSummary));
    }
}

有没有人有任何调试建议或者更好的答案来解释为什么会发生这种情况?

4

1 回答 1

0

感谢 simon 的精彩评论,我能够发现 @to 的默认空白值被传递,而不是我试图使用的默认值。当我为 @to 值指定日期时,我得到了我期望的结果。

于 2012-07-24T18:31:18.920 回答