2

我有一个查询,我将它作为数据表放入数据集中。查询运行良好,但是在数据表中它没有在数据列中列出任何内容,因为我无法报告它...任何想法为什么这不起作用?我的查询如下。

SELECT * FROM (
    SELECT timeclock.dtTimeIn, timeclock.dtTimeOut, employees.sfirstname,
       RANK() OVER ( ORDER BY dtTimeIn) rk1, --earliest record gets 1
       RANK() OVER (ORDER BY dtTimeOut DESC) rk2 --latest record gets 1

    FROM   TimeClock INNER JOIN
                         Employees ON TimeClock.lEmployeeID = Employees.lEmployeeID
    WHERE (dtTimeIn > dateadd(day, datediff(day, 0, getdate())-1, 0)) AND (dtTimeOut < dateadd(day, datediff(day, 0, getdate()), 0)) AND 
      (sDept IN ('1', '2', '3'))
) A
WHERE rk2=1 

我运行查询时的当前输出:

  dtTimeIn                    dtTimeOut       sfirstname    rk1      rk2
2/7/2013 2:36:00 PM 2/7/2013 7:52:33 PM        Brian        10        1

在此处输入图像描述

在向导中创建表适配器时...我收到一条错误消息:

The Wizard detected the following problems when configuring the TableAdapter:
"OpenTime":

Details:
Generated SELECT statement.
The OVER SQL construct or statement is not supported
To add these components to your dataset, click Finish.

我猜它不喜欢查询...但我不确定除了使用“OVER”功能之外如何完成查询...

4

3 回答 3

8

由于TableAdapter向导的限制,您必须创建DataTable使用代码。

DataTable dataTable;

using (SqlConnection sqlConn = new SqlConnection())
{
    sqlConn.Open();

    using (SqlDataAdapter sqlDataAdapter = new SqlDataAdapter())
    using (sqlDataAdapter.SelectCommand = sqlConn.CreateCommand())
    {

        sqlDataAdapter.SelectCommand.CommandType = CommandType.Text;
        sqlDataAdapter.SelectCommand.CommandText = '
            SELECT * FROM (
                SELECT timeclock.dtTimeIn, timeclock.dtTimeOut, employees.sfirstname,
                    RANK() OVER ( ORDER BY dtTimeIn) rk1, --earliest record gets 1
                    RANK() OVER (ORDER BY dtTimeOut DESC) rk2 --latest record gets 1

                FROM TimeClock INNER JOIN
                    Employees ON TimeClock.lEmployeeID = Employees.lEmployeeID
                WHERE (dtTimeIn > dateadd(day, datediff(day, 0, getdate())-1, 0)) AND (dtTimeOut < dateadd(day, datediff(day, 0, getdate()), 0)) AND (sDept IN ('1', '2', '3'))
            ) A
            WHERE rk2=1';

        sqlDataAdapter.Fill(dataTable);
    }
}

填写完毕后dataTable,只需将其分配给 ReportViewer 的DataSource

ReportDataSource rds = new ReportDataSource(dataTable.TableName, dataTable);
ReportViewer1.LocalReport.DataSources.Clear();
ReportViewer1.LocalReport.DataSources.Add(rds);

请注意,这些代码片段中有很大一部分未经测试。

于 2013-02-13T21:47:49.400 回答
6

我认为不要使用拖放界面来创建 tableAdapter,因为您的查询包含OVER SQL构造(如错误所说),您必须创建 tableAdapter 并在代码中填充数据表。

于 2013-02-11T18:34:36.070 回答
3

您可以从查询中创建一个视图,然后简单地从您的视图中选择,这应该从 tableAdapter 中隐藏 OVER SQL。

于 2013-02-13T21:11:42.000 回答