admin

如何在数据集数据表中显示数据列

sql

我有一个查询,我将它作为数据表放入数据集中。查询运行得很好,但是在数据表中它没有在“数据列”中列出任何内容,因为我无法报告它。我的查询如下。

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”功能外还有其他方法来完成查询…


阅读 185

收藏
2021-06-07

共1个答案

admin

由于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);

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

2021-06-07